forked from nyxnyx/onstar_component
-
Notifications
You must be signed in to change notification settings - Fork 0
/
device_tracker.py
53 lines (41 loc) · 1.51 KB
/
device_tracker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import logging
_LOGGER = logging.getLogger(__name__)
from homeassistant.util import slugify
from homeassistant.helpers.event import track_utc_time_change
from . import DOMAIN
def setup_scanner(hass, config, see, discovery_info=None):
data = hass.data[DOMAIN]
tracker = OnstarDeviceTracker(see, data)
_LOGGER.info("onstar device_tracker set-up")
tracker.setup(hass)
return True
class OnstarDeviceTracker:
"""BMW Connected Drive device tracker."""
def __init__(self, see, data):
"""Initialize the Tracker."""
self._see = see
self._data = data
def setup(self, hass):
"""Set up a timer and start gathering devices."""
self.update()
track_utc_time_change(
hass, lambda now: self.update(), second=range(0, 60, 30)
)
def update(self) -> None:
"""Update the device info.
Only update the state in home assistant if tracking in
the car is enabled.
"""
dev_id = slugify(self._data.status['onstar.plate'])
if self._data._pin is None:
_LOGGER.debug("Tracking is disabled for vehicle %s", dev_id)
return
_LOGGER.info("Updating %s", dev_id)
attrs = {"vin": self._data.status['onstar.vin']}
self._see(
dev_id=dev_id,
host_name=self._data.status['onstar.plate'],
gps=self._data.gps_position,
attributes=attrs,
icon="mdi:car",
)