Home Assistant Unofficial Reference 2024.12.1
number.py
Go to the documentation of this file.
1 """Number platform for Sensibo integration."""
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 pysensibo.model import SensiboDevice
10 
12  NumberDeviceClass,
13  NumberEntity,
14  NumberEntityDescription,
15 )
16 from homeassistant.const import PERCENTAGE, EntityCategory, UnitOfTemperature
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from . import SensiboConfigEntry
21 from .coordinator import SensiboDataUpdateCoordinator
22 from .entity import SensiboDeviceBaseEntity, async_handle_api_call
23 
24 PARALLEL_UPDATES = 0
25 
26 
27 @dataclass(frozen=True, kw_only=True)
29  """Class describing Sensibo Number entities."""
30 
31  remote_key: str
32  value_fn: Callable[[SensiboDevice], float | None]
33 
34 
35 DEVICE_NUMBER_TYPES = (
37  key="calibration_temp",
38  translation_key="calibration_temperature",
39  device_class=NumberDeviceClass.TEMPERATURE,
40  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
41  remote_key="temperature",
42  entity_category=EntityCategory.CONFIG,
43  entity_registry_enabled_default=False,
44  native_min_value=-10,
45  native_max_value=10,
46  native_step=0.1,
47  value_fn=lambda data: data.calibration_temp,
48  ),
50  key="calibration_hum",
51  translation_key="calibration_humidity",
52  device_class=NumberDeviceClass.HUMIDITY,
53  native_unit_of_measurement=PERCENTAGE,
54  remote_key="humidity",
55  entity_category=EntityCategory.CONFIG,
56  entity_registry_enabled_default=False,
57  native_min_value=-10,
58  native_max_value=10,
59  native_step=0.1,
60  value_fn=lambda data: data.calibration_hum,
61  ),
62 )
63 
64 
66  hass: HomeAssistant,
67  entry: SensiboConfigEntry,
68  async_add_entities: AddEntitiesCallback,
69 ) -> None:
70  """Set up Sensibo number platform."""
71 
72  coordinator = entry.runtime_data
73 
75  SensiboNumber(coordinator, device_id, description)
76  for device_id, device_data in coordinator.data.parsed.items()
77  for description in DEVICE_NUMBER_TYPES
78  )
79 
80 
82  """Representation of a Sensibo numbers."""
83 
84  entity_description: SensiboNumberEntityDescription
85 
86  def __init__(
87  self,
88  coordinator: SensiboDataUpdateCoordinator,
89  device_id: str,
90  entity_description: SensiboNumberEntityDescription,
91  ) -> None:
92  """Initiate Sensibo Number."""
93  super().__init__(coordinator, device_id)
94  self.entity_descriptionentity_description = entity_description
95  self._attr_unique_id_attr_unique_id = f"{device_id}-{entity_description.key}"
96 
97  @property
98  def native_value(self) -> float | None:
99  """Return the value from coordinator data."""
100  return self.entity_descriptionentity_description.value_fn(self.device_datadevice_data)
101 
102  async def async_set_native_value(self, value: float) -> None:
103  """Set value for calibration."""
104  await self.async_send_api_callasync_send_api_call(key=self.entity_descriptionentity_description.key, value=value)
105 
106  @async_handle_api_call
107  async def async_send_api_call(self, key: str, value: Any) -> bool:
108  """Make service call to api."""
109  data = {self.entity_descriptionentity_description.remote_key: value}
110  result = await self._client_client.async_set_calibration(
111  self._device_id_device_id,
112  data,
113  )
114  return bool(result.get("status") == "success")
bool async_send_api_call(self, str key, Any value)
Definition: number.py:107
None __init__(self, SensiboDataUpdateCoordinator coordinator, str device_id, SensiboNumberEntityDescription entity_description)
Definition: number.py:91
None async_setup_entry(HomeAssistant hass, SensiboConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: number.py:69