Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base classes for SmartTub entities."""
2 
3 import smarttub
4 
5 from homeassistant.helpers.device_registry import DeviceInfo
7  CoordinatorEntity,
8  DataUpdateCoordinator,
9 )
10 
11 from .const import DOMAIN
12 from .helpers import get_spa_name
13 
14 
16  """Base class for SmartTub entities."""
17 
18  def __init__(
19  self, coordinator: DataUpdateCoordinator, spa: smarttub.Spa, entity_name
20  ) -> None:
21  """Initialize the entity.
22 
23  Given a spa id and a short name for the entity, we provide basic device
24  info, name, unique id, etc. for all derived entities.
25  """
26 
27  super().__init__(coordinator)
28  self.spaspa = spa
29  self._attr_unique_id_attr_unique_id = f"{spa.id}-{entity_name}"
30  self._attr_device_info_attr_device_info = DeviceInfo(
31  identifiers={(DOMAIN, spa.id)},
32  manufacturer=spa.brand,
33  model=spa.model,
34  )
35  spa_name = get_spa_name(self.spaspa)
36  self._attr_name_attr_name = f"{spa_name} {entity_name}"
37 
38  @property
39  def spa_status(self) -> smarttub.SpaState:
40  """Retrieve the result of Spa.get_status()."""
41 
42  return self.coordinator.data[self.spaspa.id].get("status")
43 
44 
46  """Base class for SmartTub sensors."""
47 
48  def __init__(self, coordinator, spa, sensor_name, state_key):
49  """Initialize the entity."""
50  super().__init__(coordinator, spa, sensor_name)
51  self._state_key_state_key = state_key
52 
53  @property
54  def _state(self):
55  """Retrieve the underlying state from the spa."""
56  return getattr(self.spa_statusspa_status, self._state_key_state_key)
None __init__(self, DataUpdateCoordinator coordinator, smarttub.Spa spa, entity_name)
Definition: entity.py:20
def __init__(self, coordinator, spa, sensor_name, state_key)
Definition: entity.py:48
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
str get_spa_name(smarttub.Spa spa)
Definition: helpers.py:6