Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base entity for Traccar Server."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from pytraccar import DeviceModel, GeofenceModel, PositionModel
8 
9 from homeassistant.helpers.device_registry import DeviceInfo
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.update_coordinator import CoordinatorEntity
12 
13 from .const import DOMAIN
14 from .coordinator import TraccarServerCoordinator
15 
16 
17 class TraccarServerEntity(CoordinatorEntity[TraccarServerCoordinator]):
18  """Base entity for Traccar Server."""
19 
20  def __init__(
21  self,
22  coordinator: TraccarServerCoordinator,
23  device: DeviceModel,
24  ) -> None:
25  """Initialize the Traccar Server entity."""
26  super().__init__(coordinator)
27  self.device_iddevice_id = device["id"]
28  self._attr_device_info_attr_device_info = DeviceInfo(
29  identifiers={(DOMAIN, device["uniqueId"])},
30  model=device["model"],
31  name=device["name"],
32  )
33  self._attr_unique_id_attr_unique_id = device["uniqueId"]
34 
35  @property
36  def available(self) -> bool:
37  """Return True if entity is available."""
38  return bool(self.coordinator.data and self.device_iddevice_id in self.coordinator.data)
39 
40  @property
41  def traccar_device(self) -> DeviceModel:
42  """Return the device."""
43  return self.coordinator.data[self.device_iddevice_id]["device"]
44 
45  @property
46  def traccar_geofence(self) -> GeofenceModel | None:
47  """Return the geofence."""
48  return self.coordinator.data[self.device_iddevice_id]["geofence"]
49 
50  @property
51  def traccar_position(self) -> PositionModel:
52  """Return the position."""
53  return self.coordinator.data[self.device_iddevice_id]["position"]
54 
55  @property
56  def traccar_attributes(self) -> dict[str, Any]:
57  """Return the attributes."""
58  return self.coordinator.data[self.device_iddevice_id]["attributes"]
59 
60  async def async_added_to_hass(self) -> None:
61  """Entity added to hass."""
62  self.async_on_remove(
64  self.hasshass,
65  f"{DOMAIN}_{self.device_id}",
66  self.async_write_ha_state,
67  )
68  )
69  await super().async_added_to_hass()
None __init__(self, TraccarServerCoordinator coordinator, DeviceModel device)
Definition: entity.py:24
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103