Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Entity for the opengarage.io component."""
2 
3 from __future__ import annotations
4 
5 from homeassistant.core import callback
6 from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC, DeviceInfo
7 from homeassistant.helpers.entity import EntityDescription
8 from homeassistant.helpers.update_coordinator import CoordinatorEntity
9 
10 from .const import DOMAIN
11 from .coordinator import OpenGarageDataUpdateCoordinator
12 
13 
14 class OpenGarageEntity(CoordinatorEntity[OpenGarageDataUpdateCoordinator]):
15  """Representation of a OpenGarage entity."""
16 
17  _attr_has_entity_name = True
18 
19  def __init__(
20  self,
21  open_garage_data_coordinator: OpenGarageDataUpdateCoordinator,
22  device_id: str,
23  description: EntityDescription | None = None,
24  ) -> None:
25  """Initialize the entity."""
26  super().__init__(open_garage_data_coordinator)
27 
28  if description is not None:
29  self.entity_descriptionentity_description = description
30  self._attr_unique_id_attr_unique_id = f"{device_id}_{description.key}"
31  else:
32  self._attr_unique_id_attr_unique_id = device_id
33 
34  self._device_id_device_id = device_id
35  self._update_attr_update_attr()
36 
37  @callback
38  def _update_attr(self) -> None:
39  """Update the state and attributes."""
40 
41  @callback
42  def _handle_coordinator_update(self) -> None:
43  """Handle updated data from the coordinator."""
44  self._update_attr_update_attr()
45  self.async_write_ha_state()
46 
47  @property
48  def device_info(self) -> DeviceInfo:
49  """Return the device_info of the device."""
50  return DeviceInfo(
51  configuration_url=self.coordinator.open_garage_connection.device_url,
52  connections={(CONNECTION_NETWORK_MAC, self.coordinator.data["mac"])},
53  identifiers={(DOMAIN, self._device_id_device_id)},
54  manufacturer="Open Garage",
55  name=self.coordinator.data["name"],
56  suggested_area="Garage",
57  sw_version=self.coordinator.data["fwv"],
58  )
None __init__(self, OpenGarageDataUpdateCoordinator open_garage_data_coordinator, str device_id, EntityDescription|None description=None)
Definition: entity.py:24