2019-04-03 15:40:03 +00:00
|
|
|
"""Support for the TrackR platform."""
|
2017-01-12 23:16:05 +00:00
|
|
|
import logging
|
|
|
|
|
2019-11-26 18:22:28 +00:00
|
|
|
from pytrackr.api import trackrApiInterface
|
2017-01-12 23:16:05 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-05-21 11:08:40 +00:00
|
|
|
from homeassistant.components.device_tracker import (
|
|
|
|
PLATFORM_SCHEMA as PARENT_PLATFORM_SCHEMA,
|
|
|
|
)
|
2019-11-26 18:22:28 +00:00
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2017-01-12 23:16:05 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.event import track_utc_time_change
|
2017-02-17 20:39:05 +00:00
|
|
|
from homeassistant.util import slugify
|
2017-01-12 23:16:05 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2021-05-21 11:08:40 +00:00
|
|
|
PLATFORM_SCHEMA = PARENT_PLATFORM_SCHEMA.extend(
|
2019-07-31 19:25:30 +00:00
|
|
|
{vol.Required(CONF_USERNAME): cv.string, vol.Required(CONF_PASSWORD): cv.string}
|
|
|
|
)
|
2017-01-12 23:16:05 +00:00
|
|
|
|
|
|
|
|
2017-02-07 19:47:11 +00:00
|
|
|
def setup_scanner(hass, config: dict, see, discovery_info=None):
|
2017-01-12 23:16:05 +00:00
|
|
|
"""Validate the configuration and return a TrackR scanner."""
|
|
|
|
TrackRDeviceScanner(hass, config, see)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class TrackRDeviceScanner:
|
2017-01-12 23:16:05 +00:00
|
|
|
"""A class representing a TrackR device."""
|
|
|
|
|
|
|
|
def __init__(self, hass, config: dict, see) -> None:
|
|
|
|
"""Initialize the TrackR device scanner."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2017-01-12 23:16:05 +00:00
|
|
|
self.hass = hass
|
2017-04-30 05:04:49 +00:00
|
|
|
self.api = trackrApiInterface(
|
2019-07-31 19:25:30 +00:00
|
|
|
config.get(CONF_USERNAME), config.get(CONF_PASSWORD)
|
|
|
|
)
|
2017-01-12 23:16:05 +00:00
|
|
|
self.see = see
|
|
|
|
self.devices = self.api.get_trackrs()
|
|
|
|
self._update_info()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
track_utc_time_change(self.hass, self._update_info, second=range(0, 60, 30))
|
2017-01-12 23:16:05 +00:00
|
|
|
|
|
|
|
def _update_info(self, now=None) -> None:
|
|
|
|
"""Update the device info."""
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.debug("Updating devices %s", now)
|
2017-01-12 23:16:05 +00:00
|
|
|
|
|
|
|
# Update self.devices to collect new devices added
|
|
|
|
# to the users account.
|
|
|
|
self.devices = self.api.get_trackrs()
|
|
|
|
|
|
|
|
for trackr in self.devices:
|
|
|
|
trackr.update_state()
|
|
|
|
trackr_id = trackr.tracker_id()
|
|
|
|
trackr_device_id = trackr.id()
|
|
|
|
lost = trackr.lost()
|
2017-02-17 20:39:05 +00:00
|
|
|
dev_id = slugify(trackr.name())
|
2017-01-12 23:16:05 +00:00
|
|
|
if dev_id is None:
|
|
|
|
dev_id = trackr_id
|
|
|
|
location = trackr.last_known_location()
|
2019-07-31 19:25:30 +00:00
|
|
|
lat = location["latitude"]
|
|
|
|
lon = location["longitude"]
|
2017-01-12 23:16:05 +00:00
|
|
|
|
|
|
|
attrs = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"last_updated": trackr.last_updated(),
|
|
|
|
"last_seen": trackr.last_time_seen(),
|
|
|
|
"trackr_id": trackr_id,
|
|
|
|
"id": trackr_device_id,
|
|
|
|
"lost": lost,
|
|
|
|
"battery_level": trackr.battery_level(),
|
2017-01-12 23:16:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self.see(dev_id=dev_id, gps=(lat, lon), attributes=attrs)
|