Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base entity for Withings."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiowithings import Device
8 
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 (
14  WithingsDataUpdateCoordinator,
15  WithingsDeviceDataUpdateCoordinator,
16 )
17 
18 
19 class WithingsEntity[_T: WithingsDataUpdateCoordinator[Any]](CoordinatorEntity[_T]):
20  """Base class for withings entities."""
21 
22  _attr_has_entity_name = True
23 
24  def __init__(
25  self,
26  coordinator: _T,
27  key: str,
28  ) -> None:
29  """Initialize the Withings entity."""
30  super().__init__(coordinator)
31  self._attr_unique_id = f"withings_{coordinator.config_entry.unique_id}_{key}"
32  self._attr_device_info = DeviceInfo(
33  identifiers={(DOMAIN, str(coordinator.config_entry.unique_id))},
34  manufacturer="Withings",
35  )
36 
37 
38 class WithingsDeviceEntity(WithingsEntity[WithingsDeviceDataUpdateCoordinator]):
39  """Base class for withings device entities."""
40 
41  def __init__(
42  self,
43  coordinator: WithingsDeviceDataUpdateCoordinator,
44  device_id: str,
45  key: str,
46  ) -> None:
47  """Initialize the Withings entity."""
48  super().__init__(coordinator, key)
49  self._attr_unique_id_attr_unique_id = f"{device_id}_{key}"
50  self.device_iddevice_id = device_id
51  self._attr_device_info_attr_device_info = DeviceInfo(
52  identifiers={(DOMAIN, device_id)},
53  manufacturer="Withings",
54  name=self.devicedevice.raw_model,
55  model=self.devicedevice.raw_model,
56  via_device=(DOMAIN, str(coordinator.config_entry.unique_id)),
57  )
58 
59  @property
60  def available(self) -> bool:
61  """Return True if entity is available."""
62  return super().available and self.device_iddevice_id in self.coordinator.data
63 
64  @property
65  def device(self) -> Device:
66  """Return the Withings device."""
67  return self.coordinator.data[self.device_iddevice_id]
None __init__(self, WithingsDeviceDataUpdateCoordinator coordinator, str device_id, str key)
Definition: entity.py:46
None __init__(self, _T coordinator, str key)
Definition: entity.py:28