Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Adapter to wrap the rachiopy api for home assistant."""
2 
3 from abc import abstractmethod
4 from typing import Any
5 
6 from homeassistant.core import callback
7 from homeassistant.helpers import device_registry as dr
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.entity import Entity
10 from homeassistant.helpers.update_coordinator import CoordinatorEntity
11 
12 from .const import (
13  DEFAULT_NAME,
14  DOMAIN,
15  KEY_CONNECTED,
16  KEY_ID,
17  KEY_NAME,
18  KEY_REPORTED_STATE,
19  KEY_STATE,
20 )
21 from .coordinator import RachioUpdateCoordinator
22 from .device import RachioIro
23 
24 
26  """Base class for rachio devices."""
27 
28  _attr_should_poll = False
29 
30  def __init__(self, controller: RachioIro) -> None:
31  """Initialize a Rachio device."""
32  super().__init__()
33  self._controller_controller = controller
34  self._attr_device_info_attr_device_info = DeviceInfo(
35  identifiers={
36  (
37  DOMAIN,
38  self._controller_controller.serial_number,
39  )
40  },
41  connections={
42  (
43  dr.CONNECTION_NETWORK_MAC,
44  self._controller_controller.mac_address,
45  )
46  },
47  name=self._controller_controller.name,
48  model=self._controller_controller.model,
49  manufacturer=DEFAULT_NAME,
50  configuration_url="https://app.rach.io",
51  )
52 
53 
54 class RachioHoseTimerEntity(CoordinatorEntity[RachioUpdateCoordinator]):
55  """Base class for smart hose timer entities."""
56 
57  _attr_has_entity_name = True
58 
59  def __init__(
60  self, data: dict[str, Any], coordinator: RachioUpdateCoordinator
61  ) -> None:
62  """Initialize a Rachio smart hose timer entity."""
63  super().__init__(coordinator)
64  self.idid = data[KEY_ID]
65  self._name_name = data[KEY_NAME]
66  self._attr_device_info_attr_device_info = DeviceInfo(
67  identifiers={(DOMAIN, self.idid)},
68  model="Smart Hose Timer",
69  name=self._name_name,
70  manufacturer=DEFAULT_NAME,
71  configuration_url="https://app.rach.io",
72  )
73  self._update_attr_update_attr()
74 
75  @property
76  def available(self) -> bool:
77  """Return if the entity is available."""
78  return (
79  super().available
80  and self.coordinator.data[self.idid][KEY_STATE][KEY_REPORTED_STATE][
81  KEY_CONNECTED
82  ]
83  )
84 
85  @abstractmethod
86  def _update_attr(self) -> None:
87  """Update the state and attributes."""
88 
89  @callback
90  def _handle_coordinator_update(self) -> None:
91  """Handle updated data from the coordinator."""
92  self._update_attr_update_attr()
None __init__(self, RachioIro controller)
Definition: entity.py:30
None __init__(self, dict[str, Any] data, RachioUpdateCoordinator coordinator)
Definition: entity.py:61