mirror of
https://github.com/natekspencer/hacs-oasis_mini.git
synced 2025-12-06 18:44:14 -05:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf21a5d995 | ||
|
|
83de1d5606 | ||
|
|
2a92212aad | ||
|
|
ecad472bbd |
10
README.md
10
README.md
@@ -10,9 +10,9 @@
|
|||||||
<img alt="Oasis Mini logo" src="https://brands.home-assistant.io/oasis_mini/logo.png">
|
<img alt="Oasis Mini logo" src="https://brands.home-assistant.io/oasis_mini/logo.png">
|
||||||
</picture>
|
</picture>
|
||||||
|
|
||||||
# Oasis Mini for Home Assistant
|
# Oasis Control for Home Assistant
|
||||||
|
|
||||||
Home Assistant integration for Oasis Mini kinetic sand art devices.
|
Home Assistant integration for Oasis kinetic sand art devices.
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
@@ -43,13 +43,13 @@ While the manual installation above seems like less steps, it's important to not
|
|||||||
|
|
||||||
[](https://my.home-assistant.io/redirect/config_flow_start/?domain=oasis_mini)
|
[](https://my.home-assistant.io/redirect/config_flow_start/?domain=oasis_mini)
|
||||||
|
|
||||||
There is a config flow for this Oasis Mini integration. After installing the custom component, use the convenient My Home Assistant link above.
|
There is a config flow for this Oasis Control integration. After installing the custom component, use the convenient My Home Assistant link above.
|
||||||
|
|
||||||
Alternatively:
|
Alternatively:
|
||||||
|
|
||||||
1. Go to **Configuration**->**Integrations**
|
1. Go to **Configuration**->**Integrations**
|
||||||
2. Click **+ ADD INTEGRATION** to setup a new integration
|
2. Click **+ ADD INTEGRATION** to setup a new integration
|
||||||
3. Search for **Oasis Mini** and click on it
|
3. Search for **Oasis Control** and click on it
|
||||||
4. You will be guided through the rest of the setup process via the config flow
|
4. You will be guided through the rest of the setup process via the config flow
|
||||||
|
|
||||||
# Options
|
# Options
|
||||||
@@ -76,6 +76,6 @@ data:
|
|||||||
|
|
||||||
I'm not employed by Kinetic Oasis, and provide this custom component purely for your own enjoyment and home automation needs.
|
I'm not employed by Kinetic Oasis, and provide this custom component purely for your own enjoyment and home automation needs.
|
||||||
|
|
||||||
If you already own an Oasis Mini, found this integration useful and want to donate, consider [sponsoring me on GitHub](https://github.com/sponsors/natekspencer) or buying me a coffee ☕ (or beer 🍺) instead by using the link below:
|
If you already own an Oasis device, found this integration useful and want to donate, consider [sponsoring me on GitHub](https://github.com/sponsors/natekspencer) or buying me a coffee ☕ (or beer 🍺) instead by using the link below:
|
||||||
|
|
||||||
<a href='https://ko-fi.com/Y8Y57F59S' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
|
<a href='https://ko-fi.com/Y8Y57F59S' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://storage.ko-fi.com/cdn/kofi1.png?v=3' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
|
||||||
|
|||||||
@@ -3,17 +3,22 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any, Callable, Iterable
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import CONF_EMAIL, Platform
|
from homeassistant.const import CONF_EMAIL, Platform
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||||
|
from homeassistant.helpers.device_registry import DeviceEntry
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.helpers.entity_registry as er
|
import homeassistant.helpers.entity_registry as er
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
from .coordinator import OasisDeviceCoordinator
|
from .coordinator import OasisDeviceCoordinator
|
||||||
|
from .entity import OasisDeviceEntity
|
||||||
from .helpers import create_client
|
from .helpers import create_client
|
||||||
from .pyoasiscontrol import OasisMqttClient, UnauthenticatedError
|
from .pyoasiscontrol import OasisDevice, OasisMqttClient, UnauthenticatedError
|
||||||
|
|
||||||
type OasisDeviceConfigEntry = ConfigEntry[OasisDeviceCoordinator]
|
type OasisDeviceConfigEntry = ConfigEntry[OasisDeviceCoordinator]
|
||||||
|
|
||||||
@@ -28,10 +33,47 @@ PLATFORMS = [
|
|||||||
Platform.NUMBER,
|
Platform.NUMBER,
|
||||||
Platform.SELECT,
|
Platform.SELECT,
|
||||||
Platform.SENSOR,
|
Platform.SENSOR,
|
||||||
|
Platform.SWITCH,
|
||||||
Platform.UPDATE,
|
Platform.UPDATE,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def setup_platform_from_coordinator(
|
||||||
|
entry: OasisDeviceConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
make_entities: Callable[[OasisDevice], Iterable[OasisDeviceEntity]],
|
||||||
|
update_before_add: bool = False,
|
||||||
|
) -> None:
|
||||||
|
"""Generic pattern: add entities per device, including newly discovered ones."""
|
||||||
|
coordinator = entry.runtime_data
|
||||||
|
|
||||||
|
known_serials: set[str] = set()
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def _check_devices() -> None:
|
||||||
|
devices = coordinator.data or []
|
||||||
|
new_devices: list[OasisDevice] = []
|
||||||
|
|
||||||
|
for device in devices:
|
||||||
|
serial = device.serial_number
|
||||||
|
if not serial or serial in known_serials:
|
||||||
|
continue
|
||||||
|
|
||||||
|
known_serials.add(serial)
|
||||||
|
new_devices.append(device)
|
||||||
|
|
||||||
|
if not new_devices:
|
||||||
|
return
|
||||||
|
|
||||||
|
if entities := make_entities(new_devices):
|
||||||
|
async_add_entities(entities, update_before_add)
|
||||||
|
|
||||||
|
# Initial population
|
||||||
|
_check_devices()
|
||||||
|
# Future updates (new devices discovered)
|
||||||
|
entry.async_on_unload(coordinator.async_add_listener(_check_devices))
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, entry: OasisDeviceConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, entry: OasisDeviceConfigEntry) -> bool:
|
||||||
"""Set up Oasis devices from a config entry."""
|
"""Set up Oasis devices from a config entry."""
|
||||||
cloud_client = create_client(hass, entry.data)
|
cloud_client = create_client(hass, entry.data)
|
||||||
@@ -59,6 +101,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: OasisDeviceConfigEntry)
|
|||||||
entry.runtime_data = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
|
||||||
def _on_oasis_update() -> None:
|
def _on_oasis_update() -> None:
|
||||||
|
coordinator.last_updated = dt_util.now()
|
||||||
coordinator.async_update_listeners()
|
coordinator.async_update_listeners()
|
||||||
|
|
||||||
for device in coordinator.data:
|
for device in coordinator.data:
|
||||||
@@ -146,3 +189,15 @@ async def async_migrate_entry(hass: HomeAssistant, entry: OasisDeviceConfigEntry
|
|||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
async def async_remove_config_entry_device(
|
||||||
|
hass: HomeAssistant, config_entry: OasisDeviceConfigEntry, device_entry: DeviceEntry
|
||||||
|
) -> bool:
|
||||||
|
"""Remove a config entry from a device."""
|
||||||
|
current_serials = {d.serial_number for d in (config_entry.runtime_data.data or [])}
|
||||||
|
return not any(
|
||||||
|
identifier
|
||||||
|
for identifier in device_entry.identifiers
|
||||||
|
if identifier[0] == DOMAIN and identifier[1] in current_serials
|
||||||
|
)
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ from homeassistant.const import EntityCategory
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
|
from .pyoasiscontrol import OasisDevice
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@@ -22,12 +22,15 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device sensors using config entry."""
|
"""Set up Oasis device sensors using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
async_add_entities(
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceBinarySensorEntity(coordinator, device, descriptor)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceBinarySensorEntity(entry.runtime_data, device, descriptor)
|
||||||
|
for device in new_devices
|
||||||
for descriptor in DESCRIPTORS
|
for descriptor in DESCRIPTORS
|
||||||
)
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTORS = {
|
DESCRIPTORS = {
|
||||||
|
|||||||
@@ -13,10 +13,10 @@ from homeassistant.components.button import (
|
|||||||
)
|
)
|
||||||
from homeassistant.const import EntityCategory
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
from .helpers import add_and_play_track
|
from .helpers import add_and_play_track
|
||||||
from .pyoasiscontrol import OasisDevice
|
from .pyoasiscontrol import OasisDevice
|
||||||
@@ -29,18 +29,24 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device button using config entry."""
|
"""Set up Oasis device button using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
async_add_entities(
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceButtonEntity(coordinator, device, descriptor)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceButtonEntity(entry.runtime_data, device, descriptor)
|
||||||
|
for device in new_devices
|
||||||
for descriptor in DESCRIPTORS
|
for descriptor in DESCRIPTORS
|
||||||
)
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
|
|
||||||
|
|
||||||
async def play_random_track(device: OasisDevice) -> None:
|
async def play_random_track(device: OasisDevice) -> None:
|
||||||
"""Play random track."""
|
"""Play random track."""
|
||||||
track = random.choice(list(TRACKS))
|
track = random.choice(list(TRACKS))
|
||||||
|
try:
|
||||||
await add_and_play_track(device, track)
|
await add_and_play_track(device, track)
|
||||||
|
except TimeoutError as err:
|
||||||
|
raise HomeAssistantError("Timeout adding track to queue") from err
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
@@ -78,4 +84,3 @@ class OasisDeviceButtonEntity(OasisDeviceEntity, ButtonEntity):
|
|||||||
async def async_press(self) -> None:
|
async def async_press(self) -> None:
|
||||||
"""Press the button."""
|
"""Press the button."""
|
||||||
await self.entity_description.press_fn(self.device)
|
await self.entity_description.press_fn(self.device)
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ import logging
|
|||||||
import async_timeout
|
import async_timeout
|
||||||
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||||
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .pyoasiscontrol import OasisCloudClient, OasisDevice, OasisMqttClient
|
from .pyoasiscontrol import OasisCloudClient, OasisDevice, OasisMqttClient
|
||||||
@@ -33,7 +35,7 @@ class OasisDeviceCoordinator(DataUpdateCoordinator[list[OasisDevice]]):
|
|||||||
hass,
|
hass,
|
||||||
_LOGGER,
|
_LOGGER,
|
||||||
name=DOMAIN,
|
name=DOMAIN,
|
||||||
update_interval=timedelta(seconds=10),
|
update_interval=timedelta(minutes=10),
|
||||||
always_update=False,
|
always_update=False,
|
||||||
)
|
)
|
||||||
self.cloud_client = cloud_client
|
self.cloud_client = cloud_client
|
||||||
@@ -45,37 +47,114 @@ class OasisDeviceCoordinator(DataUpdateCoordinator[list[OasisDevice]]):
|
|||||||
self.attempt += 1
|
self.attempt += 1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with async_timeout.timeout(10):
|
async with async_timeout.timeout(30):
|
||||||
if not self.data:
|
|
||||||
raw_devices = await self.cloud_client.async_get_devices()
|
raw_devices = await self.cloud_client.async_get_devices()
|
||||||
devices = [
|
|
||||||
OasisDevice(
|
existing_by_serial = {
|
||||||
model=raw_device.get("model", {}).get("name"),
|
d.serial_number: d for d in (self.data or []) if d.serial_number
|
||||||
serial_number=raw_device.get("serial_number"),
|
}
|
||||||
)
|
|
||||||
for raw_device in raw_devices
|
for raw in raw_devices:
|
||||||
]
|
if not (serial := raw.get("serial_number")):
|
||||||
|
continue
|
||||||
|
|
||||||
|
if device := existing_by_serial.get(serial):
|
||||||
|
if name := raw.get("name"):
|
||||||
|
device.name = name
|
||||||
else:
|
else:
|
||||||
devices = self.data
|
device = OasisDevice(
|
||||||
for device in devices:
|
model=(raw.get("model") or {}).get("name"),
|
||||||
self.mqtt_client.register_device(device)
|
serial_number=serial,
|
||||||
await self.mqtt_client.wait_until_ready(device, request_status=True)
|
name=raw.get("name"),
|
||||||
if not device.mac_address:
|
cloud=self.cloud_client,
|
||||||
await device.async_get_mac_address()
|
)
|
||||||
# if not device.software_version:
|
|
||||||
# await device.async_get_software_version()
|
devices.append(device)
|
||||||
# data = await self.device.async_get_status()
|
|
||||||
# devices = self.cloud_client.mac_address
|
new_serials = {d.serial_number for d in devices if d.serial_number}
|
||||||
|
removed_serials = set(existing_by_serial) - new_serials
|
||||||
|
|
||||||
|
if removed_serials:
|
||||||
|
device_registry = dr.async_get(self.hass)
|
||||||
|
for serial in removed_serials:
|
||||||
|
_LOGGER.info(
|
||||||
|
"Oasis device %s removed from account; cleaning up in HA",
|
||||||
|
serial,
|
||||||
|
)
|
||||||
|
device_entry = device_registry.async_get_device(
|
||||||
|
identifiers={(DOMAIN, serial)}
|
||||||
|
)
|
||||||
|
if device_entry:
|
||||||
|
device_registry.async_update_device(
|
||||||
|
device_id=device_entry.id,
|
||||||
|
remove_config_entry_id=self.config_entry.entry_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
# ✅ Valid state: logged in but no devices on account
|
||||||
|
if not devices:
|
||||||
|
_LOGGER.debug("No Oasis devices found for account")
|
||||||
self.attempt = 0
|
self.attempt = 0
|
||||||
# await self.device.async_get_current_track_details()
|
if devices != self.data:
|
||||||
# await self.device.async_get_playlist_details()
|
self.last_updated = dt_util.now()
|
||||||
# await self.device.async_cloud_get_playlists()
|
return []
|
||||||
except Exception as ex: # pylint:disable=broad-except
|
|
||||||
|
self.mqtt_client.register_devices(devices)
|
||||||
|
|
||||||
|
# Best-effort playlists
|
||||||
|
try:
|
||||||
|
await self.cloud_client.async_get_playlists()
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
_LOGGER.exception("Error fetching playlists from cloud")
|
||||||
|
|
||||||
|
any_success = False
|
||||||
|
|
||||||
|
for device in devices:
|
||||||
|
try:
|
||||||
|
ready = await self.mqtt_client.wait_until_ready(
|
||||||
|
device, timeout=3, request_status=True
|
||||||
|
)
|
||||||
|
if not ready:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Timeout waiting for Oasis device %s to be ready",
|
||||||
|
device.serial_number,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
mac = await device.async_get_mac_address()
|
||||||
|
if not mac:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Could not get MAC address for Oasis device %s",
|
||||||
|
device.serial_number,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
any_success = True
|
||||||
|
device.schedule_track_refresh()
|
||||||
|
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
_LOGGER.exception(
|
||||||
|
"Error preparing Oasis device %s", device.serial_number
|
||||||
|
)
|
||||||
|
|
||||||
|
if any_success:
|
||||||
|
self.attempt = 0
|
||||||
|
else:
|
||||||
|
if self.attempt > 2 or not self.data:
|
||||||
|
raise UpdateFailed(
|
||||||
|
"Couldn't read from any Oasis device "
|
||||||
|
f"after {self.attempt} attempts"
|
||||||
|
)
|
||||||
|
|
||||||
|
except UpdateFailed:
|
||||||
|
raise
|
||||||
|
except Exception as ex: # noqa: BLE001
|
||||||
if self.attempt > 2 or not (devices or self.data):
|
if self.attempt > 2 or not (devices or self.data):
|
||||||
raise UpdateFailed(
|
raise UpdateFailed(
|
||||||
f"Couldn't read from the Oasis device after {self.attempt} attempts"
|
"Unexpected error talking to Oasis devices "
|
||||||
|
f"after {self.attempt} attempts"
|
||||||
) from ex
|
) from ex
|
||||||
|
|
||||||
if devices != self.data:
|
if devices != self.data:
|
||||||
self.last_updated = datetime.now()
|
self.last_updated = dt_util.now()
|
||||||
|
|
||||||
return devices
|
return devices
|
||||||
|
|||||||
@@ -30,10 +30,14 @@ class OasisDeviceEntity(CoordinatorEntity[OasisDeviceCoordinator]):
|
|||||||
serial_number = device.serial_number
|
serial_number = device.serial_number
|
||||||
self._attr_unique_id = f"{serial_number}-{description.key}"
|
self._attr_unique_id = f"{serial_number}-{description.key}"
|
||||||
|
|
||||||
|
connections = set()
|
||||||
|
if mac_address := device.mac_address:
|
||||||
|
connections.add((CONNECTION_NETWORK_MAC, format_mac(mac_address)))
|
||||||
|
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
connections={(CONNECTION_NETWORK_MAC, format_mac(device.mac_address))},
|
connections=connections,
|
||||||
identifiers={(DOMAIN, serial_number)},
|
identifiers={(DOMAIN, serial_number)},
|
||||||
name=f"{device.model} {serial_number}",
|
name=device.name,
|
||||||
manufacturer=device.manufacturer,
|
manufacturer=device.manufacturer,
|
||||||
model=device.model,
|
model=device.model,
|
||||||
serial_number=serial_number,
|
serial_number=serial_number,
|
||||||
|
|||||||
@@ -2,9 +2,12 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
import async_timeout
|
||||||
|
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
@@ -23,12 +26,18 @@ def create_client(hass: HomeAssistant, data: dict[str, Any]) -> OasisCloudClient
|
|||||||
|
|
||||||
async def add_and_play_track(device: OasisDevice, track: int) -> None:
|
async def add_and_play_track(device: OasisDevice, track: int) -> None:
|
||||||
"""Add and play a track."""
|
"""Add and play a track."""
|
||||||
|
async with async_timeout.timeout(10):
|
||||||
if track not in device.playlist:
|
if track not in device.playlist:
|
||||||
await device.async_add_track_to_playlist(track)
|
await device.async_add_track_to_playlist(track)
|
||||||
|
|
||||||
|
while track not in device.playlist:
|
||||||
|
await asyncio.sleep(0.1)
|
||||||
|
|
||||||
# Move track to next item in the playlist and then select it
|
# Move track to next item in the playlist and then select it
|
||||||
if (index := device.playlist.index(track)) != device.playlist_index:
|
if (index := device.playlist.index(track)) != device.playlist_index:
|
||||||
if index != (_next := min(device.playlist_index + 1, len(device.playlist) - 1)):
|
if index != (
|
||||||
|
_next := min(device.playlist_index + 1, len(device.playlist) - 1)
|
||||||
|
):
|
||||||
await device.async_move_track(index, _next)
|
await device.async_move_track(index, _next)
|
||||||
await device.async_change_track(_next)
|
await device.async_change_track(_next)
|
||||||
|
|
||||||
|
|||||||
@@ -6,13 +6,12 @@ from homeassistant.components.image import Image, ImageEntity, ImageEntityDescri
|
|||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import UNDEFINED
|
from homeassistant.helpers.typing import UNDEFINED
|
||||||
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
from .coordinator import OasisDeviceCoordinator
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
from .pyoasiscontrol import OasisDevice
|
from .pyoasiscontrol import OasisDevice
|
||||||
from .pyoasiscontrol.const import TRACKS
|
|
||||||
from .pyoasiscontrol.utils import draw_svg
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@@ -21,11 +20,14 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device image using config entry."""
|
"""Set up Oasis device image using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
async_add_entities(
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceImageEntity(coordinator, device, IMAGE)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceImageEntity(entry.runtime_data, device, IMAGE)
|
||||||
)
|
for device in new_devices
|
||||||
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
|
|
||||||
|
|
||||||
IMAGE = ImageEntityDescription(key="image", name=None)
|
IMAGE = ImageEntityDescription(key="image", name=None)
|
||||||
@@ -34,7 +36,6 @@ IMAGE = ImageEntityDescription(key="image", name=None)
|
|||||||
class OasisDeviceImageEntity(OasisDeviceEntity, ImageEntity):
|
class OasisDeviceImageEntity(OasisDeviceEntity, ImageEntity):
|
||||||
"""Oasis device image entity."""
|
"""Oasis device image entity."""
|
||||||
|
|
||||||
_attr_content_type = "image/svg+xml"
|
|
||||||
_track_id: int | None = None
|
_track_id: int | None = None
|
||||||
_progress: int = 0
|
_progress: int = 0
|
||||||
|
|
||||||
@@ -52,33 +53,33 @@ class OasisDeviceImageEntity(OasisDeviceEntity, ImageEntity):
|
|||||||
def image(self) -> bytes | None:
|
def image(self) -> bytes | None:
|
||||||
"""Return bytes of image."""
|
"""Return bytes of image."""
|
||||||
if not self._cached_image:
|
if not self._cached_image:
|
||||||
self._cached_image = Image(
|
if (svg := self.device.create_svg()) is None:
|
||||||
self.content_type, draw_svg(self.device.track, self._progress, "1")
|
self._attr_image_url = self.device.track_image_url
|
||||||
)
|
self._attr_image_last_updated = dt_util.now()
|
||||||
|
return None
|
||||||
|
self._attr_content_type = "image/svg+xml"
|
||||||
|
self._cached_image = Image(self.content_type, svg)
|
||||||
return self._cached_image.content
|
return self._cached_image.content
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
"""Handle updated data from the coordinator."""
|
"""Handle updated data from the coordinator."""
|
||||||
if (
|
device = self.device
|
||||||
self._track_id != self.device.track_id
|
|
||||||
or self._progress != self.device.progress
|
track_changed = self._track_id != device.track_id
|
||||||
) and (self.device.status == "playing" or self._cached_image is None):
|
progress_changed = self._progress != device.progress
|
||||||
|
allow_update = device.status == "playing" or self._cached_image is None
|
||||||
|
|
||||||
|
if (track_changed or progress_changed) and allow_update:
|
||||||
self._attr_image_last_updated = self.coordinator.last_updated
|
self._attr_image_last_updated = self.coordinator.last_updated
|
||||||
self._track_id = self.device.track_id
|
self._track_id = device.track_id
|
||||||
self._progress = self.device.progress
|
self._progress = device.progress
|
||||||
self._cached_image = None
|
self._cached_image = None
|
||||||
if self.device.track and self.device.track.get("svg_content"):
|
|
||||||
|
if device.track and device.track.get("svg_content"):
|
||||||
self._attr_image_url = UNDEFINED
|
self._attr_image_url = UNDEFINED
|
||||||
else:
|
else:
|
||||||
self._attr_image_url = (
|
self._attr_image_url = device.track_image_url
|
||||||
f"https://app.grounded.so/uploads/{track['image']}"
|
|
||||||
if (
|
|
||||||
track := (self.device.track or TRACKS.get(self.device.track_id))
|
|
||||||
)
|
|
||||||
and "image" in track
|
|
||||||
else None
|
|
||||||
)
|
|
||||||
|
|
||||||
if self.hass:
|
if self.hass:
|
||||||
super()._handle_coordinator_update()
|
super()._handle_coordinator_update()
|
||||||
|
|||||||
@@ -23,9 +23,9 @@ from homeassistant.util.color import (
|
|||||||
value_to_brightness,
|
value_to_brightness,
|
||||||
)
|
)
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
|
from .pyoasiscontrol import OasisDevice
|
||||||
from .pyoasiscontrol.const import LED_EFFECTS
|
from .pyoasiscontrol.const import LED_EFFECTS
|
||||||
|
|
||||||
|
|
||||||
@@ -35,11 +35,14 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device lights using config entry."""
|
"""Set up Oasis device lights using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
async_add_entities(
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceLightEntity(coordinator, device, DESCRIPTOR)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceLightEntity(entry.runtime_data, device, DESCRIPTOR)
|
||||||
)
|
for device in new_devices
|
||||||
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTOR = LightEntityDescription(key="led", translation_key="led")
|
DESCRIPTOR = LightEntityDescription(key="led", translation_key="led")
|
||||||
@@ -53,7 +56,7 @@ class OasisDeviceLightEntity(OasisDeviceEntity, LightEntity):
|
|||||||
@property
|
@property
|
||||||
def brightness(self) -> int:
|
def brightness(self) -> int:
|
||||||
"""Return the brightness of this light between 0..255."""
|
"""Return the brightness of this light between 0..255."""
|
||||||
scale = (1, self.device.max_brightness)
|
scale = (1, self.device.brightness_max)
|
||||||
return value_to_brightness(scale, self.device.brightness)
|
return value_to_brightness(scale, self.device.brightness)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@@ -99,15 +102,14 @@ class OasisDeviceLightEntity(OasisDeviceEntity, LightEntity):
|
|||||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity off."""
|
"""Turn the entity off."""
|
||||||
await self.device.async_set_led(brightness=0)
|
await self.device.async_set_led(brightness=0)
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Turn the entity on."""
|
"""Turn the entity on."""
|
||||||
if brightness := kwargs.get(ATTR_BRIGHTNESS):
|
if brightness := kwargs.get(ATTR_BRIGHTNESS):
|
||||||
scale = (1, self.device.max_brightness)
|
scale = (1, self.device.brightness_max)
|
||||||
brightness = math.ceil(brightness_to_value(scale, brightness))
|
brightness = math.ceil(brightness_to_value(scale, brightness))
|
||||||
else:
|
else:
|
||||||
brightness = self.device.brightness or 100
|
brightness = self.device.brightness or self.device.brightness_on
|
||||||
|
|
||||||
if color := kwargs.get(ATTR_RGB_COLOR):
|
if color := kwargs.get(ATTR_RGB_COLOR):
|
||||||
color = f"#{color_rgb_to_hex(*color)}"
|
color = f"#{color_rgb_to_hex(*color)}"
|
||||||
@@ -120,4 +122,3 @@ class OasisDeviceLightEntity(OasisDeviceEntity, LightEntity):
|
|||||||
await self.device.async_set_led(
|
await self.device.async_set_led(
|
||||||
brightness=brightness, color=color, led_effect=led_effect
|
brightness=brightness, color=color, led_effect=led_effect
|
||||||
)
|
)
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|||||||
@@ -18,12 +18,11 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.exceptions import ServiceValidationError
|
from homeassistant.exceptions import ServiceValidationError
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .coordinator import OasisDeviceCoordinator
|
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
from .helpers import get_track_id
|
from .helpers import get_track_id
|
||||||
from .pyoasiscontrol.const import TRACKS
|
from .pyoasiscontrol import OasisDevice
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@@ -32,11 +31,14 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device media_players using config entry."""
|
"""Set up Oasis device media_players using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
async_add_entities(
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceMediaPlayerEntity(coordinator, device, DESCRIPTOR)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceMediaPlayerEntity(entry.runtime_data, device, DESCRIPTOR)
|
||||||
)
|
for device in new_devices
|
||||||
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTOR = MediaPlayerEntityDescription(key="oasis_mini", name=None)
|
DESCRIPTOR = MediaPlayerEntityDescription(key="oasis_mini", name=None)
|
||||||
@@ -73,11 +75,7 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
@property
|
@property
|
||||||
def media_image_url(self) -> str | None:
|
def media_image_url(self) -> str | None:
|
||||||
"""Image url of current playing media."""
|
"""Image url of current playing media."""
|
||||||
if not (track := self.device.track):
|
return self.device.track_image_url
|
||||||
track = TRACKS.get(self.device.track_id)
|
|
||||||
if track and "image" in track:
|
|
||||||
return f"https://app.grounded.so/uploads/{track['image']}"
|
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def media_position(self) -> int:
|
def media_position(self) -> int:
|
||||||
@@ -92,11 +90,7 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
@property
|
@property
|
||||||
def media_title(self) -> str | None:
|
def media_title(self) -> str | None:
|
||||||
"""Title of current playing media."""
|
"""Title of current playing media."""
|
||||||
if not self.device.track_id:
|
return self.device.track_name
|
||||||
return None
|
|
||||||
if not (track := self.device.track):
|
|
||||||
track = TRACKS.get(self.device.track_id, {})
|
|
||||||
return track.get("name", f"Unknown Title (#{self.device.track_id})")
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def repeat(self) -> RepeatMode:
|
def repeat(self) -> RepeatMode:
|
||||||
@@ -134,19 +128,16 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
"""Send pause command."""
|
"""Send pause command."""
|
||||||
self.abort_if_busy()
|
self.abort_if_busy()
|
||||||
await self.device.async_pause()
|
await self.device.async_pause()
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_media_play(self) -> None:
|
async def async_media_play(self) -> None:
|
||||||
"""Send play command."""
|
"""Send play command."""
|
||||||
self.abort_if_busy()
|
self.abort_if_busy()
|
||||||
await self.device.async_play()
|
await self.device.async_play()
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_media_stop(self) -> None:
|
async def async_media_stop(self) -> None:
|
||||||
"""Send stop command."""
|
"""Send stop command."""
|
||||||
self.abort_if_busy()
|
self.abort_if_busy()
|
||||||
await self.device.async_stop()
|
await self.device.async_stop()
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_set_repeat(self, repeat: RepeatMode) -> None:
|
async def async_set_repeat(self, repeat: RepeatMode) -> None:
|
||||||
"""Set repeat mode."""
|
"""Set repeat mode."""
|
||||||
@@ -154,7 +145,6 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
repeat != RepeatMode.OFF
|
repeat != RepeatMode.OFF
|
||||||
and not (repeat == RepeatMode.ONE and self.repeat == RepeatMode.ALL)
|
and not (repeat == RepeatMode.ONE and self.repeat == RepeatMode.ALL)
|
||||||
)
|
)
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_media_previous_track(self) -> None:
|
async def async_media_previous_track(self) -> None:
|
||||||
"""Send previous track command."""
|
"""Send previous track command."""
|
||||||
@@ -162,7 +152,6 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
if (index := self.device.playlist_index - 1) < 0:
|
if (index := self.device.playlist_index - 1) < 0:
|
||||||
index = len(self.device.playlist) - 1
|
index = len(self.device.playlist) - 1
|
||||||
await self.device.async_change_track(index)
|
await self.device.async_change_track(index)
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_media_next_track(self) -> None:
|
async def async_media_next_track(self) -> None:
|
||||||
"""Send next track command."""
|
"""Send next track command."""
|
||||||
@@ -170,7 +159,6 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
if (index := self.device.playlist_index + 1) >= len(self.device.playlist):
|
if (index := self.device.playlist_index + 1) >= len(self.device.playlist):
|
||||||
index = 0
|
index = 0
|
||||||
await self.device.async_change_track(index)
|
await self.device.async_change_track(index)
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_play_media(
|
async def async_play_media(
|
||||||
self,
|
self,
|
||||||
@@ -220,10 +208,7 @@ class OasisDeviceMediaPlayerEntity(OasisDeviceEntity, MediaPlayerEntity):
|
|||||||
):
|
):
|
||||||
await device.async_play()
|
await device.async_play()
|
||||||
|
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
async def async_clear_playlist(self) -> None:
|
async def async_clear_playlist(self) -> None:
|
||||||
"""Clear players playlist."""
|
"""Clear players playlist."""
|
||||||
self.abort_if_busy()
|
self.abort_if_busy()
|
||||||
await self.device.async_clear_playlist()
|
await self.device.async_clear_playlist()
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ from homeassistant.components.number import (
|
|||||||
NumberEntityDescription,
|
NumberEntityDescription,
|
||||||
NumberMode,
|
NumberMode,
|
||||||
)
|
)
|
||||||
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
|
from .pyoasiscontrol import OasisDevice
|
||||||
from .pyoasiscontrol.device import (
|
from .pyoasiscontrol.device import (
|
||||||
BALL_SPEED_MAX,
|
BALL_SPEED_MAX,
|
||||||
BALL_SPEED_MIN,
|
BALL_SPEED_MIN,
|
||||||
@@ -27,18 +28,22 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device numbers using config entry."""
|
"""Set up Oasis device numbers using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
async_add_entities(
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceNumberEntity(coordinator, device, descriptor)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceNumberEntity(entry.runtime_data, device, descriptor)
|
||||||
|
for device in new_devices
|
||||||
for descriptor in DESCRIPTORS
|
for descriptor in DESCRIPTORS
|
||||||
)
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTORS = {
|
DESCRIPTORS = {
|
||||||
NumberEntityDescription(
|
NumberEntityDescription(
|
||||||
key="ball_speed",
|
key="ball_speed",
|
||||||
translation_key="ball_speed",
|
translation_key="ball_speed",
|
||||||
|
entity_category=EntityCategory.CONFIG,
|
||||||
mode=NumberMode.SLIDER,
|
mode=NumberMode.SLIDER,
|
||||||
native_max_value=BALL_SPEED_MAX,
|
native_max_value=BALL_SPEED_MAX,
|
||||||
native_min_value=BALL_SPEED_MIN,
|
native_min_value=BALL_SPEED_MIN,
|
||||||
@@ -46,6 +51,7 @@ DESCRIPTORS = {
|
|||||||
NumberEntityDescription(
|
NumberEntityDescription(
|
||||||
key="led_speed",
|
key="led_speed",
|
||||||
translation_key="led_speed",
|
translation_key="led_speed",
|
||||||
|
entity_category=EntityCategory.CONFIG,
|
||||||
mode=NumberMode.SLIDER,
|
mode=NumberMode.SLIDER,
|
||||||
native_max_value=LED_SPEED_MAX,
|
native_max_value=LED_SPEED_MAX,
|
||||||
native_min_value=LED_SPEED_MIN,
|
native_min_value=LED_SPEED_MIN,
|
||||||
@@ -63,8 +69,8 @@ class OasisDeviceNumberEntity(OasisDeviceEntity, NumberEntity):
|
|||||||
|
|
||||||
async def async_set_native_value(self, value: float) -> None:
|
async def async_set_native_value(self, value: float) -> None:
|
||||||
"""Set new value."""
|
"""Set new value."""
|
||||||
|
value = int(value)
|
||||||
if self.entity_description.key == "ball_speed":
|
if self.entity_description.key == "ball_speed":
|
||||||
await self.device.async_set_ball_speed(value)
|
await self.device.async_set_ball_speed(value)
|
||||||
elif self.entity_description.key == "led_speed":
|
elif self.entity_description.key == "led_speed":
|
||||||
await self.device.async_set_led(led_speed=value)
|
await self.device.async_set_led(led_speed=value)
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
import logging
|
import logging
|
||||||
from typing import Any
|
from typing import Any
|
||||||
@@ -16,6 +17,7 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
BASE_URL = "https://app.grounded.so"
|
BASE_URL = "https://app.grounded.so"
|
||||||
PLAYLISTS_REFRESH_LIMITER = timedelta(minutes=5)
|
PLAYLISTS_REFRESH_LIMITER = timedelta(minutes=5)
|
||||||
|
SOFTWARE_REFRESH_LIMITER = timedelta(hours=1)
|
||||||
|
|
||||||
|
|
||||||
class OasisCloudClient:
|
class OasisCloudClient:
|
||||||
@@ -32,15 +34,6 @@ class OasisCloudClient:
|
|||||||
* latest software metadata
|
* latest software metadata
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_session: ClientSession | None
|
|
||||||
_owns_session: bool
|
|
||||||
_access_token: str | None
|
|
||||||
|
|
||||||
# these are "cache" fields for tracks/playlists
|
|
||||||
_playlists_next_refresh: float
|
|
||||||
playlists: list[dict[str, Any]]
|
|
||||||
_playlist_details: dict[int, dict[str, str]]
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
*,
|
*,
|
||||||
@@ -51,10 +44,17 @@ class OasisCloudClient:
|
|||||||
self._owns_session = session is None
|
self._owns_session = session is None
|
||||||
self._access_token = access_token
|
self._access_token = access_token
|
||||||
|
|
||||||
# simple in-memory caches
|
# playlists cache
|
||||||
self._playlists_next_refresh = 0.0
|
self.playlists: list[dict[str, Any]] = []
|
||||||
self.playlists = []
|
self._playlists_next_refresh = now()
|
||||||
self._playlist_details = {}
|
self._playlists_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
self._playlist_details: dict[int, dict[str, str]] = {}
|
||||||
|
|
||||||
|
# software metadata cache
|
||||||
|
self._software_details: dict[str, int | str] | None = None
|
||||||
|
self._software_next_refresh = now()
|
||||||
|
self._software_lock = asyncio.Lock()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def session(self) -> ClientSession:
|
def session(self) -> ClientSession:
|
||||||
@@ -105,14 +105,31 @@ class OasisCloudClient:
|
|||||||
self, personal_only: bool = False
|
self, personal_only: bool = False
|
||||||
) -> list[dict[str, Any]]:
|
) -> list[dict[str, Any]]:
|
||||||
"""Get playlists from the cloud (cached by PLAYLISTS_REFRESH_LIMITER)."""
|
"""Get playlists from the cloud (cached by PLAYLISTS_REFRESH_LIMITER)."""
|
||||||
if self._playlists_next_refresh <= now():
|
now_dt = now()
|
||||||
|
|
||||||
|
def _is_cache_valid() -> bool:
|
||||||
|
return self._playlists_next_refresh > now_dt and bool(self.playlists)
|
||||||
|
|
||||||
|
if _is_cache_valid():
|
||||||
|
return self.playlists
|
||||||
|
|
||||||
|
async with self._playlists_lock:
|
||||||
|
# Double-check in case another task just refreshed it
|
||||||
|
now_dt = now()
|
||||||
|
if _is_cache_valid():
|
||||||
|
return self.playlists
|
||||||
|
|
||||||
params = {"my_playlists": str(personal_only).lower()}
|
params = {"my_playlists": str(personal_only).lower()}
|
||||||
playlists = await self._async_auth_request(
|
playlists = await self._async_auth_request(
|
||||||
"GET", "api/playlist", params=params
|
"GET", "api/playlist", params=params
|
||||||
)
|
)
|
||||||
if playlists:
|
|
||||||
|
if not isinstance(playlists, list):
|
||||||
|
playlists = []
|
||||||
|
|
||||||
self.playlists = playlists
|
self.playlists = playlists
|
||||||
self._playlists_next_refresh = now() + PLAYLISTS_REFRESH_LIMITER
|
self._playlists_next_refresh = now_dt + PLAYLISTS_REFRESH_LIMITER
|
||||||
|
|
||||||
return self.playlists
|
return self.playlists
|
||||||
|
|
||||||
async def async_get_track_info(self, track_id: int) -> dict[str, Any] | None:
|
async def async_get_track_info(self, track_id: int) -> dict[str, Any] | None:
|
||||||
@@ -143,9 +160,37 @@ class OasisCloudClient:
|
|||||||
track_details += response.get("data", [])
|
track_details += response.get("data", [])
|
||||||
return track_details
|
return track_details
|
||||||
|
|
||||||
async def async_get_latest_software_details(self) -> dict[str, int | str]:
|
async def async_get_latest_software_details(
|
||||||
"""Get latest software metadata from cloud."""
|
self, *, force_refresh: bool = False
|
||||||
return await self._async_auth_request("GET", "api/software/last-version")
|
) -> dict[str, int | str] | None:
|
||||||
|
"""Get latest software metadata from cloud (cached)."""
|
||||||
|
now_dt = now()
|
||||||
|
|
||||||
|
def _is_cache_valid() -> bool:
|
||||||
|
return (
|
||||||
|
not force_refresh
|
||||||
|
and self._software_details is not None
|
||||||
|
and self._software_next_refresh > now_dt
|
||||||
|
)
|
||||||
|
|
||||||
|
if _is_cache_valid():
|
||||||
|
return self._software_details
|
||||||
|
|
||||||
|
async with self._software_lock:
|
||||||
|
# Double-check in case another task just refreshed it
|
||||||
|
now_dt = now()
|
||||||
|
if _is_cache_valid():
|
||||||
|
return self._software_details
|
||||||
|
|
||||||
|
details = await self._async_auth_request("GET", "api/software/last-version")
|
||||||
|
|
||||||
|
if not isinstance(details, dict):
|
||||||
|
details = {}
|
||||||
|
|
||||||
|
self._software_details = details
|
||||||
|
self._software_next_refresh = now_dt + SOFTWARE_REFRESH_LIMITER
|
||||||
|
|
||||||
|
return self._software_details
|
||||||
|
|
||||||
async def _async_auth_request(self, method: str, url: str, **kwargs: Any) -> Any:
|
async def _async_auth_request(self, method: str, url: str, **kwargs: Any) -> Any:
|
||||||
"""Perform an authenticated cloud request."""
|
"""Perform an authenticated cloud request."""
|
||||||
|
|||||||
@@ -7,9 +7,7 @@ from typing import Any
|
|||||||
|
|
||||||
from aiohttp import ClientSession
|
from aiohttp import ClientSession
|
||||||
|
|
||||||
from ..const import AUTOPLAY_MAP
|
|
||||||
from ..device import OasisDevice
|
from ..device import OasisDevice
|
||||||
from ..utils import _bit_to_bool, _parse_int
|
|
||||||
from .transport import OasisClientProtocol
|
from .transport import OasisClientProtocol
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -179,37 +177,4 @@ class OasisHttpClient(OasisClientProtocol):
|
|||||||
return
|
return
|
||||||
|
|
||||||
_LOGGER.debug("Status for %s: %s", device.serial_number, raw_status)
|
_LOGGER.debug("Status for %s: %s", device.serial_number, raw_status)
|
||||||
|
device.update_from_status_string(raw_status)
|
||||||
values = raw_status.split(";")
|
|
||||||
if len(values) < 7:
|
|
||||||
_LOGGER.warning(
|
|
||||||
"Unexpected status format for %s: %s", device.serial_number, values
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
playlist = [_parse_int(track) for track in values[3].split(",") if track]
|
|
||||||
shift = len(values) - 18 if len(values) > 17 else 0
|
|
||||||
|
|
||||||
try:
|
|
||||||
status: dict[str, Any] = {
|
|
||||||
"status_code": _parse_int(values[0]),
|
|
||||||
"error": _parse_int(values[1]),
|
|
||||||
"ball_speed": _parse_int(values[2]),
|
|
||||||
"playlist": playlist,
|
|
||||||
"playlist_index": min(_parse_int(values[4]), len(playlist)),
|
|
||||||
"progress": _parse_int(values[5]),
|
|
||||||
"led_effect": values[6],
|
|
||||||
"led_speed": _parse_int(values[8]),
|
|
||||||
"brightness": _parse_int(values[9]),
|
|
||||||
"color": values[10] if "#" in values[10] else None,
|
|
||||||
"busy": _bit_to_bool(values[11 + shift]),
|
|
||||||
"download_progress": _parse_int(values[12 + shift]),
|
|
||||||
"max_brightness": _parse_int(values[13 + shift]),
|
|
||||||
"repeat_playlist": _bit_to_bool(values[15 + shift]),
|
|
||||||
"autoplay": AUTOPLAY_MAP.get(value := values[16 + shift], value),
|
|
||||||
}
|
|
||||||
except Exception: # noqa: BLE001
|
|
||||||
_LOGGER.exception("Error parsing HTTP status for %s", device.serial_number)
|
|
||||||
return
|
|
||||||
|
|
||||||
device.update_from_status_dict(status)
|
|
||||||
|
|||||||
@@ -7,13 +7,12 @@ import base64
|
|||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
import logging
|
import logging
|
||||||
import ssl
|
import ssl
|
||||||
from typing import Any, Final
|
from typing import Any, Final, Iterable
|
||||||
|
|
||||||
import aiomqtt
|
import aiomqtt
|
||||||
|
|
||||||
from ..const import AUTOPLAY_MAP
|
|
||||||
from ..device import OasisDevice
|
from ..device import OasisDevice
|
||||||
from ..utils import _bit_to_bool
|
from ..utils import _bit_to_bool, _parse_int
|
||||||
from .transport import OasisClientProtocol
|
from .transport import OasisClientProtocol
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@@ -26,6 +25,9 @@ USERNAME: Final = "YXBw"
|
|||||||
PASSWORD: Final = "RWdETFlKMDczfi4t"
|
PASSWORD: Final = "RWdETFlKMDczfi4t"
|
||||||
RECONNECT_INTERVAL: Final = 4
|
RECONNECT_INTERVAL: Final = 4
|
||||||
|
|
||||||
|
# Command queue behaviour
|
||||||
|
MAX_PENDING_COMMANDS: Final = 10
|
||||||
|
|
||||||
|
|
||||||
class OasisMqttClient(OasisClientProtocol):
|
class OasisMqttClient(OasisClientProtocol):
|
||||||
"""MQTT-based Oasis transport using WSS.
|
"""MQTT-based Oasis transport using WSS.
|
||||||
@@ -58,6 +60,11 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
self._subscribed_serials: set[str] = set()
|
self._subscribed_serials: set[str] = set()
|
||||||
self._subscription_lock = asyncio.Lock()
|
self._subscription_lock = asyncio.Lock()
|
||||||
|
|
||||||
|
# Pending command queue: (serial, payload)
|
||||||
|
self._command_queue: asyncio.Queue[tuple[str, str]] = asyncio.Queue(
|
||||||
|
maxsize=MAX_PENDING_COMMANDS
|
||||||
|
)
|
||||||
|
|
||||||
def register_device(self, device: OasisDevice) -> None:
|
def register_device(self, device: OasisDevice) -> None:
|
||||||
"""Register a device so MQTT messages can be routed to it."""
|
"""Register a device so MQTT messages can be routed to it."""
|
||||||
if not device.serial_number:
|
if not device.serial_number:
|
||||||
@@ -70,6 +77,10 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
self._first_status_events.setdefault(serial, asyncio.Event())
|
self._first_status_events.setdefault(serial, asyncio.Event())
|
||||||
self._mac_events.setdefault(serial, asyncio.Event())
|
self._mac_events.setdefault(serial, asyncio.Event())
|
||||||
|
|
||||||
|
# Attach ourselves as the client if the device doesn't already have one
|
||||||
|
if not device.client:
|
||||||
|
device.attach_client(self)
|
||||||
|
|
||||||
# If we're already connected, subscribe to this device's topics
|
# If we're already connected, subscribe to this device's topics
|
||||||
if self._client is not None:
|
if self._client is not None:
|
||||||
try:
|
try:
|
||||||
@@ -81,8 +92,10 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
"Could not schedule subscription for %s (no running loop)", serial
|
"Could not schedule subscription for %s (no running loop)", serial
|
||||||
)
|
)
|
||||||
|
|
||||||
if not device.client:
|
def register_devices(self, devices: Iterable[OasisDevice]) -> None:
|
||||||
device.attach_client(self)
|
"""Convenience method to register multiple devices."""
|
||||||
|
for device in devices:
|
||||||
|
self.register_device(device)
|
||||||
|
|
||||||
def unregister_device(self, device: OasisDevice) -> None:
|
def unregister_device(self, device: OasisDevice) -> None:
|
||||||
serial = device.serial_number
|
serial = device.serial_number
|
||||||
@@ -135,8 +148,9 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
async def _resubscribe_all(self) -> None:
|
async def _resubscribe_all(self) -> None:
|
||||||
"""Resubscribe to all known devices after (re)connect."""
|
"""Resubscribe to all known devices after (re)connect."""
|
||||||
self._subscribed_serials.clear()
|
self._subscribed_serials.clear()
|
||||||
for serial in list(self._devices):
|
for serial, device in self._devices.items():
|
||||||
await self._subscribe_serial(serial)
|
await self._subscribe_serial(serial)
|
||||||
|
await self.async_get_all(device)
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
"""Start MQTT connection loop."""
|
"""Start MQTT connection loop."""
|
||||||
@@ -168,6 +182,14 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
finally:
|
finally:
|
||||||
self._client = None
|
self._client = None
|
||||||
|
|
||||||
|
# Drop pending commands on stop
|
||||||
|
while not self._command_queue.empty():
|
||||||
|
try:
|
||||||
|
self._command_queue.get_nowait()
|
||||||
|
self._command_queue.task_done()
|
||||||
|
except asyncio.QueueEmpty:
|
||||||
|
break
|
||||||
|
|
||||||
async def wait_until_ready(
|
async def wait_until_ready(
|
||||||
self, device: OasisDevice, timeout: float = 10.0, request_status: bool = True
|
self, device: OasisDevice, timeout: float = 10.0, request_status: bool = True
|
||||||
) -> bool:
|
) -> bool:
|
||||||
@@ -243,6 +265,13 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
|
|
||||||
return device.mac_address
|
return device.mac_address
|
||||||
|
|
||||||
|
async def async_send_auto_clean_command(
|
||||||
|
self, device: OasisDevice, auto_clean: bool
|
||||||
|
) -> None:
|
||||||
|
"""Send auto clean command."""
|
||||||
|
payload = f"WRIAUTOCLEAN={1 if auto_clean else 0}"
|
||||||
|
await self._publish_command(device, payload)
|
||||||
|
|
||||||
async def async_send_ball_speed_command(
|
async def async_send_ball_speed_command(
|
||||||
self,
|
self,
|
||||||
device: OasisDevice,
|
device: OasisDevice,
|
||||||
@@ -260,7 +289,7 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
brightness: int,
|
brightness: int,
|
||||||
) -> None:
|
) -> None:
|
||||||
payload = f"WRILED={led_effect};0;{color};{led_speed};{brightness}"
|
payload = f"WRILED={led_effect};0;{color};{led_speed};{brightness}"
|
||||||
await self._publish_command(device, payload)
|
await self._publish_command(device, payload, bool(brightness))
|
||||||
|
|
||||||
async def async_send_sleep_command(self, device: OasisDevice) -> None:
|
async def async_send_sleep_command(self, device: OasisDevice) -> None:
|
||||||
await self._publish_command(device, "CMDSLEEP")
|
await self._publish_command(device, "CMDSLEEP")
|
||||||
@@ -300,9 +329,6 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
payload = f"WRIJOBLIST={track_str}"
|
payload = f"WRIJOBLIST={track_str}"
|
||||||
await self._publish_command(device, payload)
|
await self._publish_command(device, payload)
|
||||||
|
|
||||||
# local state optimistic update
|
|
||||||
device.update_from_status_dict({"playlist": playlist})
|
|
||||||
|
|
||||||
async def async_send_set_repeat_playlist_command(
|
async def async_send_set_repeat_playlist_command(
|
||||||
self,
|
self,
|
||||||
device: OasisDevice,
|
device: OasisDevice,
|
||||||
@@ -328,7 +354,7 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
await self._publish_command(device, payload)
|
await self._publish_command(device, payload)
|
||||||
|
|
||||||
async def async_send_play_command(self, device: OasisDevice) -> None:
|
async def async_send_play_command(self, device: OasisDevice) -> None:
|
||||||
await self._publish_command(device, "CMDPLAY")
|
await self._publish_command(device, "CMDPLAY", True)
|
||||||
|
|
||||||
async def async_send_pause_command(self, device: OasisDevice) -> None:
|
async def async_send_pause_command(self, device: OasisDevice) -> None:
|
||||||
await self._publish_command(device, "CMDPAUSE")
|
await self._publish_command(device, "CMDPAUSE")
|
||||||
@@ -339,21 +365,96 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
async def async_send_reboot_command(self, device: OasisDevice) -> None:
|
async def async_send_reboot_command(self, device: OasisDevice) -> None:
|
||||||
await self._publish_command(device, "CMDBOOT")
|
await self._publish_command(device, "CMDBOOT")
|
||||||
|
|
||||||
|
async def async_get_all(self, device: OasisDevice) -> None:
|
||||||
|
"""Request FULLSTATUS + SCHEDULE (compact snapshot)."""
|
||||||
|
await self._publish_command(device, "GETALL")
|
||||||
|
|
||||||
async def async_get_status(self, device: OasisDevice) -> None:
|
async def async_get_status(self, device: OasisDevice) -> None:
|
||||||
"""Ask device to publish STATUS topics."""
|
"""Ask device to publish STATUS topics."""
|
||||||
await self._publish_command(device, "GETSTATUS")
|
await self._publish_command(device, "GETSTATUS")
|
||||||
|
|
||||||
async def _publish_command(self, device: OasisDevice, payload: str) -> None:
|
async def _enqueue_command(self, serial: str, payload: str) -> None:
|
||||||
if not self._client:
|
"""Queue a command to be sent when connected.
|
||||||
raise RuntimeError("MQTT client not connected yet")
|
|
||||||
|
|
||||||
serial = device.serial_number
|
If the queue is full, drop the oldest command to make room.
|
||||||
if not serial:
|
"""
|
||||||
raise RuntimeError("Device has no serial_number set")
|
if self._command_queue.full():
|
||||||
|
try:
|
||||||
|
dropped = self._command_queue.get_nowait()
|
||||||
|
self._command_queue.task_done()
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Command queue full, dropping oldest command: %s", dropped
|
||||||
|
)
|
||||||
|
except asyncio.QueueEmpty:
|
||||||
|
# race: became empty between full() and get_nowait()
|
||||||
|
pass
|
||||||
|
|
||||||
|
await self._command_queue.put((serial, payload))
|
||||||
|
_LOGGER.debug("Queued command for %s: %s", serial, payload)
|
||||||
|
|
||||||
|
async def _flush_pending_commands(self) -> None:
|
||||||
|
"""Send any queued commands now that we're connected."""
|
||||||
|
if not self._client:
|
||||||
|
return
|
||||||
|
|
||||||
|
while not self._command_queue.empty():
|
||||||
|
try:
|
||||||
|
serial, payload = self._command_queue.get_nowait()
|
||||||
|
except asyncio.QueueEmpty:
|
||||||
|
break
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Skip commands for unknown devices
|
||||||
|
if serial not in self._devices:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Skipping queued command for unknown device %s: %s",
|
||||||
|
serial,
|
||||||
|
payload,
|
||||||
|
)
|
||||||
|
self._command_queue.task_done()
|
||||||
|
continue
|
||||||
|
|
||||||
topic = f"{serial}/COMMAND/CMD"
|
topic = f"{serial}/COMMAND/CMD"
|
||||||
|
_LOGGER.debug("Flushing queued MQTT command %s => %s", topic, payload)
|
||||||
|
await self._client.publish(topic, payload.encode(), qos=1)
|
||||||
|
except Exception:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"Failed to flush queued command for %s, re-queuing", serial
|
||||||
|
)
|
||||||
|
# Put it back and break; we'll try again on next reconnect
|
||||||
|
await self._enqueue_command(serial, payload)
|
||||||
|
self._command_queue.task_done()
|
||||||
|
break
|
||||||
|
|
||||||
|
self._command_queue.task_done()
|
||||||
|
|
||||||
|
async def _publish_command(
|
||||||
|
self, device: OasisDevice, payload: str, wake: bool = False
|
||||||
|
) -> None:
|
||||||
|
serial = device.serial_number
|
||||||
|
if not serial:
|
||||||
|
raise RuntimeError("Device has no serial number set")
|
||||||
|
|
||||||
|
if wake and device.is_sleeping:
|
||||||
|
await self.async_get_all(device)
|
||||||
|
|
||||||
|
# If not connected, just queue the command
|
||||||
|
if not self._client or not self._connected_event.is_set():
|
||||||
|
_LOGGER.debug(
|
||||||
|
"MQTT not connected, queueing command for %s: %s", serial, payload
|
||||||
|
)
|
||||||
|
await self._enqueue_command(serial, payload)
|
||||||
|
return
|
||||||
|
|
||||||
|
topic = f"{serial}/COMMAND/CMD"
|
||||||
|
try:
|
||||||
_LOGGER.debug("MQTT publish %s => %s", topic, payload)
|
_LOGGER.debug("MQTT publish %s => %s", topic, payload)
|
||||||
await self._client.publish(topic, payload.encode(), qos=1)
|
await self._client.publish(topic, payload.encode(), qos=1)
|
||||||
|
except Exception:
|
||||||
|
_LOGGER.debug(
|
||||||
|
"MQTT publish failed, queueing command for %s: %s", serial, payload
|
||||||
|
)
|
||||||
|
await self._enqueue_command(serial, payload)
|
||||||
|
|
||||||
async def _mqtt_loop(self) -> None:
|
async def _mqtt_loop(self) -> None:
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
@@ -361,12 +462,7 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
|
|
||||||
while not self._stop_event.is_set():
|
while not self._stop_event.is_set():
|
||||||
try:
|
try:
|
||||||
_LOGGER.debug(
|
_LOGGER.info("Connecting MQTT WSS to wss://%s:%s/%s", HOST, PORT, PATH)
|
||||||
"Connecting MQTT WSS to wss://%s:%s/%s",
|
|
||||||
HOST,
|
|
||||||
PORT,
|
|
||||||
PATH,
|
|
||||||
)
|
|
||||||
|
|
||||||
async with aiomqtt.Client(
|
async with aiomqtt.Client(
|
||||||
hostname=HOST,
|
hostname=HOST,
|
||||||
@@ -386,6 +482,9 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
# Subscribe only to STATUS topics for known devices
|
# Subscribe only to STATUS topics for known devices
|
||||||
await self._resubscribe_all()
|
await self._resubscribe_all()
|
||||||
|
|
||||||
|
# Flush any queued commands now that we're connected
|
||||||
|
await self._flush_pending_commands()
|
||||||
|
|
||||||
async for msg in client.messages:
|
async for msg in client.messages:
|
||||||
if self._stop_event.is_set():
|
if self._stop_event.is_set():
|
||||||
break
|
break
|
||||||
@@ -394,13 +493,13 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
break
|
break
|
||||||
except Exception:
|
except Exception:
|
||||||
_LOGGER.debug("MQTT connection error")
|
_LOGGER.info("MQTT connection error")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if self._connected_event.is_set():
|
if self._connected_event.is_set():
|
||||||
self._connected_event.clear()
|
self._connected_event.clear()
|
||||||
if self._connected_at:
|
if self._connected_at:
|
||||||
_LOGGER.debug(
|
_LOGGER.info(
|
||||||
"MQTT was connected for %s",
|
"MQTT was connected for %s",
|
||||||
datetime.now(UTC) - self._connected_at,
|
datetime.now(UTC) - self._connected_at,
|
||||||
)
|
)
|
||||||
@@ -409,14 +508,13 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
self._subscribed_serials.clear()
|
self._subscribed_serials.clear()
|
||||||
|
|
||||||
if not self._stop_event.is_set():
|
if not self._stop_event.is_set():
|
||||||
_LOGGER.debug(
|
_LOGGER.info(
|
||||||
"Disconnected from broker, retrying in %.1fs", RECONNECT_INTERVAL
|
"Disconnected from broker, retrying in %.1fs", RECONNECT_INTERVAL
|
||||||
)
|
)
|
||||||
await asyncio.sleep(RECONNECT_INTERVAL)
|
await asyncio.sleep(RECONNECT_INTERVAL)
|
||||||
|
|
||||||
async def _handle_status_message(self, msg: aiomqtt.Message) -> None:
|
async def _handle_status_message(self, msg: aiomqtt.Message) -> None:
|
||||||
"""Map MQTT STATUS topics → OasisDevice.update_from_status_dict payloads."""
|
"""Map MQTT STATUS topics → OasisDevice.update_from_status_dict payloads."""
|
||||||
|
|
||||||
topic_str = str(msg.topic) if msg.topic is not None else ""
|
topic_str = str(msg.topic) if msg.topic is not None else ""
|
||||||
payload = msg.payload.decode(errors="replace")
|
payload = msg.payload.decode(errors="replace")
|
||||||
|
|
||||||
@@ -429,7 +527,6 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
|
|
||||||
device = self._devices.get(serial)
|
device = self._devices.get(serial)
|
||||||
if not device:
|
if not device:
|
||||||
# Ignore devices we don't know about
|
|
||||||
_LOGGER.debug("Received MQTT for unknown device %s: %s", serial, topic_str)
|
_LOGGER.debug("Received MQTT for unknown device %s: %s", serial, topic_str)
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -451,13 +548,13 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
elif status_name == "LED_EFFECT":
|
elif status_name == "LED_EFFECT":
|
||||||
data["led_effect"] = payload
|
data["led_effect"] = payload
|
||||||
elif status_name == "LED_EFFECT_COLOR":
|
elif status_name == "LED_EFFECT_COLOR":
|
||||||
data["led_effect_color"] = payload
|
data["led_color_id"] = payload
|
||||||
elif status_name == "LED_SPEED":
|
elif status_name == "LED_SPEED":
|
||||||
data["led_speed"] = int(payload)
|
data["led_speed"] = int(payload)
|
||||||
elif status_name == "LED_BRIGHTNESS":
|
elif status_name == "LED_BRIGHTNESS":
|
||||||
data["brightness"] = int(payload)
|
data["brightness"] = int(payload)
|
||||||
elif status_name == "LED_MAX":
|
elif status_name == "LED_MAX":
|
||||||
data["max_brightness"] = int(payload)
|
data["brightness_max"] = int(payload)
|
||||||
elif status_name == "LED_EFFECT_PARAM":
|
elif status_name == "LED_EFFECT_PARAM":
|
||||||
data["color"] = payload if payload.startswith("#") else None
|
data["color"] = payload if payload.startswith("#") else None
|
||||||
elif status_name == "SYSTEM_BUSY":
|
elif status_name == "SYSTEM_BUSY":
|
||||||
@@ -467,7 +564,7 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
elif status_name == "REPEAT_JOB":
|
elif status_name == "REPEAT_JOB":
|
||||||
data["repeat_playlist"] = payload in ("1", "true", "True")
|
data["repeat_playlist"] = payload in ("1", "true", "True")
|
||||||
elif status_name == "WAIT_AFTER_JOB":
|
elif status_name == "WAIT_AFTER_JOB":
|
||||||
data["autoplay"] = AUTOPLAY_MAP.get(payload, payload)
|
data["autoplay"] = _parse_int(payload)
|
||||||
elif status_name == "AUTO_CLEAN":
|
elif status_name == "AUTO_CLEAN":
|
||||||
data["auto_clean"] = payload in ("1", "true", "True")
|
data["auto_clean"] = payload in ("1", "true", "True")
|
||||||
elif status_name == "SOFTWARE_VER":
|
elif status_name == "SOFTWARE_VER":
|
||||||
@@ -494,6 +591,9 @@ class OasisMqttClient(OasisClientProtocol):
|
|||||||
data["schedule"] = payload
|
data["schedule"] = payload
|
||||||
elif status_name == "ENVIRONMENT":
|
elif status_name == "ENVIRONMENT":
|
||||||
data["environment"] = payload
|
data["environment"] = payload
|
||||||
|
elif status_name == "FULLSTATUS":
|
||||||
|
if parsed := device.parse_status_string(payload):
|
||||||
|
data = parsed
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Unknown status received for %s: %s=%s",
|
"Unknown status received for %s: %s=%s",
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ class OasisClientProtocol(Protocol):
|
|||||||
|
|
||||||
async def async_get_mac_address(self, device: OasisDevice) -> str | None: ...
|
async def async_get_mac_address(self, device: OasisDevice) -> str | None: ...
|
||||||
|
|
||||||
|
async def async_send_auto_clean_command(
|
||||||
|
self, device: OasisDevice, auto_clean: bool
|
||||||
|
) -> None: ...
|
||||||
|
|
||||||
async def async_send_ball_speed_command(
|
async def async_send_ball_speed_command(
|
||||||
self,
|
self,
|
||||||
device: OasisDevice,
|
device: OasisDevice,
|
||||||
@@ -83,3 +87,7 @@ class OasisClientProtocol(Protocol):
|
|||||||
async def async_send_stop_command(self, device: OasisDevice) -> None: ...
|
async def async_send_stop_command(self, device: OasisDevice) -> None: ...
|
||||||
|
|
||||||
async def async_send_reboot_command(self, device: OasisDevice) -> None: ...
|
async def async_send_reboot_command(self, device: OasisDevice) -> None: ...
|
||||||
|
|
||||||
|
async def async_get_all(self, device: OasisDevice) -> None: ...
|
||||||
|
|
||||||
|
async def async_get_status(self, device: OasisDevice) -> None: ...
|
||||||
|
|||||||
@@ -21,6 +21,9 @@ AUTOPLAY_MAP: Final[dict[str, str]] = {
|
|||||||
"2": "5 minutes",
|
"2": "5 minutes",
|
||||||
"3": "10 minutes",
|
"3": "10 minutes",
|
||||||
"4": "30 minutes",
|
"4": "30 minutes",
|
||||||
|
"6": "1 hour",
|
||||||
|
"7": "6 hours",
|
||||||
|
"8": "12 hours",
|
||||||
"5": "24 hours",
|
"5": "24 hours",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,13 +94,14 @@ LED_EFFECTS: Final[dict[str, str]] = {
|
|||||||
"41": "Color Comets",
|
"41": "Color Comets",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
STATUS_CODE_SLEEPING: Final = 6
|
||||||
STATUS_CODE_MAP: Final[dict[int, str]] = {
|
STATUS_CODE_MAP: Final[dict[int, str]] = {
|
||||||
0: "booting",
|
0: "booting",
|
||||||
2: "stopped",
|
2: "stopped",
|
||||||
3: "centering",
|
3: "centering",
|
||||||
4: "playing",
|
4: "playing",
|
||||||
5: "paused",
|
5: "paused",
|
||||||
6: "sleeping",
|
STATUS_CODE_SLEEPING: "sleeping",
|
||||||
9: "error",
|
9: "error",
|
||||||
11: "updating",
|
11: "updating",
|
||||||
13: "downloading",
|
13: "downloading",
|
||||||
|
|||||||
@@ -2,22 +2,33 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
from typing import TYPE_CHECKING, Any, Callable, Final, Iterable
|
from typing import TYPE_CHECKING, Any, Callable, Final, Iterable
|
||||||
|
|
||||||
from .const import ERROR_CODE_MAP, LED_EFFECTS, STATUS_CODE_MAP, TRACKS
|
from .const import (
|
||||||
|
ERROR_CODE_MAP,
|
||||||
|
LED_EFFECTS,
|
||||||
|
STATUS_CODE_MAP,
|
||||||
|
STATUS_CODE_SLEEPING,
|
||||||
|
TRACKS,
|
||||||
|
)
|
||||||
|
from .utils import _bit_to_bool, _parse_int, create_svg, decrypt_svg_content
|
||||||
|
|
||||||
if TYPE_CHECKING: # avoid runtime circular imports
|
if TYPE_CHECKING: # avoid runtime circular imports
|
||||||
|
from .clients import OasisCloudClient
|
||||||
from .clients.transport import OasisClientProtocol
|
from .clients.transport import OasisClientProtocol
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
BALL_SPEED_MAX: Final = 400
|
BALL_SPEED_MAX: Final = 400
|
||||||
BALL_SPEED_MIN: Final = 100
|
BALL_SPEED_MIN: Final = 100
|
||||||
|
BRIGHTNESS_DEFAULT: Final = 100
|
||||||
LED_SPEED_MAX: Final = 90
|
LED_SPEED_MAX: Final = 90
|
||||||
LED_SPEED_MIN: Final = -90
|
LED_SPEED_MIN: Final = -90
|
||||||
|
|
||||||
_STATE_FIELDS = (
|
_STATE_FIELDS = (
|
||||||
|
"auto_clean",
|
||||||
"autoplay",
|
"autoplay",
|
||||||
"ball_speed",
|
"ball_speed",
|
||||||
"brightness",
|
"brightness",
|
||||||
@@ -28,7 +39,6 @@ _STATE_FIELDS = (
|
|||||||
"led_effect",
|
"led_effect",
|
||||||
"led_speed",
|
"led_speed",
|
||||||
"mac_address",
|
"mac_address",
|
||||||
"max_brightness",
|
|
||||||
"playlist",
|
"playlist",
|
||||||
"playlist_index",
|
"playlist_index",
|
||||||
"progress",
|
"progress",
|
||||||
@@ -53,33 +63,39 @@ class OasisDevice:
|
|||||||
*,
|
*,
|
||||||
model: str | None = None,
|
model: str | None = None,
|
||||||
serial_number: str | None = None,
|
serial_number: str | None = None,
|
||||||
|
name: str | None = None,
|
||||||
ssid: str | None = None,
|
ssid: str | None = None,
|
||||||
ip_address: str | None = None,
|
ip_address: str | None = None,
|
||||||
|
cloud: OasisCloudClient | None = None,
|
||||||
client: OasisClientProtocol | None = None,
|
client: OasisClientProtocol | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
# Transport
|
# Transport
|
||||||
self._client: OasisClientProtocol | None = client
|
self._cloud = cloud
|
||||||
|
self._client = client
|
||||||
self._listeners: list[Callable[[], None]] = []
|
self._listeners: list[Callable[[], None]] = []
|
||||||
|
|
||||||
# Details
|
# Details
|
||||||
self.model: str | None = model
|
self.model = model
|
||||||
self.serial_number: str | None = serial_number
|
self.serial_number = serial_number
|
||||||
self.ssid: str | None = ssid
|
self.name = name if name else f"{model} {serial_number}"
|
||||||
self.ip_address: str | None = ip_address
|
self.ssid = ssid
|
||||||
|
self.ip_address = ip_address
|
||||||
|
|
||||||
# Status
|
# Status
|
||||||
self.auto_clean: bool = False
|
self.auto_clean: bool = False
|
||||||
self.autoplay: str = "off"
|
self.autoplay: int = 0
|
||||||
self.ball_speed: int = BALL_SPEED_MIN
|
self.ball_speed: int = BALL_SPEED_MIN
|
||||||
self.brightness: int = 0
|
self._brightness: int = 0
|
||||||
|
self.brightness_max: int = 200
|
||||||
|
self.brightness_on: int = BRIGHTNESS_DEFAULT
|
||||||
self.busy: bool = False
|
self.busy: bool = False
|
||||||
self.color: str | None = None
|
self.color: str | None = None
|
||||||
self.download_progress: int = 0
|
self.download_progress: int = 0
|
||||||
self.error: int = 0
|
self.error: int = 0
|
||||||
|
self.led_color_id: str = "0"
|
||||||
self.led_effect: str = "0"
|
self.led_effect: str = "0"
|
||||||
self.led_speed: int = 0
|
self.led_speed: int = 0
|
||||||
self.mac_address: str | None = None
|
self.mac_address: str | None = None
|
||||||
self.max_brightness: int = 200
|
|
||||||
self.playlist: list[int] = []
|
self.playlist: list[int] = []
|
||||||
self.playlist_index: int = 0
|
self.playlist_index: int = 0
|
||||||
self.progress: int = 0
|
self.progress: int = 0
|
||||||
@@ -96,8 +112,25 @@ class OasisDevice:
|
|||||||
self.environment: str | None = None
|
self.environment: str | None = None
|
||||||
self.schedule: Any | None = None
|
self.schedule: Any | None = None
|
||||||
|
|
||||||
# Track metadata cache (used if you hydrate from cloud)
|
# Track metadata cache
|
||||||
self._track: dict | None = None
|
self._track: dict | None = None
|
||||||
|
self._track_task: asyncio.Task | None = None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def brightness(self) -> int:
|
||||||
|
"""Return the brightness."""
|
||||||
|
return 0 if self.is_sleeping else self._brightness
|
||||||
|
|
||||||
|
@brightness.setter
|
||||||
|
def brightness(self, value: int) -> None:
|
||||||
|
self._brightness = value
|
||||||
|
if value:
|
||||||
|
self.brightness_on = value
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_sleeping(self) -> bool:
|
||||||
|
"""Return `True` if the status is set to sleeping."""
|
||||||
|
return self.status_code == STATUS_CODE_SLEEPING
|
||||||
|
|
||||||
def attach_client(self, client: OasisClientProtocol) -> None:
|
def attach_client(self, client: OasisClientProtocol) -> None:
|
||||||
"""Attach a transport client (MQTT, HTTP, etc.) to this device."""
|
"""Attach a transport client (MQTT, HTTP, etc.) to this device."""
|
||||||
@@ -120,7 +153,8 @@ class OasisDevice:
|
|||||||
old = getattr(self, name, None)
|
old = getattr(self, name, None)
|
||||||
if old != value:
|
if old != value:
|
||||||
_LOGGER.debug(
|
_LOGGER.debug(
|
||||||
"%s changed: '%s' -> '%s'",
|
"%s %s changed: '%s' -> '%s'",
|
||||||
|
self.serial_number,
|
||||||
name.replace("_", " ").capitalize(),
|
name.replace("_", " ").capitalize(),
|
||||||
old,
|
old,
|
||||||
value,
|
value,
|
||||||
@@ -132,16 +166,83 @@ class OasisDevice:
|
|||||||
def update_from_status_dict(self, data: dict[str, Any]) -> None:
|
def update_from_status_dict(self, data: dict[str, Any]) -> None:
|
||||||
"""Update device fields from a status payload (from any transport)."""
|
"""Update device fields from a status payload (from any transport)."""
|
||||||
changed = False
|
changed = False
|
||||||
|
playlist_or_index_changed = False
|
||||||
|
|
||||||
for key, value in data.items():
|
for key, value in data.items():
|
||||||
if hasattr(self, key):
|
if hasattr(self, key):
|
||||||
if self._update_field(key, value):
|
if self._update_field(key, value):
|
||||||
changed = True
|
changed = True
|
||||||
|
if key in ("playlist", "playlist_index"):
|
||||||
|
playlist_or_index_changed = True
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("Unknown field: %s=%s", key, value)
|
_LOGGER.warning("Unknown field: %s=%s", key, value)
|
||||||
|
|
||||||
|
if playlist_or_index_changed:
|
||||||
|
self.schedule_track_refresh()
|
||||||
|
|
||||||
if changed:
|
if changed:
|
||||||
self._notify_listeners()
|
self._notify_listeners()
|
||||||
|
|
||||||
|
def parse_status_string(self, raw_status: str) -> dict[str, Any] | None:
|
||||||
|
"""Parse a semicolon-separated status string into a state dict.
|
||||||
|
|
||||||
|
Used by:
|
||||||
|
- HTTP GETSTATUS response
|
||||||
|
- MQTT FULLSTATUS payload (includes software_version)
|
||||||
|
"""
|
||||||
|
if not raw_status:
|
||||||
|
return None
|
||||||
|
|
||||||
|
values = raw_status.split(";")
|
||||||
|
|
||||||
|
# We rely on indices 0..17 existing (18 fields)
|
||||||
|
if (n := len(values)) < 18:
|
||||||
|
_LOGGER.warning(
|
||||||
|
"Unexpected status format for %s: %s", self.serial_number, values
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
playlist = [_parse_int(track) for track in values[3].split(",") if track]
|
||||||
|
|
||||||
|
try:
|
||||||
|
status: dict[str, Any] = {
|
||||||
|
"status_code": _parse_int(values[0]),
|
||||||
|
"error": _parse_int(values[1]),
|
||||||
|
"ball_speed": _parse_int(values[2]),
|
||||||
|
"playlist": playlist,
|
||||||
|
"playlist_index": min(_parse_int(values[4]), len(playlist)),
|
||||||
|
"progress": _parse_int(values[5]),
|
||||||
|
"led_effect": values[6],
|
||||||
|
"led_color_id": values[7],
|
||||||
|
"led_speed": _parse_int(values[8]),
|
||||||
|
"brightness": _parse_int(values[9]),
|
||||||
|
"color": values[10] if "#" in values[10] else None,
|
||||||
|
"busy": _bit_to_bool(values[11]),
|
||||||
|
"download_progress": _parse_int(values[12]),
|
||||||
|
"brightness_max": _parse_int(values[13]),
|
||||||
|
"wifi_connected": _bit_to_bool(values[14]),
|
||||||
|
"repeat_playlist": _bit_to_bool(values[15]),
|
||||||
|
"autoplay": _parse_int(values[16]),
|
||||||
|
"auto_clean": _bit_to_bool(values[17]),
|
||||||
|
}
|
||||||
|
|
||||||
|
# Optional trailing field(s)
|
||||||
|
if n > 18:
|
||||||
|
status["software_version"] = values[18]
|
||||||
|
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
_LOGGER.exception(
|
||||||
|
"Error parsing status string for %s: %r", self.serial_number, raw_status
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
|
return status
|
||||||
|
|
||||||
|
def update_from_status_string(self, raw_status: str) -> None:
|
||||||
|
"""Parse and apply a raw status string."""
|
||||||
|
if status := self.parse_status_string(raw_status):
|
||||||
|
self.update_from_status_dict(status)
|
||||||
|
|
||||||
def as_dict(self) -> dict[str, Any]:
|
def as_dict(self) -> dict[str, Any]:
|
||||||
"""Return core state as a dict."""
|
"""Return core state as a dict."""
|
||||||
return {field: getattr(self, field) for field in _STATE_FIELDS}
|
return {field: getattr(self, field) for field in _STATE_FIELDS}
|
||||||
@@ -158,6 +259,13 @@ class OasisDevice:
|
|||||||
"""Return human-readable status from status_code."""
|
"""Return human-readable status from status_code."""
|
||||||
return STATUS_CODE_MAP.get(self.status_code, f"Unknown ({self.status_code})")
|
return STATUS_CODE_MAP.get(self.status_code, f"Unknown ({self.status_code})")
|
||||||
|
|
||||||
|
@property
|
||||||
|
def track(self) -> dict | None:
|
||||||
|
"""Return cached track info if it matches the current `track_id`."""
|
||||||
|
if (track := self._track) and track["id"] == self.track_id:
|
||||||
|
return track
|
||||||
|
return TRACKS.get(self.track_id)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def track_id(self) -> int | None:
|
def track_id(self) -> int | None:
|
||||||
if not self.playlist:
|
if not self.playlist:
|
||||||
@@ -166,37 +274,45 @@ class OasisDevice:
|
|||||||
return self.playlist[0] if i >= len(self.playlist) else self.playlist[i]
|
return self.playlist[0] if i >= len(self.playlist) else self.playlist[i]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def track(self) -> dict | None:
|
def track_image_url(self) -> str | None:
|
||||||
"""Return cached track info if it matches the current `track_id`."""
|
"""Return the track image url, if any."""
|
||||||
if self._track and self._track.get("id") == self.track_id:
|
if (track := self.track) and (image := track.get("image")):
|
||||||
return self._track
|
return f"https://app.grounded.so/uploads/{image}"
|
||||||
if track := TRACKS.get(self.track_id):
|
return None
|
||||||
self._track = track
|
|
||||||
return self._track
|
@property
|
||||||
|
def track_name(self) -> str | None:
|
||||||
|
"""Return the track name, if any."""
|
||||||
|
if track := self.track:
|
||||||
|
return track.get("name", f"Unknown Title (#{self.track_id})")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def drawing_progress(self) -> float | None:
|
def drawing_progress(self) -> float | None:
|
||||||
"""Return drawing progress percentage for the current track."""
|
"""Return drawing progress percentage for the current track."""
|
||||||
# if not (self.track and (svg_content := self.track.get("svg_content"))):
|
if not (self.track and (svg_content := self.track.get("svg_content"))):
|
||||||
# return None
|
return None
|
||||||
# svg_content = decrypt_svg_content(svg_content)
|
svg_content = decrypt_svg_content(svg_content)
|
||||||
# paths = svg_content.split("L")
|
paths = svg_content.split("L")
|
||||||
total = self.track.get("reduced_svg_content_new", 0) # or len(paths)
|
total = self.track.get("reduced_svg_content_new", 0) or len(paths)
|
||||||
percent = (100 * self.progress) / total
|
percent = (100 * self.progress) / total
|
||||||
return percent
|
return min(percent, 100)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def playlist_details(self) -> dict[int, dict[str, str]]:
|
def playlist_details(self) -> dict[int, dict[str, str]]:
|
||||||
"""Basic playlist details using built-in TRACKS metadata."""
|
"""Basic playlist details using built-in TRACKS metadata."""
|
||||||
return {
|
return {
|
||||||
track_id: TRACKS.get(
|
track_id: {self.track_id: self.track or {}, **TRACKS}.get(
|
||||||
track_id,
|
track_id,
|
||||||
{"name": f"Unknown Title (#{track_id})"},
|
{"name": f"Unknown Title (#{track_id})"},
|
||||||
)
|
)
|
||||||
for track_id in self.playlist
|
for track_id in self.playlist
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def create_svg(self) -> str | None:
|
||||||
|
"""Create the current svg based on track and progress."""
|
||||||
|
return create_svg(self.track, self.progress)
|
||||||
|
|
||||||
def add_update_listener(self, listener: Callable[[], None]) -> Callable[[], None]:
|
def add_update_listener(self, listener: Callable[[], None]) -> Callable[[], None]:
|
||||||
"""Register a callback for state changes.
|
"""Register a callback for state changes.
|
||||||
|
|
||||||
@@ -231,6 +347,10 @@ class OasisDevice:
|
|||||||
self._update_field("mac_address", mac)
|
self._update_field("mac_address", mac)
|
||||||
return mac
|
return mac
|
||||||
|
|
||||||
|
async def async_set_auto_clean(self, auto_clean: bool) -> None:
|
||||||
|
client = self._require_client()
|
||||||
|
await client.async_send_auto_clean_command(self, auto_clean)
|
||||||
|
|
||||||
async def async_set_ball_speed(self, speed: int) -> None:
|
async def async_set_ball_speed(self, speed: int) -> None:
|
||||||
if not BALL_SPEED_MIN <= speed <= BALL_SPEED_MAX:
|
if not BALL_SPEED_MIN <= speed <= BALL_SPEED_MAX:
|
||||||
raise ValueError("Invalid speed specified")
|
raise ValueError("Invalid speed specified")
|
||||||
@@ -245,7 +365,7 @@ class OasisDevice:
|
|||||||
led_speed: int | None = None,
|
led_speed: int | None = None,
|
||||||
brightness: int | None = None,
|
brightness: int | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set the Oasis Mini LED (shared validation & attribute updates)."""
|
"""Set the Oasis device LED (shared validation & attribute updates)."""
|
||||||
if led_effect is None:
|
if led_effect is None:
|
||||||
led_effect = self.led_effect
|
led_effect = self.led_effect
|
||||||
if color is None:
|
if color is None:
|
||||||
@@ -259,7 +379,7 @@ class OasisDevice:
|
|||||||
raise ValueError("Invalid led effect specified")
|
raise ValueError("Invalid led effect specified")
|
||||||
if not LED_SPEED_MIN <= led_speed <= LED_SPEED_MAX:
|
if not LED_SPEED_MIN <= led_speed <= LED_SPEED_MAX:
|
||||||
raise ValueError("Invalid led speed specified")
|
raise ValueError("Invalid led speed specified")
|
||||||
if not 0 <= brightness <= self.max_brightness:
|
if not 0 <= brightness <= self.brightness_max:
|
||||||
raise ValueError("Invalid brightness specified")
|
raise ValueError("Invalid brightness specified")
|
||||||
|
|
||||||
client = self._require_client()
|
client = self._require_client()
|
||||||
@@ -325,3 +445,43 @@ class OasisDevice:
|
|||||||
async def async_reboot(self) -> None:
|
async def async_reboot(self) -> None:
|
||||||
client = self._require_client()
|
client = self._require_client()
|
||||||
await client.async_send_reboot_command(self)
|
await client.async_send_reboot_command(self)
|
||||||
|
|
||||||
|
def schedule_track_refresh(self) -> None:
|
||||||
|
"""Schedule an async refresh of current track info if track_id changed."""
|
||||||
|
if not self._cloud:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
except RuntimeError:
|
||||||
|
_LOGGER.debug("No running loop; cannot schedule track refresh")
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._track_task and not self._track_task.done():
|
||||||
|
self._track_task.cancel()
|
||||||
|
|
||||||
|
self._track_task = loop.create_task(self._async_refresh_current_track())
|
||||||
|
|
||||||
|
async def _async_refresh_current_track(self) -> None:
|
||||||
|
"""Refresh the current track info."""
|
||||||
|
if not self._cloud:
|
||||||
|
return
|
||||||
|
|
||||||
|
if (track_id := self.track_id) is None:
|
||||||
|
self._track = None
|
||||||
|
return
|
||||||
|
|
||||||
|
if self._track and self._track.get("id") == track_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
track = await self._cloud.async_get_track_info(track_id)
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
_LOGGER.exception("Error fetching track info for %s", track_id)
|
||||||
|
return
|
||||||
|
|
||||||
|
if not track:
|
||||||
|
return
|
||||||
|
|
||||||
|
self._track = track
|
||||||
|
self._notify_listeners()
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""Oasis Mini utils."""
|
"""Oasis control utils."""
|
||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
@@ -35,8 +35,8 @@ def _parse_int(val: str) -> int:
|
|||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|
||||||
def draw_svg(track: dict, progress: int, model_id: str) -> str | None:
|
def create_svg(track: dict, progress: int) -> str | None:
|
||||||
"""Draw SVG."""
|
"""Create an SVG from a track based on progress."""
|
||||||
if track and (svg_content := track.get("svg_content")):
|
if track and (svg_content := track.get("svg_content")):
|
||||||
try:
|
try:
|
||||||
if progress is not None:
|
if progress is not None:
|
||||||
|
|||||||
@@ -7,16 +7,19 @@ from dataclasses import dataclass
|
|||||||
from typing import Any, Awaitable, Callable
|
from typing import Any, Awaitable, Callable
|
||||||
|
|
||||||
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
from homeassistant.components.select import SelectEntity, SelectEntityDescription
|
||||||
|
from homeassistant.const import EntityCategory
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity import EntityDescription
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
from .coordinator import OasisDeviceCoordinator
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
from .pyoasiscontrol import OasisDevice
|
from .pyoasiscontrol import OasisDevice
|
||||||
from .pyoasiscontrol.const import AUTOPLAY_MAP, TRACKS
|
from .pyoasiscontrol.const import AUTOPLAY_MAP, TRACKS
|
||||||
|
|
||||||
|
AUTOPLAY_MAP_LIST = list(AUTOPLAY_MAP)
|
||||||
|
|
||||||
|
|
||||||
def playlists_update_handler(entity: OasisDeviceSelectEntity) -> None:
|
def playlists_update_handler(entity: OasisDeviceSelectEntity) -> None:
|
||||||
"""Handle playlists updates."""
|
"""Handle playlists updates."""
|
||||||
@@ -25,7 +28,7 @@ def playlists_update_handler(entity: OasisDeviceSelectEntity) -> None:
|
|||||||
counts = defaultdict(int)
|
counts = defaultdict(int)
|
||||||
options = []
|
options = []
|
||||||
current_option: str | None = None
|
current_option: str | None = None
|
||||||
for playlist in device.playlists:
|
for playlist in device._cloud.playlists:
|
||||||
name = playlist["name"]
|
name = playlist["name"]
|
||||||
counts[name] += 1
|
counts[name] += 1
|
||||||
if counts[name] > 1:
|
if counts[name] > 1:
|
||||||
@@ -68,19 +71,15 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device select using config entry."""
|
"""Set up Oasis device select using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
entities = [
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceSelectEntity(coordinator, device, descriptor)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceSelectEntity(entry.runtime_data, device, descriptor)
|
||||||
|
for device in new_devices
|
||||||
for descriptor in DESCRIPTORS
|
for descriptor in DESCRIPTORS
|
||||||
]
|
]
|
||||||
# if coordinator.device.access_token:
|
|
||||||
# entities.extend(
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
# OasisDeviceSelectEntity(coordinator, device, descriptor)
|
|
||||||
# for device in coordinator.data
|
|
||||||
# for descriptor in CLOUD_DESCRIPTORS
|
|
||||||
# )
|
|
||||||
async_add_entities(entities)
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True, kw_only=True)
|
@dataclass(frozen=True, kw_only=True)
|
||||||
@@ -96,29 +95,30 @@ DESCRIPTORS = (
|
|||||||
OasisDeviceSelectEntityDescription(
|
OasisDeviceSelectEntityDescription(
|
||||||
key="autoplay",
|
key="autoplay",
|
||||||
translation_key="autoplay",
|
translation_key="autoplay",
|
||||||
options=list(AUTOPLAY_MAP.values()),
|
entity_category=EntityCategory.CONFIG,
|
||||||
current_value=lambda device: device.autoplay,
|
options=AUTOPLAY_MAP_LIST,
|
||||||
select_fn=lambda device, option: device.async_set_autoplay(option),
|
current_value=lambda device: str(device.autoplay),
|
||||||
|
select_fn=lambda device, index: (
|
||||||
|
device.async_set_autoplay(AUTOPLAY_MAP_LIST[index])
|
||||||
|
),
|
||||||
|
),
|
||||||
|
OasisDeviceSelectEntityDescription(
|
||||||
|
key="playlists",
|
||||||
|
translation_key="playlist",
|
||||||
|
current_value=lambda device: (device._cloud.playlists, device.playlist.copy()),
|
||||||
|
select_fn=lambda device, index: device.async_set_playlist(
|
||||||
|
[pattern["id"] for pattern in device._cloud.playlists[index]["patterns"]]
|
||||||
|
),
|
||||||
|
update_handler=playlists_update_handler,
|
||||||
),
|
),
|
||||||
OasisDeviceSelectEntityDescription(
|
OasisDeviceSelectEntityDescription(
|
||||||
key="queue",
|
key="queue",
|
||||||
translation_key="queue",
|
translation_key="queue",
|
||||||
current_value=lambda device: (device.playlist.copy(), device.playlist_index),
|
current_value=lambda device: (device.playlist.copy(), device.playlist_index),
|
||||||
select_fn=lambda device, option: device.async_change_track(option),
|
select_fn=lambda device, index: device.async_change_track(index),
|
||||||
update_handler=queue_update_handler,
|
update_handler=queue_update_handler,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
CLOUD_DESCRIPTORS = (
|
|
||||||
OasisDeviceSelectEntityDescription(
|
|
||||||
key="playlists",
|
|
||||||
translation_key="playlist",
|
|
||||||
current_value=lambda device: (device.playlists, device.playlist.copy()),
|
|
||||||
select_fn=lambda device, option: device.async_set_playlist(
|
|
||||||
[pattern["id"] for pattern in device.playlists[option]["patterns"]]
|
|
||||||
),
|
|
||||||
update_handler=playlists_update_handler,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class OasisDeviceSelectEntity(OasisDeviceEntity, SelectEntity):
|
class OasisDeviceSelectEntity(OasisDeviceEntity, SelectEntity):
|
||||||
@@ -140,7 +140,6 @@ class OasisDeviceSelectEntity(OasisDeviceEntity, SelectEntity):
|
|||||||
async def async_select_option(self, option: str) -> None:
|
async def async_select_option(self, option: str) -> None:
|
||||||
"""Change the selected option."""
|
"""Change the selected option."""
|
||||||
await self.entity_description.select_fn(self.device, self.options.index(option))
|
await self.entity_description.select_fn(self.device, self.options.index(option))
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _handle_coordinator_update(self) -> None:
|
def _handle_coordinator_update(self) -> None:
|
||||||
@@ -152,8 +151,8 @@ class OasisDeviceSelectEntity(OasisDeviceEntity, SelectEntity):
|
|||||||
if update_handler := self.entity_description.update_handler:
|
if update_handler := self.entity_description.update_handler:
|
||||||
update_handler(self)
|
update_handler(self)
|
||||||
else:
|
else:
|
||||||
self._attr_current_option = getattr(
|
self._attr_current_option = str(
|
||||||
self.device, self.entity_description.key
|
getattr(self.device, self.entity_description.key)
|
||||||
)
|
)
|
||||||
if self.hass:
|
if self.hass:
|
||||||
return super()._handle_coordinator_update()
|
return super()._handle_coordinator_update()
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ from homeassistant.const import PERCENTAGE, EntityCategory
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
|
from .pyoasiscontrol import OasisDevice
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@@ -22,18 +22,15 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device sensors using config entry."""
|
"""Set up Oasis device sensors using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
entities = [
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceSensorEntity(coordinator, device, descriptor)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceSensorEntity(entry.runtime_data, device, descriptor)
|
||||||
|
for device in new_devices
|
||||||
for descriptor in DESCRIPTORS
|
for descriptor in DESCRIPTORS
|
||||||
]
|
]
|
||||||
entities.extend(
|
|
||||||
OasisDeviceSensorEntity(coordinator, device, descriptor)
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
for device in coordinator.data
|
|
||||||
for descriptor in CLOUD_DESCRIPTORS
|
|
||||||
)
|
|
||||||
async_add_entities(entities)
|
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTORS = {
|
DESCRIPTORS = {
|
||||||
@@ -45,19 +42,6 @@ DESCRIPTORS = {
|
|||||||
native_unit_of_measurement=PERCENTAGE,
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
),
|
),
|
||||||
} | {
|
|
||||||
SensorEntityDescription(
|
|
||||||
key=key,
|
|
||||||
translation_key=key,
|
|
||||||
entity_category=EntityCategory.DIAGNOSTIC,
|
|
||||||
entity_registry_enabled_default=False,
|
|
||||||
)
|
|
||||||
for key in ("error", "status")
|
|
||||||
# for key in ("error", "led_color_id", "status")
|
|
||||||
# for key in ("error_message", "led_color_id", "status")
|
|
||||||
}
|
|
||||||
|
|
||||||
CLOUD_DESCRIPTORS = (
|
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key="drawing_progress",
|
key="drawing_progress",
|
||||||
translation_key="drawing_progress",
|
translation_key="drawing_progress",
|
||||||
@@ -66,7 +50,16 @@ CLOUD_DESCRIPTORS = (
|
|||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
suggested_display_precision=1,
|
suggested_display_precision=1,
|
||||||
),
|
),
|
||||||
|
} | {
|
||||||
|
SensorEntityDescription(
|
||||||
|
key=key,
|
||||||
|
translation_key=key,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
)
|
)
|
||||||
|
for key in ("error", "led_color_id", "status")
|
||||||
|
# for key in ("error_message", "led_color_id", "status")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class OasisDeviceSensorEntity(OasisDeviceEntity, SensorEntity):
|
class OasisDeviceSensorEntity(OasisDeviceEntity, SensorEntity):
|
||||||
|
|||||||
@@ -74,7 +74,18 @@
|
|||||||
},
|
},
|
||||||
"select": {
|
"select": {
|
||||||
"autoplay": {
|
"autoplay": {
|
||||||
"name": "Autoplay"
|
"name": "Autoplay",
|
||||||
|
"state": {
|
||||||
|
"0": "on",
|
||||||
|
"1": "off",
|
||||||
|
"2": "5 minutes",
|
||||||
|
"3": "10 minutes",
|
||||||
|
"4": "30 minutes",
|
||||||
|
"6": "1 hour",
|
||||||
|
"7": "6 hours",
|
||||||
|
"8": "12 hours",
|
||||||
|
"5": "24 hours"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"playlist": {
|
"playlist": {
|
||||||
"name": "Playlist"
|
"name": "Playlist"
|
||||||
@@ -133,6 +144,11 @@
|
|||||||
"live": "Live drawing"
|
"live": "Live drawing"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"switch": {
|
||||||
|
"auto_clean": {
|
||||||
|
"name": "Auto-clean"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"exceptions": {
|
"exceptions": {
|
||||||
|
|||||||
@@ -1,53 +1,57 @@
|
|||||||
# """Oasis Mini switch entity."""
|
"""Oasis device switch entity."""
|
||||||
|
|
||||||
# from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
# from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
# from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
||||||
# from homeassistant.core import HomeAssistant
|
from homeassistant.const import EntityCategory
|
||||||
# from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
# from . import OasisMiniConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
# from .entity import OasisMiniEntity
|
from .entity import OasisDeviceEntity
|
||||||
|
from .pyoasiscontrol import OasisDevice
|
||||||
|
|
||||||
|
|
||||||
# async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
# hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
# entry: OasisMiniConfigEntry,
|
entry: OasisDeviceConfigEntry,
|
||||||
# async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
# ) -> None:
|
) -> None:
|
||||||
# """Set up Oasis Mini switchs using config entry."""
|
"""Set up Oasis device switchs using config entry."""
|
||||||
# async_add_entities(
|
|
||||||
# [
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
# OasisMiniSwitchEntity(entry.runtime_data, descriptor)
|
return [
|
||||||
# for descriptor in DESCRIPTORS
|
OasisDeviceSwitchEntity(entry.runtime_data, device, descriptor)
|
||||||
# ]
|
for device in new_devices
|
||||||
# )
|
for descriptor in DESCRIPTORS
|
||||||
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||||
|
|
||||||
|
|
||||||
# class OasisMiniSwitchEntity(OasisMiniEntity, SwitchEntity):
|
DESCRIPTORS = {
|
||||||
# """Oasis Mini switch entity."""
|
SwitchEntityDescription(
|
||||||
|
key="auto_clean",
|
||||||
# @property
|
translation_key="auto_clean",
|
||||||
# def is_on(self) -> bool:
|
entity_category=EntityCategory.CONFIG,
|
||||||
# """Return True if entity is on."""
|
),
|
||||||
# return int(getattr(self.device, self.entity_description.key))
|
}
|
||||||
|
|
||||||
# async def async_turn_off(self, **kwargs: Any) -> None:
|
|
||||||
# """Turn the entity off."""
|
|
||||||
# await self.device.async_set_repeat_playlist(False)
|
|
||||||
# await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
# async def async_turn_on(self, **kwargs: Any) -> None:
|
|
||||||
# """Turn the entity on."""
|
|
||||||
# await self.device.async_set_repeat_playlist(True)
|
|
||||||
# await self.coordinator.async_request_refresh()
|
|
||||||
|
|
||||||
|
|
||||||
# DESCRIPTORS = {
|
class OasisDeviceSwitchEntity(OasisDeviceEntity, SwitchEntity):
|
||||||
# SwitchEntityDescription(
|
"""Oasis device switch entity."""
|
||||||
# key="repeat_playlist",
|
|
||||||
# name="Repeat playlist",
|
@property
|
||||||
# ),
|
def is_on(self) -> bool:
|
||||||
# }
|
"""Return True if entity is on."""
|
||||||
|
return bool(getattr(self.device, self.entity_description.key))
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the entity off."""
|
||||||
|
await self.device.async_set_auto_clean(False)
|
||||||
|
|
||||||
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
|
"""Turn the entity on."""
|
||||||
|
await self.device.async_set_auto_clean(True)
|
||||||
|
|||||||
@@ -74,7 +74,18 @@
|
|||||||
},
|
},
|
||||||
"select": {
|
"select": {
|
||||||
"autoplay": {
|
"autoplay": {
|
||||||
"name": "Autoplay"
|
"name": "Autoplay",
|
||||||
|
"state": {
|
||||||
|
"0": "on",
|
||||||
|
"1": "off",
|
||||||
|
"2": "5 minutes",
|
||||||
|
"3": "10 minutes",
|
||||||
|
"4": "30 minutes",
|
||||||
|
"6": "1 hour",
|
||||||
|
"7": "6 hours",
|
||||||
|
"8": "12 hours",
|
||||||
|
"5": "24 hours"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"playlist": {
|
"playlist": {
|
||||||
"name": "Playlist"
|
"name": "Playlist"
|
||||||
@@ -133,6 +144,11 @@
|
|||||||
"live": "Live drawing"
|
"live": "Live drawing"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"switch": {
|
||||||
|
"auto_clean": {
|
||||||
|
"name": "Auto-clean"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"exceptions": {
|
"exceptions": {
|
||||||
|
|||||||
@@ -15,9 +15,9 @@ from homeassistant.components.update import (
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import OasisDeviceConfigEntry
|
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||||
from .coordinator import OasisDeviceCoordinator
|
|
||||||
from .entity import OasisDeviceEntity
|
from .entity import OasisDeviceEntity
|
||||||
|
from .pyoasiscontrol import OasisDevice
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -30,12 +30,14 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up Oasis device updates using config entry."""
|
"""Set up Oasis device updates using config entry."""
|
||||||
coordinator: OasisDeviceCoordinator = entry.runtime_data
|
|
||||||
entities = (
|
def make_entities(new_devices: list[OasisDevice]):
|
||||||
OasisDeviceUpdateEntity(coordinator, device, DESCRIPTOR)
|
return [
|
||||||
for device in coordinator.data
|
OasisDeviceUpdateEntity(entry.runtime_data, device, DESCRIPTOR)
|
||||||
)
|
for device in new_devices
|
||||||
async_add_entities(entities, True)
|
]
|
||||||
|
|
||||||
|
setup_platform_from_coordinator(entry, async_add_entities, make_entities, True)
|
||||||
|
|
||||||
|
|
||||||
DESCRIPTOR = UpdateEntityDescription(
|
DESCRIPTOR = UpdateEntityDescription(
|
||||||
|
|||||||
Reference in New Issue
Block a user