1
0
mirror of https://github.com/natekspencer/hacs-oasis_mini.git synced 2025-12-06 18:44:14 -05:00

Swap out direct HTTP connection with server MQTT connection to handle firmware 2.60+ (#98)

* Switch to using mqtt

* Better mqtt handling when connection is interrupted

* Get track info from the cloud when playlist or index changes

* Add additional helpers

* Dynamically handle devices and other enhancements

* 📝 Add docstrings to `mqtt`

Docstrings generation was requested by @natekspencer.

* https://github.com/natekspencer/hacs-oasis_mini/pull/98#issuecomment-3568450288

The following files were modified:

* `custom_components/oasis_mini/__init__.py`
* `custom_components/oasis_mini/binary_sensor.py`
* `custom_components/oasis_mini/button.py`
* `custom_components/oasis_mini/config_flow.py`
* `custom_components/oasis_mini/coordinator.py`
* `custom_components/oasis_mini/entity.py`
* `custom_components/oasis_mini/helpers.py`
* `custom_components/oasis_mini/image.py`
* `custom_components/oasis_mini/light.py`
* `custom_components/oasis_mini/media_player.py`
* `custom_components/oasis_mini/number.py`
* `custom_components/oasis_mini/pyoasiscontrol/clients/cloud_client.py`
* `custom_components/oasis_mini/pyoasiscontrol/clients/http_client.py`
* `custom_components/oasis_mini/pyoasiscontrol/clients/mqtt_client.py`
* `custom_components/oasis_mini/pyoasiscontrol/clients/transport.py`
* `custom_components/oasis_mini/pyoasiscontrol/device.py`
* `custom_components/oasis_mini/pyoasiscontrol/utils.py`
* `custom_components/oasis_mini/select.py`
* `custom_components/oasis_mini/sensor.py`
* `custom_components/oasis_mini/switch.py`
* `custom_components/oasis_mini/update.py`
* `update_tracks.py`

* Fix formatting in transport.py

* Replace tabs with spaces

* Use tuples instead of sets for descriptors

* Encode svg in image entity

* Fix iot_class

* Fix tracks list url

* Ensure update_tracks closes the connection

* Fix number typing and docstring

* Fix docstring in update_tracks

* Cache playlist based on type

* Fix formatting in device.py

* Add missing async_send_auto_clean_command to http client

* Propagate UnauthenticatedError from async_get_track_info

* Adjust exceptions

* Move create_client outside of try block in config_flow

* Formatting

* Address PR comments

* Formatting

* Add noqa: ARG001 on unused hass

* Close cloud/MQTT clients if initial coordinator refresh fails.

* Address PR again

* PR fixes

* Pass config entry to coordinator

* Remove async_timeout (thanks ChatGPT... not)

* Address PR

* Replace magic numbers for status code

* Update autoplay wording/ordering

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Nathan Spencer
2025-11-24 01:09:23 -07:00
committed by GitHub
parent 171a608314
commit 379b6f67f2
40 changed files with 4262 additions and 1263 deletions

View File

@@ -1,4 +1,4 @@
"""Oasis Mini select entity."""
"""Oasis device select entity."""
from __future__ import annotations
@@ -7,71 +7,37 @@ from dataclasses import dataclass
from typing import Any, Awaitable, Callable
from homeassistant.components.select import SelectEntity, SelectEntityDescription
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import OasisMiniConfigEntry
from .coordinator import OasisMiniCoordinator
from .entity import OasisMiniEntity
from .pyoasismini import AUTOPLAY_MAP, OasisMini
from .pyoasismini.const import TRACKS
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
from .coordinator import OasisDeviceCoordinator
from .entity import OasisDeviceEntity
from .pyoasiscontrol import OasisDevice
from .pyoasiscontrol.const import AUTOPLAY_MAP, TRACKS
AUTOPLAY_MAP_LIST = list(AUTOPLAY_MAP)
@dataclass(frozen=True, kw_only=True)
class OasisMiniSelectEntityDescription(SelectEntityDescription):
"""Oasis Mini select entity description."""
def playlists_update_handler(entity: OasisDeviceSelectEntity) -> None:
"""
Update the playlists select options and current option from the device's cloud playlists.
current_value: Callable[[OasisMini], Any]
select_fn: Callable[[OasisMini, int], Awaitable[None]]
update_handler: Callable[[OasisMiniSelectEntity], None] | None = None
Iterates the device's cloud playlists to build a display list of playlist names (appending " (N)" for duplicate names)
and sets the entity's options to that list. If the device's current playlist matches a playlist's pattern IDs,
sets the entity's current option to that playlist's display name; otherwise leaves it None.
class OasisMiniSelectEntity(OasisMiniEntity, SelectEntity):
"""Oasis Mini select entity."""
entity_description: OasisMiniSelectEntityDescription
_current_value: Any | None = None
def __init__(
self,
coordinator: OasisMiniCoordinator,
description: EntityDescription,
) -> None:
"""Construct an Oasis Mini select entity."""
super().__init__(coordinator, description)
self._handle_coordinator_update()
async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
await self.entity_description.select_fn(self.device, self.options.index(option))
await self.coordinator.async_request_refresh()
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
new_value = self.entity_description.current_value(self.device)
if self._current_value == new_value:
return
self._current_value = new_value
if update_handler := self.entity_description.update_handler:
update_handler(self)
else:
self._attr_current_option = getattr(
self.device, self.entity_description.key
)
if self.hass:
return super()._handle_coordinator_update()
def playlists_update_handler(entity: OasisMiniSelectEntity) -> None:
"""Handle playlists updates."""
Parameters:
entity (OasisDeviceSelectEntity): The select entity to update.
"""
# pylint: disable=protected-access
device = entity.device
counts = defaultdict(int)
options = []
current_option: str | None = None
for playlist in device.playlists:
for playlist in device._cloud.playlists:
name = playlist["name"]
counts[name] += 1
if counts[name] > 1:
@@ -83,14 +49,21 @@ def playlists_update_handler(entity: OasisMiniSelectEntity) -> None:
entity._attr_current_option = current_option
def queue_update_handler(entity: OasisMiniSelectEntity) -> None:
"""Handle queue updates."""
def queue_update_handler(entity: OasisDeviceSelectEntity) -> None:
"""
Update the select options and current selection for the device's playback queue.
Populate the entity's options from the device's current playlist and playlist details, disambiguating duplicate track names by appending a counter (e.g., "Title (2)"). Set the entity's current option to the track at device.playlist_index (or None if the queue is empty).
Parameters:
entity (OasisDeviceSelectEntity): The select entity whose options and current option will be updated.
"""
# pylint: disable=protected-access
device = entity.device
counts = defaultdict(int)
options = []
for track in device.playlist:
name = device._playlist.get(track, {}).get(
name = device.playlist_details.get(track, {}).get(
"name",
TRACKS.get(track, {"id": track, "name": f"Unknown Title (#{track})"}).get(
"name",
@@ -108,48 +81,132 @@ def queue_update_handler(entity: OasisMiniSelectEntity) -> None:
entity._attr_current_option = options[index] if options else None
async def async_setup_entry(
hass: HomeAssistant, # noqa: ARG001
entry: OasisDeviceConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""
Set up select entities for each Oasis device from a config entry.
Creates OasisDeviceSelectEntity instances for every device and descriptor and registers them with Home Assistant via the platform setup.
Parameters:
hass (HomeAssistant): Home Assistant core object.
entry (OasisDeviceConfigEntry): Configuration entry containing runtime data and devices to expose.
async_add_entities (AddEntitiesCallback): Callback to add created entities to Home Assistant.
"""
def make_entities(new_devices: list[OasisDevice]):
"""
Create select entity instances for each provided Oasis device.
Parameters:
new_devices (list[OasisDevice]): Devices to create select entities for.
Returns:
list[OasisDeviceSelectEntity]: A flat list of OasisDeviceSelectEntity objects created for every combination of device and descriptor.
"""
return [
OasisDeviceSelectEntity(entry.runtime_data, device, descriptor)
for device in new_devices
for descriptor in DESCRIPTORS
]
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
@dataclass(frozen=True, kw_only=True)
class OasisDeviceSelectEntityDescription(SelectEntityDescription):
"""Oasis device select entity description."""
current_value: Callable[[OasisDevice], Any]
select_fn: Callable[[OasisDevice, int], Awaitable[None]]
update_handler: Callable[[OasisDeviceSelectEntity], None] | None = None
DESCRIPTORS = (
OasisMiniSelectEntityDescription(
OasisDeviceSelectEntityDescription(
key="autoplay",
translation_key="autoplay",
options=list(AUTOPLAY_MAP.values()),
current_value=lambda device: device.autoplay,
select_fn=lambda device, option: device.async_set_autoplay(option),
entity_category=EntityCategory.CONFIG,
options=AUTOPLAY_MAP_LIST,
current_value=lambda device: str(device.autoplay),
select_fn=lambda device, index: (
device.async_set_autoplay(AUTOPLAY_MAP_LIST[index])
),
),
OasisMiniSelectEntityDescription(
key="queue",
translation_key="queue",
current_value=lambda device: (device.playlist.copy(), device.playlist_index),
select_fn=lambda device, option: device.async_change_track(option),
update_handler=queue_update_handler,
),
)
CLOUD_DESCRIPTORS = (
OasisMiniSelectEntityDescription(
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"]]
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(
key="queue",
translation_key="queue",
current_value=lambda device: (device.playlist.copy(), device.playlist_index),
select_fn=lambda device, index: device.async_change_track(index),
update_handler=queue_update_handler,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: OasisMiniConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up Oasis Mini select using config entry."""
coordinator: OasisMiniCoordinator = entry.runtime_data
entities = [
OasisMiniSelectEntity(coordinator, descriptor) for descriptor in DESCRIPTORS
]
if coordinator.device.access_token:
entities.extend(
OasisMiniSelectEntity(coordinator, descriptor)
for descriptor in CLOUD_DESCRIPTORS
)
async_add_entities(entities)
class OasisDeviceSelectEntity(OasisDeviceEntity, SelectEntity):
"""Oasis device select entity."""
entity_description: OasisDeviceSelectEntityDescription
_current_value: Any | None = None
def __init__(
self,
coordinator: OasisDeviceCoordinator,
device: OasisDevice,
description: EntityDescription,
) -> None:
"""
Initialize the Oasis device select entity and perform an initial coordinator update.
Parameters:
coordinator (OasisDeviceCoordinator): Coordinator that manages device updates.
device (OasisDevice): The Oasis device this entity represents.
description (EntityDescription): Metadata describing this select entity.
"""
super().__init__(coordinator, device, description)
self._handle_coordinator_update()
async def async_select_option(self, option: str) -> None:
"""
Select and apply the option identified by its display string.
Parameters:
option (str): The display string of the option to select; the option's index in the current options list is used to apply the selection.
"""
await self.entity_description.select_fn(self.device, self.options.index(option))
@callback
def _handle_coordinator_update(self) -> None:
"""
Update the entity's cached value and current option when coordinator data changes.
If the derived current value differs from the stored value, update the stored value.
If the entity description provides an update_handler, call it with this entity; otherwise,
set the entity's current option to the string form of the device attribute named by the
description's key. If Home Assistant is available on the entity, delegate to the base
class's _handle_coordinator_update to propagate the state change.
"""
new_value = self.entity_description.current_value(self.device)
if self._current_value == new_value:
return
self._current_value = new_value
if update_handler := self.entity_description.update_handler:
update_handler(self)
else:
self._attr_current_option = str(
getattr(self.device, self.entity_description.key)
)
if self.hass:
return super()._handle_coordinator_update()