Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Support for tracking a Volvo."""
2 
3 from __future__ import annotations
4 
5 from volvooncall.dashboard import Instrument
6 
7 from homeassistant.components.device_tracker import TrackerEntity
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.core import HomeAssistant, callback
10 from homeassistant.helpers.dispatcher import async_dispatcher_connect
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 
13 from .const import DOMAIN, VOLVO_DISCOVERY_NEW
14 from .coordinator import VolvoUpdateCoordinator
15 from .entity import VolvoEntity
16 
17 
19  hass: HomeAssistant,
20  config_entry: ConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Configure device_trackers from a config entry created in the integrations UI."""
24  coordinator: VolvoUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
25  volvo_data = coordinator.volvo_data
26 
27  @callback
28  def async_discover_device(instruments: list[Instrument]) -> None:
29  """Discover and add a discovered Volvo On Call device tracker."""
32  instrument.vehicle.vin,
33  instrument.component,
34  instrument.attr,
35  instrument.slug_attr,
36  coordinator,
37  )
38  for instrument in instruments
39  if instrument.component == "device_tracker"
40  )
41 
42  async_discover_device([*volvo_data.instruments])
43 
44  config_entry.async_on_unload(
45  async_dispatcher_connect(hass, VOLVO_DISCOVERY_NEW, async_discover_device)
46  )
47 
48 
49 class VolvoTrackerEntity(VolvoEntity, TrackerEntity):
50  """A tracked Volvo vehicle."""
51 
52  @property
53  def latitude(self) -> float | None:
54  """Return latitude value of the device."""
55  latitude, _ = self._get_pos_get_pos()
56  return latitude
57 
58  @property
59  def longitude(self) -> float | None:
60  """Return longitude value of the device."""
61  _, longitude = self._get_pos_get_pos()
62  return longitude
63 
64  def _get_pos(self) -> tuple[float, float]:
65  volvo_data = self.coordinator.volvo_data
66  instrument = volvo_data.instrument(
67  self.vinvin, self.componentcomponent, self.attributeattribute, self.slug_attrslug_attr
68  )
69 
70  latitude, longitude, _, _, _ = instrument.state
71 
72  return (float(latitude), float(longitude))
ElkSystem|None async_discover_device(HomeAssistant hass, str host)
Definition: discovery.py:78
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103