Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Entity for UPnP/IGD."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from homeassistant.helpers.device_registry import DeviceInfo
8 from homeassistant.helpers.entity import EntityDescription
9 from homeassistant.helpers.update_coordinator import CoordinatorEntity
10 
11 from .coordinator import UpnpDataUpdateCoordinator
12 
13 
14 @dataclass(frozen=True)
16  """UPnP entity description."""
17 
18  unique_id: str | None = None
19  value_key: str | None = None
20 
21  def __post_init__(self):
22  """Post initialize."""
23  object.__setattr__(self, "value_key", self.value_key or self.key)
24 
25 
26 class UpnpEntity(CoordinatorEntity[UpnpDataUpdateCoordinator]):
27  """Base class for UPnP/IGD entities."""
28 
29  entity_description: UpnpEntityDescription
30  _attr_has_entity_name = True
31 
32  def __init__(
33  self,
34  coordinator: UpnpDataUpdateCoordinator,
35  entity_description: UpnpEntityDescription,
36  ) -> None:
37  """Initialize the base entities."""
38  super().__init__(coordinator)
39  self._device_device = coordinator.device
40  self.entity_descriptionentity_description = entity_description
41  self._attr_unique_id_attr_unique_id = f"{coordinator.device.original_udn}_{entity_description.unique_id or entity_description.key}"
42  self._attr_device_info_attr_device_info = DeviceInfo(
43  connections=coordinator.device_entry.connections,
44  name=coordinator.device_entry.name,
45  manufacturer=coordinator.device_entry.manufacturer,
46  model=coordinator.device_entry.model,
47  configuration_url=coordinator.device_entry.configuration_url,
48  )
49 
50  @property
51  def available(self) -> bool:
52  """Return if entity is available."""
53  return super().available and (
54  self.coordinator.data.get(self.entity_descriptionentity_description.key) is not None
55  )
None __init__(self, UpnpDataUpdateCoordinator coordinator, UpnpEntityDescription entity_description)
Definition: entity.py:36