Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Support for number entities."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from thinqconnect import DeviceType
8 from thinqconnect.devices.const import Property as ThinQProperty
9 from thinqconnect.integration import ActiveMode, TimerProperty
10 
12  NumberDeviceClass,
13  NumberEntity,
14  NumberEntityDescription,
15  NumberMode,
16 )
17 from homeassistant.const import PERCENTAGE, UnitOfTemperature, UnitOfTime
18 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 
21 from . import ThinqConfigEntry
22 from .entity import ThinQEntity
23 
24 NUMBER_DESC: dict[ThinQProperty, NumberEntityDescription] = {
25  ThinQProperty.FAN_SPEED: NumberEntityDescription(
26  key=ThinQProperty.FAN_SPEED,
27  translation_key=ThinQProperty.FAN_SPEED,
28  ),
29  ThinQProperty.LAMP_BRIGHTNESS: NumberEntityDescription(
30  key=ThinQProperty.LAMP_BRIGHTNESS,
31  translation_key=ThinQProperty.LAMP_BRIGHTNESS,
32  ),
33  ThinQProperty.LIGHT_STATUS: NumberEntityDescription(
34  key=ThinQProperty.LIGHT_STATUS,
35  native_unit_of_measurement=PERCENTAGE,
36  translation_key=ThinQProperty.LIGHT_STATUS,
37  ),
38  ThinQProperty.TARGET_HUMIDITY: NumberEntityDescription(
39  key=ThinQProperty.TARGET_HUMIDITY,
40  device_class=NumberDeviceClass.HUMIDITY,
41  native_unit_of_measurement=PERCENTAGE,
42  translation_key=ThinQProperty.TARGET_HUMIDITY,
43  ),
44  ThinQProperty.TARGET_TEMPERATURE: NumberEntityDescription(
45  key=ThinQProperty.TARGET_TEMPERATURE,
46  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
47  translation_key=ThinQProperty.TARGET_TEMPERATURE,
48  ),
49  ThinQProperty.WIND_TEMPERATURE: NumberEntityDescription(
50  key=ThinQProperty.WIND_TEMPERATURE,
51  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
52  translation_key=ThinQProperty.WIND_TEMPERATURE,
53  ),
54 }
55 TIMER_NUMBER_DESC: dict[ThinQProperty, NumberEntityDescription] = {
56  ThinQProperty.RELATIVE_HOUR_TO_START: NumberEntityDescription(
57  key=ThinQProperty.RELATIVE_HOUR_TO_START,
58  native_unit_of_measurement=UnitOfTime.HOURS,
59  translation_key=ThinQProperty.RELATIVE_HOUR_TO_START,
60  ),
61  TimerProperty.RELATIVE_HOUR_TO_START_WM: NumberEntityDescription(
62  key=ThinQProperty.RELATIVE_HOUR_TO_START,
63  native_min_value=0,
64  native_unit_of_measurement=UnitOfTime.HOURS,
65  translation_key=TimerProperty.RELATIVE_HOUR_TO_START_WM,
66  ),
67  ThinQProperty.RELATIVE_HOUR_TO_STOP: NumberEntityDescription(
68  key=ThinQProperty.RELATIVE_HOUR_TO_STOP,
69  native_unit_of_measurement=UnitOfTime.HOURS,
70  translation_key=ThinQProperty.RELATIVE_HOUR_TO_STOP,
71  ),
72  TimerProperty.RELATIVE_HOUR_TO_STOP_WM: NumberEntityDescription(
73  key=ThinQProperty.RELATIVE_HOUR_TO_STOP,
74  native_min_value=0,
75  native_unit_of_measurement=UnitOfTime.HOURS,
76  translation_key=TimerProperty.RELATIVE_HOUR_TO_STOP_WM,
77  ),
78  ThinQProperty.SLEEP_TIMER_RELATIVE_HOUR_TO_STOP: NumberEntityDescription(
79  key=ThinQProperty.SLEEP_TIMER_RELATIVE_HOUR_TO_STOP,
80  native_unit_of_measurement=UnitOfTime.HOURS,
81  translation_key=ThinQProperty.SLEEP_TIMER_RELATIVE_HOUR_TO_STOP,
82  ),
83 }
84 WASHER_NUMBERS: tuple[NumberEntityDescription, ...] = (
85  TIMER_NUMBER_DESC[TimerProperty.RELATIVE_HOUR_TO_START_WM],
86  TIMER_NUMBER_DESC[TimerProperty.RELATIVE_HOUR_TO_STOP_WM],
87 )
88 
89 DEVICE_TYPE_NUMBER_MAP: dict[DeviceType, tuple[NumberEntityDescription, ...]] = {
90  DeviceType.AIR_CONDITIONER: (
91  TIMER_NUMBER_DESC[ThinQProperty.RELATIVE_HOUR_TO_START],
92  TIMER_NUMBER_DESC[ThinQProperty.RELATIVE_HOUR_TO_STOP],
93  TIMER_NUMBER_DESC[ThinQProperty.SLEEP_TIMER_RELATIVE_HOUR_TO_STOP],
94  ),
95  DeviceType.AIR_PURIFIER_FAN: (
96  NUMBER_DESC[ThinQProperty.WIND_TEMPERATURE],
97  TIMER_NUMBER_DESC[ThinQProperty.SLEEP_TIMER_RELATIVE_HOUR_TO_STOP],
98  ),
99  DeviceType.DRYER: WASHER_NUMBERS,
100  DeviceType.HOOD: (
101  NUMBER_DESC[ThinQProperty.LAMP_BRIGHTNESS],
102  NUMBER_DESC[ThinQProperty.FAN_SPEED],
103  ),
104  DeviceType.HUMIDIFIER: (
105  NUMBER_DESC[ThinQProperty.TARGET_HUMIDITY],
106  TIMER_NUMBER_DESC[ThinQProperty.SLEEP_TIMER_RELATIVE_HOUR_TO_STOP],
107  ),
108  DeviceType.MICROWAVE_OVEN: (
109  NUMBER_DESC[ThinQProperty.LAMP_BRIGHTNESS],
110  NUMBER_DESC[ThinQProperty.FAN_SPEED],
111  ),
112  DeviceType.OVEN: (NUMBER_DESC[ThinQProperty.TARGET_TEMPERATURE],),
113  DeviceType.REFRIGERATOR: (NUMBER_DESC[ThinQProperty.TARGET_TEMPERATURE],),
114  DeviceType.STYLER: (TIMER_NUMBER_DESC[TimerProperty.RELATIVE_HOUR_TO_STOP_WM],),
115  DeviceType.WASHCOMBO_MAIN: WASHER_NUMBERS,
116  DeviceType.WASHCOMBO_MINI: WASHER_NUMBERS,
117  DeviceType.WASHER: WASHER_NUMBERS,
118  DeviceType.WASHTOWER_DRYER: WASHER_NUMBERS,
119  DeviceType.WASHTOWER: WASHER_NUMBERS,
120  DeviceType.WASHTOWER_WASHER: WASHER_NUMBERS,
121  DeviceType.WATER_HEATER: (
123  key=ThinQProperty.TARGET_TEMPERATURE,
124  native_max_value=60,
125  native_min_value=35,
126  native_step=1,
127  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
128  translation_key=ThinQProperty.TARGET_TEMPERATURE,
129  ),
130  ),
131  DeviceType.WINE_CELLAR: (
132  NUMBER_DESC[ThinQProperty.LIGHT_STATUS],
133  NUMBER_DESC[ThinQProperty.TARGET_TEMPERATURE],
134  ),
135 }
136 
137 _LOGGER = logging.getLogger(__name__)
138 
139 
141  hass: HomeAssistant,
142  entry: ThinqConfigEntry,
143  async_add_entities: AddEntitiesCallback,
144 ) -> None:
145  """Set up an entry for number platform."""
146  entities: list[ThinQNumberEntity] = []
147  for coordinator in entry.runtime_data.coordinators.values():
148  if (
149  descriptions := DEVICE_TYPE_NUMBER_MAP.get(
150  coordinator.api.device.device_type
151  )
152  ) is not None:
153  for description in descriptions:
154  entities.extend(
155  ThinQNumberEntity(coordinator, description, property_id)
156  for property_id in coordinator.api.get_active_idx(
157  description.key, ActiveMode.READ_WRITE
158  )
159  )
160 
161  if entities:
162  async_add_entities(entities)
163 
164 
166  """Represent a thinq number platform."""
167 
168  _attr_mode = NumberMode.BOX
169 
170  def _update_status(self) -> None:
171  """Update status itself."""
172  super()._update_status()
173 
174  self._attr_native_value_attr_native_value = self.datadatadatadata.value
175 
176  # Update unit.
177  if (
178  unit_of_measurement := self._get_unit_of_measurement_get_unit_of_measurement(self.datadatadatadata.unit)
179  ) is not None:
180  self._attr_native_unit_of_measurement_attr_native_unit_of_measurement = unit_of_measurement
181 
182  # Undate range.
183  if (
184  self.entity_descriptionentity_description.native_min_value is None
185  and (min_value := self.datadatadatadata.min) is not None
186  ):
187  self._attr_native_min_value_attr_native_min_value = min_value
188 
189  if (
190  self.entity_descriptionentity_description.native_max_value is None
191  and (max_value := self.datadatadatadata.max) is not None
192  ):
193  self._attr_native_max_value_attr_native_max_value = max_value
194 
195  if (
196  self.entity_descriptionentity_description.native_step is None
197  and (step := self.datadatadatadata.step) is not None
198  ):
199  self._attr_native_step_attr_native_step = step
200 
201  _LOGGER.debug(
202  "[%s:%s] update status: %s -> %s, unit:%s, min:%s, max:%s, step:%s",
203  self.coordinator.device_name,
204  self.property_idproperty_id,
205  self.datadatadatadata.value,
206  self.native_valuenative_value,
207  self.native_unit_of_measurementnative_unit_of_measurement,
208  self.native_min_valuenative_min_value,
209  self.native_max_valuenative_max_value,
210  self.native_stepnative_step,
211  )
212 
213  async def async_set_native_value(self, value: float) -> None:
214  """Change to new number value."""
215  if self.stepstep.is_integer():
216  value = int(value)
217  _LOGGER.debug(
218  "[%s:%s] async_set_native_value: %s",
219  self.coordinator.device_name,
220  self.property_idproperty_id,
221  value,
222  )
223 
224  await self.async_call_apiasync_call_api(self.coordinator.api.post(self.property_idproperty_id, value))
None async_call_api(self, Coroutine[Any, Any, Any] target, Callable[[], None]|None on_fail_method=None)
Definition: entity.py:101
str|None _get_unit_of_measurement(self, str|None unit)
Definition: entity.py:73
None async_setup_entry(HomeAssistant hass, ThinqConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:144