2019-02-13 20:21:14 +00:00
|
|
|
"""Support for tracking Tesla cars."""
|
2017-08-31 04:13:02 +00:00
|
|
|
import logging
|
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
from homeassistant.helpers.event import async_track_utc_time_change
|
2017-08-31 04:13:02 +00:00
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DOMAIN as TESLA_DOMAIN
|
|
|
|
|
2017-08-31 04:13:02 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
|
2017-08-31 04:13:02 +00:00
|
|
|
"""Set up the Tesla tracker."""
|
2019-11-15 04:15:58 +00:00
|
|
|
tracker = TeslaDeviceTracker(
|
|
|
|
hass, config, async_see, hass.data[TESLA_DOMAIN]["devices"]["devices_tracker"]
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-11-15 04:15:58 +00:00
|
|
|
await tracker.update_info()
|
|
|
|
async_track_utc_time_change(hass, tracker.update_info, second=range(0, 60, 30))
|
2017-08-31 04:13:02 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class TeslaDeviceTracker:
|
2017-08-31 04:13:02 +00:00
|
|
|
"""A class representing a Tesla device."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config, see, tesla_devices):
|
|
|
|
"""Initialize the Tesla device scanner."""
|
|
|
|
self.hass = hass
|
|
|
|
self.see = see
|
|
|
|
self.devices = tesla_devices
|
|
|
|
|
2019-11-15 04:15:58 +00:00
|
|
|
async def update_info(self, now=None):
|
2017-08-31 04:13:02 +00:00
|
|
|
"""Update the device info."""
|
|
|
|
for device in self.devices:
|
2019-11-15 04:15:58 +00:00
|
|
|
await device.async_update()
|
2017-08-31 04:13:02 +00:00
|
|
|
name = device.name
|
|
|
|
_LOGGER.debug("Updating device position: %s", name)
|
|
|
|
dev_id = slugify(device.uniq_name)
|
|
|
|
location = device.get_location()
|
2018-03-04 10:35:38 +00:00
|
|
|
if location:
|
2019-07-31 19:25:30 +00:00
|
|
|
lat = location["latitude"]
|
|
|
|
lon = location["longitude"]
|
|
|
|
attrs = {"trackr_id": dev_id, "id": dev_id, "name": name}
|
2019-11-15 04:15:58 +00:00
|
|
|
await self.see(
|
2019-07-31 19:25:30 +00:00
|
|
|
dev_id=dev_id, host_name=name, gps=(lat, lon), attributes=attrs
|
2018-03-04 10:35:38 +00:00
|
|
|
)
|