1 """YoLink device number type config settings."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
9 from yolink.client_request
import ClientRequest
10 from yolink.const
import ATTR_DEVICE_SPEAKER_HUB
11 from yolink.device
import YoLinkDevice
15 NumberEntityDescription,
22 from .const
import DOMAIN
23 from .coordinator
import YoLinkCoordinator
24 from .entity
import YoLinkEntity
26 OPTIONS_VOLUME =
"options_volume"
29 @dataclass(frozen=True, kw_only=True)
31 """YoLink NumberEntity description."""
33 exists_fn: Callable[[YoLinkDevice], bool]
34 should_update_entity: Callable
38 NUMBER_TYPE_CONF_SUPPORT_DEVICES = [ATTR_DEVICE_SPEAKER_HUB]
40 SUPPORT_SET_VOLUME_DEVICES = [ATTR_DEVICE_SPEAKER_HUB]
44 """Get volume option."""
45 if (options := state.get(
"options"))
is not None:
46 return options.get(
"volume")
50 DEVICE_CONFIG_DESCRIPTIONS: tuple[YoLinkNumberTypeConfigEntityDescription, ...] = (
53 translation_key=
"config_volume",
56 mode=NumberMode.SLIDER,
58 native_unit_of_measurement=
None,
59 exists_fn=
lambda device: device.device_type
in SUPPORT_SET_VOLUME_DEVICES,
60 should_update_entity=
lambda value: value
is not None,
61 value=get_volume_value,
68 config_entry: ConfigEntry,
69 async_add_entities: AddEntitiesCallback,
71 """Set up device number type config option entity from a config entry."""
72 device_coordinators = hass.data[DOMAIN][config_entry.entry_id].device_coordinators
73 config_device_coordinators = [
75 for device_coordinator
in device_coordinators.values()
76 if device_coordinator.device.device_type
in NUMBER_TYPE_CONF_SUPPORT_DEVICES
81 config_device_coordinator,
84 for config_device_coordinator
in config_device_coordinators
85 for description
in DEVICE_CONFIG_DESCRIPTIONS
86 if description.exists_fn(config_device_coordinator.device)
91 """YoLink number type config Entity."""
93 entity_description: YoLinkNumberTypeConfigEntityDescription
97 config_entry: ConfigEntry,
98 coordinator: YoLinkCoordinator,
99 description: YoLinkNumberTypeConfigEntityDescription,
101 """Init YoLink device number type config entities."""
102 super().
__init__(config_entry, coordinator)
104 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.device.device_id} {description.key}"
108 """Update HA Entity State."""
111 )
is None and self.
entity_descriptionentity_description.should_update_entity(attr_val)
is False:
117 """Update SpeakerHub volume."""
118 await self.
call_devicecall_device(ClientRequest(
"setOption", {
"volume": volume}))
121 """Update the current value."""
123 self.coordinator.device.device_type == ATTR_DEVICE_SPEAKER_HUB
None call_device(self, ClientRequest request)
None __init__(self, ConfigEntry config_entry, YoLinkCoordinator coordinator, YoLinkNumberTypeConfigEntityDescription description)
None async_set_native_value(self, float value)
None update_entity_state(self, dict state)
None update_speaker_hub_volume(self, float volume)
None async_write_ha_state(self)
int|None get_volume_value(dict[str, Any] state)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)