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:
@@ -1,4 +1,4 @@
|
||||
"""Oasis Mini button entity."""
|
||||
"""Oasis device button entity."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -13,55 +13,87 @@ from homeassistant.components.button import (
|
||||
)
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import OasisMiniConfigEntry
|
||||
from .entity import OasisMiniEntity
|
||||
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||
from .entity import OasisDeviceEntity
|
||||
from .helpers import add_and_play_track
|
||||
from .pyoasismini import OasisMini
|
||||
from .pyoasismini.const import TRACKS
|
||||
from .pyoasiscontrol import OasisDevice
|
||||
from .pyoasiscontrol.const import TRACKS
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: OasisMiniConfigEntry,
|
||||
hass: HomeAssistant, # noqa: ARG001
|
||||
entry: OasisDeviceConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Oasis Mini button using config entry."""
|
||||
async_add_entities(
|
||||
[
|
||||
OasisMiniButtonEntity(entry.runtime_data, descriptor)
|
||||
"""
|
||||
Create and add button entities for each Oasis device defined in the config entry.
|
||||
|
||||
Parameters:
|
||||
entry (OasisDeviceConfigEntry): Config entry containing runtime data and registered Oasis devices.
|
||||
async_add_entities (AddEntitiesCallback): Callback used to register the created entities with Home Assistant.
|
||||
"""
|
||||
|
||||
def make_entities(new_devices: list[OasisDevice]):
|
||||
"""
|
||||
Create button entities for each provided Oasis device using the module descriptors.
|
||||
|
||||
Parameters:
|
||||
new_devices (list[OasisDevice]): Devices to create button entities for.
|
||||
|
||||
Returns:
|
||||
list[OasisDeviceButtonEntity]: Button entity instances created for each device and each descriptor in DESCRIPTORS.
|
||||
"""
|
||||
return [
|
||||
OasisDeviceButtonEntity(entry.runtime_data, device, descriptor)
|
||||
for device in new_devices
|
||||
for descriptor in DESCRIPTORS
|
||||
]
|
||||
)
|
||||
|
||||
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||
|
||||
|
||||
async def play_random_track(device: OasisMini) -> None:
|
||||
"""Play random track."""
|
||||
async def play_random_track(device: OasisDevice) -> None:
|
||||
"""
|
||||
Play a random track on the given Oasis device.
|
||||
|
||||
Selects a track at random from the available TRACKS and attempts to add it to the device's queue and play it. Raises HomeAssistantError if adding the track times out.
|
||||
|
||||
Parameters:
|
||||
device: The Oasis device on which to play the track.
|
||||
|
||||
Raises:
|
||||
HomeAssistantError: If adding the selected track to the device's queue times out.
|
||||
"""
|
||||
track = random.choice(list(TRACKS))
|
||||
await add_and_play_track(device, track)
|
||||
try:
|
||||
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)
|
||||
class OasisMiniButtonEntityDescription(ButtonEntityDescription):
|
||||
"""Oasis Mini button entity description."""
|
||||
class OasisDeviceButtonEntityDescription(ButtonEntityDescription):
|
||||
"""Oasis device button entity description."""
|
||||
|
||||
press_fn: Callable[[OasisMini], Awaitable[None]]
|
||||
press_fn: Callable[[OasisDevice], Awaitable[None]]
|
||||
|
||||
|
||||
DESCRIPTORS = (
|
||||
OasisMiniButtonEntityDescription(
|
||||
OasisDeviceButtonEntityDescription(
|
||||
key="reboot",
|
||||
device_class=ButtonDeviceClass.RESTART,
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
press_fn=lambda device: device.async_reboot(),
|
||||
),
|
||||
OasisMiniButtonEntityDescription(
|
||||
OasisDeviceButtonEntityDescription(
|
||||
key="random_track",
|
||||
translation_key="random_track",
|
||||
press_fn=play_random_track,
|
||||
),
|
||||
OasisMiniButtonEntityDescription(
|
||||
OasisDeviceButtonEntityDescription(
|
||||
key="sleep",
|
||||
translation_key="sleep",
|
||||
press_fn=lambda device: device.async_sleep(),
|
||||
@@ -69,12 +101,15 @@ DESCRIPTORS = (
|
||||
)
|
||||
|
||||
|
||||
class OasisMiniButtonEntity(OasisMiniEntity, ButtonEntity):
|
||||
"""Oasis Mini button entity."""
|
||||
class OasisDeviceButtonEntity(OasisDeviceEntity, ButtonEntity):
|
||||
"""Oasis device button entity."""
|
||||
|
||||
entity_description: OasisMiniButtonEntityDescription
|
||||
entity_description: OasisDeviceButtonEntityDescription
|
||||
|
||||
async def async_press(self) -> None:
|
||||
"""Press the button."""
|
||||
"""
|
||||
Trigger the button's configured action on the associated device.
|
||||
|
||||
Calls the entity description's `press_fn` with the device to perform the button's effect.
|
||||
"""
|
||||
await self.entity_description.press_fn(self.device)
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
Reference in New Issue
Block a user