mirror of
https://github.com/natekspencer/hacs-oasis_mini.git
synced 2025-12-06 18:44:14 -05:00
Switch to using mqtt
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""Oasis Mini coordinator."""
|
||||
"""Oasis devices coordinator."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -11,18 +11,23 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DOMAIN
|
||||
from .pyoasismini import OasisMini
|
||||
from .pyoasiscontrol import OasisCloudClient, OasisDevice, OasisMqttClient
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OasisMiniCoordinator(DataUpdateCoordinator[str]):
|
||||
"""Oasis Mini data update coordinator."""
|
||||
class OasisDeviceCoordinator(DataUpdateCoordinator[list[OasisDevice]]):
|
||||
"""Oasis device data update coordinator."""
|
||||
|
||||
attempt: int = 0
|
||||
last_updated: datetime | None = None
|
||||
|
||||
def __init__(self, hass: HomeAssistant, device: OasisMini) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
cloud_client: OasisCloudClient,
|
||||
mqtt_client: OasisMqttClient,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(
|
||||
hass,
|
||||
@@ -31,32 +36,46 @@ class OasisMiniCoordinator(DataUpdateCoordinator[str]):
|
||||
update_interval=timedelta(seconds=10),
|
||||
always_update=False,
|
||||
)
|
||||
self.device = device
|
||||
self.cloud_client = cloud_client
|
||||
self.mqtt_client = mqtt_client
|
||||
|
||||
async def _async_update_data(self):
|
||||
async def _async_update_data(self) -> list[OasisDevice]:
|
||||
"""Update the data."""
|
||||
data: str | None = None
|
||||
devices: list[OasisDevice] = []
|
||||
self.attempt += 1
|
||||
|
||||
try:
|
||||
async with async_timeout.timeout(10):
|
||||
if not self.device.mac_address:
|
||||
await self.device.async_get_mac_address()
|
||||
if not self.device.serial_number:
|
||||
await self.device.async_get_serial_number()
|
||||
if not self.device.software_version:
|
||||
await self.device.async_get_software_version()
|
||||
data = await self.device.async_get_status()
|
||||
if not self.data:
|
||||
raw_devices = await self.cloud_client.async_get_devices()
|
||||
devices = [
|
||||
OasisDevice(
|
||||
model=raw_device.get("model", {}).get("name"),
|
||||
serial_number=raw_device.get("serial_number"),
|
||||
)
|
||||
for raw_device in raw_devices
|
||||
]
|
||||
else:
|
||||
devices = self.data
|
||||
for device in devices:
|
||||
self.mqtt_client.register_device(device)
|
||||
await self.mqtt_client.wait_until_ready(device, request_status=True)
|
||||
if not device.mac_address:
|
||||
await device.async_get_mac_address()
|
||||
# if not device.software_version:
|
||||
# await device.async_get_software_version()
|
||||
# data = await self.device.async_get_status()
|
||||
# devices = self.cloud_client.mac_address
|
||||
self.attempt = 0
|
||||
await self.device.async_get_current_track_details()
|
||||
await self.device.async_get_playlist_details()
|
||||
await self.device.async_cloud_get_playlists()
|
||||
# await self.device.async_get_current_track_details()
|
||||
# await self.device.async_get_playlist_details()
|
||||
# await self.device.async_cloud_get_playlists()
|
||||
except Exception as ex: # pylint:disable=broad-except
|
||||
if self.attempt > 2 or not (data or self.data):
|
||||
if self.attempt > 2 or not (devices or self.data):
|
||||
raise UpdateFailed(
|
||||
f"Couldn't read from the Oasis Mini after {self.attempt} attempts"
|
||||
f"Couldn't read from the Oasis device after {self.attempt} attempts"
|
||||
) from ex
|
||||
|
||||
if data != self.data:
|
||||
if devices != self.data:
|
||||
self.last_updated = datetime.now()
|
||||
return data
|
||||
return devices
|
||||
|
||||
Reference in New Issue
Block a user