Home Assistant Unofficial Reference 2024.12.1
device_tracker.py
Go to the documentation of this file.
1 """Device tracker for Mobile app."""
2 
4  ATTR_BATTERY,
5  ATTR_GPS,
6  ATTR_GPS_ACCURACY,
7  ATTR_LOCATION_NAME,
8  TrackerEntity,
9 )
10 from homeassistant.config_entries import ConfigEntry
11 from homeassistant.const import (
12  ATTR_BATTERY_LEVEL,
13  ATTR_DEVICE_ID,
14  ATTR_LATITUDE,
15  ATTR_LONGITUDE,
16 )
17 from homeassistant.core import HomeAssistant, callback
18 from homeassistant.helpers.dispatcher import async_dispatcher_connect
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.restore_state import RestoreEntity
21 
22 from .const import (
23  ATTR_ALTITUDE,
24  ATTR_COURSE,
25  ATTR_DEVICE_NAME,
26  ATTR_SPEED,
27  ATTR_VERTICAL_ACCURACY,
28  SIGNAL_LOCATION_UPDATE,
29 )
30 from .helpers import device_info
31 
32 ATTR_KEYS = (ATTR_ALTITUDE, ATTR_COURSE, ATTR_SPEED, ATTR_VERTICAL_ACCURACY)
33 
34 
36  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
37 ) -> None:
38  """Set up Mobile app based off an entry."""
39  entity = MobileAppEntity(entry)
40  async_add_entities([entity])
41 
42 
43 class MobileAppEntity(TrackerEntity, RestoreEntity):
44  """Represent a tracked device."""
45 
46  def __init__(self, entry, data=None):
47  """Set up Mobile app entity."""
48  self._entry_entry = entry
49  self._data_data = data
50  self._dispatch_unsub_dispatch_unsub = None
51 
52  @property
53  def unique_id(self):
54  """Return the unique ID."""
55  return self._entry_entry.data[ATTR_DEVICE_ID]
56 
57  @property
58  def battery_level(self):
59  """Return the battery level of the device."""
60  return self._data_data.get(ATTR_BATTERY)
61 
62  @property
64  """Return device specific attributes."""
65  attrs = {}
66  for key in ATTR_KEYS:
67  if (value := self._data_data.get(key)) is not None:
68  attrs[key] = value
69 
70  return attrs
71 
72  @property
73  def location_accuracy(self):
74  """Return the gps accuracy of the device."""
75  return self._data_data.get(ATTR_GPS_ACCURACY)
76 
77  @property
78  def latitude(self):
79  """Return latitude value of the device."""
80  if (gps := self._data_data.get(ATTR_GPS)) is None:
81  return None
82 
83  return gps[0]
84 
85  @property
86  def longitude(self):
87  """Return longitude value of the device."""
88  if (gps := self._data_data.get(ATTR_GPS)) is None:
89  return None
90 
91  return gps[1]
92 
93  @property
94  def location_name(self):
95  """Return a location name for the current location of the device."""
96  if location_name := self._data_data.get(ATTR_LOCATION_NAME):
97  return location_name
98  return None
99 
100  @property
101  def name(self):
102  """Return the name of the device."""
103  return self._entry_entry.data[ATTR_DEVICE_NAME]
104 
105  @property
106  def device_info(self):
107  """Return the device info."""
108  return device_info(self._entry_entry.data)
109 
110  async def async_added_to_hass(self) -> None:
111  """Call when entity about to be added to Home Assistant."""
112  await super().async_added_to_hass()
113  self._dispatch_unsub_dispatch_unsub = async_dispatcher_connect(
114  self.hasshass,
115  SIGNAL_LOCATION_UPDATE.format(self._entry_entry.entry_id),
116  self.update_dataupdate_data,
117  )
118 
119  # Don't restore if we got set up with data.
120  if self._data_data is not None:
121  return
122 
123  if (state := await self.async_get_last_stateasync_get_last_state()) is None:
124  self._data_data = {}
125  return
126 
127  attr = state.attributes
128  data = {
129  ATTR_GPS: (attr.get(ATTR_LATITUDE), attr.get(ATTR_LONGITUDE)),
130  ATTR_GPS_ACCURACY: attr.get(ATTR_GPS_ACCURACY),
131  ATTR_BATTERY: attr.get(ATTR_BATTERY_LEVEL),
132  }
133  data.update({key: attr[key] for key in attr if key in ATTR_KEYS})
134  self._data_data = data
135 
136  async def async_will_remove_from_hass(self) -> None:
137  """Call when entity is being removed from hass."""
138  await super().async_will_remove_from_hass()
139 
140  if self._dispatch_unsub_dispatch_unsub:
141  self._dispatch_unsub_dispatch_unsub()
142  self._dispatch_unsub_dispatch_unsub = None
143 
144  @callback
145  def update_data(self, data):
146  """Mark the device as seen."""
147  self._data_data = data
148  self.async_write_ha_stateasync_write_ha_state()
web.Response get(self, web.Request request, str config_key)
Definition: view.py:88
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103