Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Melnor integration models."""
2 
3 from collections.abc import Callable
4 
5 from melnor_bluetooth.device import Device, Valve
6 
7 from homeassistant.components.number import EntityDescription
8 from homeassistant.core import callback
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.update_coordinator import CoordinatorEntity
11 
12 from .const import DOMAIN
13 from .coordinator import MelnorDataUpdateCoordinator
14 
15 
16 class MelnorBluetoothEntity(CoordinatorEntity[MelnorDataUpdateCoordinator]):
17  """Base class for melnor entities."""
18 
19  _device: Device
20  _attr_has_entity_name = True
21 
22  def __init__(
23  self,
24  coordinator: MelnorDataUpdateCoordinator,
25  ) -> None:
26  """Initialize a melnor base entity."""
27  super().__init__(coordinator)
28 
29  self._device_device_device = coordinator.data
30 
31  self._attr_device_info_attr_device_info = DeviceInfo(
32  identifiers={(DOMAIN, self._device_device_device.mac)},
33  manufacturer="Melnor",
34  model=self._device_device_device.model,
35  name=self._device_device_device.name,
36  )
37 
38  @callback
39  def _handle_coordinator_update(self) -> None:
40  """Handle updated data from the coordinator."""
41  self._device_device_device = self.coordinator.data
42  self.async_write_ha_state()
43 
44  @property
45  def available(self) -> bool:
46  """Return True if entity is available."""
47  return self._device_device_device.is_connected
48 
49 
51  """Base class for valves that define themselves as child devices."""
52 
53  _valve: Valve
54 
55  def __init__(
56  self,
57  coordinator: MelnorDataUpdateCoordinator,
58  entity_description: EntityDescription,
59  valve: Valve,
60  ) -> None:
61  """Initialize a valve entity."""
62  super().__init__(coordinator)
63 
64  self._attr_unique_id_attr_unique_id = (
65  f"{self._device.mac}-zone{valve.id}-{entity_description.key}"
66  )
67  self.entity_descriptionentity_description = entity_description
68 
69  self._valve_valve = valve
70 
72  identifiers={(DOMAIN, f"{self._device.mac}-zone{self._valve.id}")},
73  manufacturer="Melnor",
74  name=f"Zone {valve.id + 1}",
75  via_device=(DOMAIN, self._device_device_device.mac),
76  )
77 
78 
79 def get_entities_for_valves[_T: EntityDescription](
80  coordinator: MelnorDataUpdateCoordinator,
81  descriptions: list[_T],
82  function: Callable[
83  [Valve, _T],
84  CoordinatorEntity[MelnorDataUpdateCoordinator],
85  ],
86 ) -> list[CoordinatorEntity[MelnorDataUpdateCoordinator]]:
87  """Get descriptions for valves."""
88  entities: list[CoordinatorEntity[MelnorDataUpdateCoordinator]] = []
89 
90  # This device may not have 4 valves total, but the library will only expose the right number of valves
91  for i in range(1, 5):
92  valve = coordinator.data[f"zone{i}"]
93 
94  if valve is not None:
95  entities.extend(
96  function(valve, description) for description in descriptions
97  )
98 
99  return entities
None __init__(self, MelnorDataUpdateCoordinator coordinator)
Definition: entity.py:25
None __init__(self, MelnorDataUpdateCoordinator coordinator, EntityDescription entity_description, Valve valve)
Definition: entity.py:60