Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Device tracker platform for Teslemetry integration."""
2 
3 from __future__ import annotations
4 
6 from homeassistant.const import STATE_HOME
7 from homeassistant.core import HomeAssistant
8 from homeassistant.helpers.entity_platform import AddEntitiesCallback
9 
10 from . import TeslemetryConfigEntry
11 from .entity import TeslemetryVehicleEntity
12 from .models import TeslemetryVehicleData
13 
14 PARALLEL_UPDATES = 0
15 
16 
18  hass: HomeAssistant,
19  entry: TeslemetryConfigEntry,
20  async_add_entities: AddEntitiesCallback,
21 ) -> None:
22  """Set up the Teslemetry device tracker platform from a config entry."""
23 
25  klass(vehicle)
26  for klass in (
27  TeslemetryDeviceTrackerLocationEntity,
28  TeslemetryDeviceTrackerRouteEntity,
29  )
30  for vehicle in entry.runtime_data.vehicles
31  )
32 
33 
35  """Base class for Teslemetry tracker entities."""
36 
37  lat_key: str
38  lon_key: str
39 
40  def __init__(
41  self,
42  vehicle: TeslemetryVehicleData,
43  ) -> None:
44  """Initialize the device tracker."""
45  super().__init__(vehicle, self.keykey)
46 
47  def _async_update_attrs(self) -> None:
48  """Update the attributes of the device tracker."""
49 
50  self._attr_available_attr_available = (
51  self.getget(self.lat_key, False) is not None
52  and self.getget(self.lon_key, False) is not None
53  )
54 
55  @property
56  def latitude(self) -> float | None:
57  """Return latitude value of the device."""
58  return self.getget(self.lat_key)
59 
60  @property
61  def longitude(self) -> float | None:
62  """Return longitude value of the device."""
63  return self.getget(self.lon_key)
64 
65 
67  """Vehicle location device tracker class."""
68 
69  key = "location"
70  lat_key = "drive_state_latitude"
71  lon_key = "drive_state_longitude"
72 
73 
75  """Vehicle navigation device tracker class."""
76 
77  key = "route"
78  lat_key = "drive_state_active_route_latitude"
79  lon_key = "drive_state_active_route_longitude"
80 
81  @property
82  def location_name(self) -> str | None:
83  """Return a location name for the current location of the device."""
84  location = self.getget("drive_state_active_route_destination")
85  if location == "Home":
86  return STATE_HOME
87  return location
Any|None get(self, str key, Any|None default=None)
Definition: entity.py:61
Definition: config_entry.py:1
None async_setup_entry(HomeAssistant hass, TeslemetryConfigEntry entry, AddEntitiesCallback async_add_entities)