Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """YoLink device number type config settings."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from typing import Any
8 
9 from yolink.client_request import ClientRequest
10 from yolink.const import ATTR_DEVICE_SPEAKER_HUB
11 from yolink.device import YoLinkDevice
12 
14  NumberEntity,
15  NumberEntityDescription,
16  NumberMode,
17 )
18 from homeassistant.config_entries import ConfigEntry
19 from homeassistant.core import HomeAssistant, callback
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 
22 from .const import DOMAIN
23 from .coordinator import YoLinkCoordinator
24 from .entity import YoLinkEntity
25 
26 OPTIONS_VOLUME = "options_volume"
27 
28 
29 @dataclass(frozen=True, kw_only=True)
31  """YoLink NumberEntity description."""
32 
33  exists_fn: Callable[[YoLinkDevice], bool]
34  should_update_entity: Callable
35  value: Callable
36 
37 
38 NUMBER_TYPE_CONF_SUPPORT_DEVICES = [ATTR_DEVICE_SPEAKER_HUB]
39 
40 SUPPORT_SET_VOLUME_DEVICES = [ATTR_DEVICE_SPEAKER_HUB]
41 
42 
43 def get_volume_value(state: dict[str, Any]) -> int | None:
44  """Get volume option."""
45  if (options := state.get("options")) is not None:
46  return options.get("volume")
47  return None
48 
49 
50 DEVICE_CONFIG_DESCRIPTIONS: tuple[YoLinkNumberTypeConfigEntityDescription, ...] = (
52  key=OPTIONS_VOLUME,
53  translation_key="config_volume",
54  native_min_value=1,
55  native_max_value=16,
56  mode=NumberMode.SLIDER,
57  native_step=1.0,
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,
62  ),
63 )
64 
65 
67  hass: HomeAssistant,
68  config_entry: ConfigEntry,
69  async_add_entities: AddEntitiesCallback,
70 ) -> None:
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 = [
74  device_coordinator
75  for device_coordinator in device_coordinators.values()
76  if device_coordinator.device.device_type in NUMBER_TYPE_CONF_SUPPORT_DEVICES
77  ]
80  config_entry,
81  config_device_coordinator,
82  description,
83  )
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)
87  )
88 
89 
91  """YoLink number type config Entity."""
92 
93  entity_description: YoLinkNumberTypeConfigEntityDescription
94 
95  def __init__(
96  self,
97  config_entry: ConfigEntry,
98  coordinator: YoLinkCoordinator,
99  description: YoLinkNumberTypeConfigEntityDescription,
100  ) -> None:
101  """Init YoLink device number type config entities."""
102  super().__init__(config_entry, coordinator)
103  self.entity_descriptionentity_description = description
104  self._attr_unique_id_attr_unique_id = f"{coordinator.device.device_id} {description.key}"
105 
106  @callback
107  def update_entity_state(self, state: dict) -> None:
108  """Update HA Entity State."""
109  if (
110  attr_val := self.entity_descriptionentity_description.value(state)
111  ) is None and self.entity_descriptionentity_description.should_update_entity(attr_val) is False:
112  return
113  self._attr_native_value_attr_native_value = attr_val
114  self.async_write_ha_stateasync_write_ha_state()
115 
116  async def update_speaker_hub_volume(self, volume: float) -> None:
117  """Update SpeakerHub volume."""
118  await self.call_devicecall_device(ClientRequest("setOption", {"volume": volume}))
119 
120  async def async_set_native_value(self, value: float) -> None:
121  """Update the current value."""
122  if (
123  self.coordinator.device.device_type == ATTR_DEVICE_SPEAKER_HUB
124  and self.entity_descriptionentity_description.key == OPTIONS_VOLUME
125  ):
126  await self.update_speaker_hub_volumeupdate_speaker_hub_volume(value)
127  self._attr_native_value_attr_native_value = value
128  self.async_write_ha_stateasync_write_ha_state()