Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Generic Plugwise Entity Class."""
2 
3 from __future__ import annotations
4 
5 from plugwise.constants import GwEntityData
6 
7 from homeassistant.const import ATTR_NAME, ATTR_VIA_DEVICE, CONF_HOST
9  CONNECTION_NETWORK_MAC,
10  CONNECTION_ZIGBEE,
11  DeviceInfo,
12 )
13 from homeassistant.helpers.update_coordinator import CoordinatorEntity
14 
15 from .const import DOMAIN
16 from .coordinator import PlugwiseDataUpdateCoordinator
17 
18 
19 class PlugwiseEntity(CoordinatorEntity[PlugwiseDataUpdateCoordinator]):
20  """Represent a PlugWise Entity."""
21 
22  _attr_has_entity_name = True
23 
24  def __init__(
25  self,
26  coordinator: PlugwiseDataUpdateCoordinator,
27  device_id: str,
28  ) -> None:
29  """Initialise the gateway."""
30  super().__init__(coordinator)
31  self._dev_id_dev_id = device_id
32 
33  configuration_url: str | None = None
34  if entry := self.coordinator.config_entry:
35  configuration_url = f"http://{entry.data[CONF_HOST]}"
36 
37  data = coordinator.data.devices[device_id]
38  connections = set()
39  if mac := data.get("mac_address"):
40  connections.add((CONNECTION_NETWORK_MAC, mac))
41  if mac := data.get("zigbee_mac_address"):
42  connections.add((CONNECTION_ZIGBEE, mac))
43 
44  self._attr_device_info_attr_device_info = DeviceInfo(
45  configuration_url=configuration_url,
46  identifiers={(DOMAIN, device_id)},
47  connections=connections,
48  manufacturer=data.get("vendor"),
49  model=data.get("model"),
50  model_id=data.get("model_id"),
51  name=coordinator.data.gateway["smile_name"],
52  sw_version=data.get("firmware"),
53  hw_version=data.get("hardware"),
54  )
55 
56  if device_id != coordinator.data.gateway["gateway_id"]:
57  self._attr_device_info_attr_device_info.update(
58  {
59  ATTR_NAME: data.get("name"),
60  ATTR_VIA_DEVICE: (
61  DOMAIN,
62  str(self.coordinator.data.gateway["gateway_id"]),
63  ),
64  }
65  )
66 
67  @property
68  def available(self) -> bool:
69  """Return if entity is available."""
70  return (
71  self._dev_id_dev_id in self.coordinator.data.devices
72  and ("available" not in self.devicedevice or self.devicedevice["available"] is True)
73  and super().available
74  )
75 
76  @property
77  def device(self) -> GwEntityData:
78  """Return data for this device."""
79  return self.coordinator.data.devices[self._dev_id_dev_id]
80 
81  async def async_added_to_hass(self) -> None:
82  """Subscribe to updates."""
83  self._handle_coordinator_update()
84  await super().async_added_to_hass()
None __init__(self, PlugwiseDataUpdateCoordinator coordinator, str device_id)
Definition: entity.py:28