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 number entity."""
|
||||
"""Oasis device number entity."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -7,35 +7,61 @@ from homeassistant.components.number import (
|
||||
NumberEntityDescription,
|
||||
NumberMode,
|
||||
)
|
||||
from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import OasisMiniConfigEntry
|
||||
from .entity import OasisMiniEntity
|
||||
from .pyoasismini import BALL_SPEED_MAX, BALL_SPEED_MIN, LED_SPEED_MAX, LED_SPEED_MIN
|
||||
from . import OasisDeviceConfigEntry, setup_platform_from_coordinator
|
||||
from .entity import OasisDeviceEntity
|
||||
from .pyoasiscontrol import OasisDevice
|
||||
from .pyoasiscontrol.device import (
|
||||
BALL_SPEED_MAX,
|
||||
BALL_SPEED_MIN,
|
||||
LED_SPEED_MAX,
|
||||
LED_SPEED_MIN,
|
||||
)
|
||||
|
||||
|
||||
class OasisMiniNumberEntity(OasisMiniEntity, NumberEntity):
|
||||
"""Oasis Mini number entity."""
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, # noqa: ARG001
|
||||
entry: OasisDeviceConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""
|
||||
Set up number entities for Oasis devices from a configuration entry.
|
||||
|
||||
@property
|
||||
def native_value(self) -> str | None:
|
||||
"""Return the value reported by the number."""
|
||||
return getattr(self.device, self.entity_description.key)
|
||||
Creates number entities for each discovered Oasis device and each descriptor in DESCRIPTORS, then registers those entities with the platform coordinator so they are added to Home Assistant.
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""Set new value."""
|
||||
if self.entity_description.key == "ball_speed":
|
||||
await self.device.async_set_ball_speed(value)
|
||||
elif self.entity_description.key == "led_speed":
|
||||
await self.device.async_set_led(led_speed=value)
|
||||
await self.coordinator.async_request_refresh()
|
||||
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 number entity instances for each provided Oasis device using the module's DESCRIPTORS.
|
||||
|
||||
Parameters:
|
||||
new_devices (list[OasisDevice]): Devices to create entities for.
|
||||
|
||||
Returns:
|
||||
list[OasisDeviceNumberEntity]: A flat list of number entities (one per descriptor for each device).
|
||||
"""
|
||||
return [
|
||||
OasisDeviceNumberEntity(entry.runtime_data, device, descriptor)
|
||||
for device in new_devices
|
||||
for descriptor in DESCRIPTORS
|
||||
]
|
||||
|
||||
setup_platform_from_coordinator(entry, async_add_entities, make_entities)
|
||||
|
||||
|
||||
DESCRIPTORS = {
|
||||
DESCRIPTORS = (
|
||||
NumberEntityDescription(
|
||||
key="ball_speed",
|
||||
translation_key="ball_speed",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
mode=NumberMode.SLIDER,
|
||||
native_max_value=BALL_SPEED_MAX,
|
||||
native_min_value=BALL_SPEED_MIN,
|
||||
@@ -43,22 +69,38 @@ DESCRIPTORS = {
|
||||
NumberEntityDescription(
|
||||
key="led_speed",
|
||||
translation_key="led_speed",
|
||||
entity_category=EntityCategory.CONFIG,
|
||||
mode=NumberMode.SLIDER,
|
||||
native_max_value=LED_SPEED_MAX,
|
||||
native_min_value=LED_SPEED_MIN,
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: OasisMiniConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Oasis Mini numbers using config entry."""
|
||||
async_add_entities(
|
||||
[
|
||||
OasisMiniNumberEntity(entry.runtime_data, descriptor)
|
||||
for descriptor in DESCRIPTORS
|
||||
]
|
||||
)
|
||||
class OasisDeviceNumberEntity(OasisDeviceEntity, NumberEntity):
|
||||
"""Oasis device number entity."""
|
||||
|
||||
@property
|
||||
def native_value(self) -> float | None:
|
||||
"""
|
||||
Get the current value of the number entity from the underlying device.
|
||||
|
||||
Returns:
|
||||
float | None: The current value as a float, or `None` if the device has no value.
|
||||
"""
|
||||
return getattr(self.device, self.entity_description.key)
|
||||
|
||||
async def async_set_native_value(self, value: float) -> None:
|
||||
"""
|
||||
Set the configured numeric value on the underlying Oasis device.
|
||||
|
||||
The provided value is converted to an integer and applied to the device property indicated by this entity's description key: if the key is "ball_speed" the device's ball speed is updated; if the key is "led_speed" the device's LED speed is updated.
|
||||
|
||||
Parameters:
|
||||
value (float): New numeric value to apply; will be converted to an integer.
|
||||
"""
|
||||
value = int(value)
|
||||
if self.entity_description.key == "ball_speed":
|
||||
await self.device.async_set_ball_speed(value)
|
||||
elif self.entity_description.key == "led_speed":
|
||||
await self.device.async_set_led(led_speed=value)
|
||||
|
||||
Reference in New Issue
Block a user