Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """A entity class for Tractive integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.core import callback
8 from homeassistant.helpers.device_registry import DeviceInfo
9 from homeassistant.helpers.dispatcher import async_dispatcher_connect
10 from homeassistant.helpers.entity import Entity
11 
12 from . import TractiveClient
13 from .const import DOMAIN, SERVER_UNAVAILABLE
14 
15 
17  """Tractive entity class."""
18 
19  _attr_has_entity_name = True
20 
21  def __init__(
22  self,
23  client: TractiveClient,
24  trackable: dict[str, Any],
25  tracker_details: dict[str, Any],
26  dispatcher_signal: str,
27  ) -> None:
28  """Initialize tracker entity."""
29  self._attr_device_info_attr_device_info = DeviceInfo(
30  configuration_url="https://my.tractive.com/",
31  identifiers={(DOMAIN, tracker_details["_id"])},
32  name=trackable["details"]["name"],
33  manufacturer="Tractive GmbH",
34  sw_version=tracker_details["fw_version"],
35  model=tracker_details["model_number"],
36  )
37  self._user_id_user_id = client.user_id
38  self._tracker_id_tracker_id = tracker_details["_id"]
39  self._client_client = client
40  self._dispatcher_signal_dispatcher_signal = dispatcher_signal
41 
42  async def async_added_to_hass(self) -> None:
43  """Handle entity which will be added."""
44  if not self._client_client.subscribed:
45  self._client_client.subscribe()
46 
47  self.async_on_removeasync_on_remove(
49  self.hasshass,
50  self._dispatcher_signal_dispatcher_signal,
51  self.handle_status_updatehandle_status_update,
52  )
53  )
54 
55  self.async_on_removeasync_on_remove(
57  self.hasshass,
58  f"{SERVER_UNAVAILABLE}-{self._user_id}",
59  self.handle_server_unavailablehandle_server_unavailable,
60  )
61  )
62 
63  @callback
64  def handle_status_update(self, event: dict[str, Any]) -> None:
65  """Handle status update."""
66  self._attr_available_attr_available = event[self.entity_description.key] is not None
67  self.async_write_ha_stateasync_write_ha_state()
68 
69  @callback
70  def handle_server_unavailable(self) -> None:
71  """Handle server unavailable."""
72  self._attr_available_attr_available = False
73  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, TractiveClient client, dict[str, Any] trackable, dict[str, Any] tracker_details, str dispatcher_signal)
Definition: entity.py:27
None handle_status_update(self, dict[str, Any] event)
Definition: entity.py:64
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] subscribe(HomeAssistant hass, str topic, MessageCallbackType msg_callback, int qos=DEFAULT_QOS, str encoding="utf-8")
Definition: client.py:247
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103