/
traccar.py
executable file
·73 lines (65 loc) · 2.95 KB
/
traccar.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#-*- coding: utf-8 -*-
"""
Support for the TRACCAR platform.
"""
import logging
import requests, json
import voluptuous as vol
CONF_HOST = "host"
from homeassistant.components.device_tracker import (
PLATFORM_SCHEMA, ATTR_ATTRIBUTES)
from homeassistant.const import CONF_NAME, CONF_USERNAME, CONF_PASSWORD
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import track_utc_time_change
from homeassistant.util import slugify
_LOGGER = logging.getLogger(__name__)
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_HOST): cv.string
})
def setup_scanner(hass, config: dict, see, discovery_info=None):
"""Validate the configuration and return a Traccar scanner."""
TraccarDeviceScanner(hass, config, see)
return True
class TraccarDeviceScanner(object):
"""A class representing a Traccar device."""
def __init__(self, hass, config: dict, see) -> None:
"""Initialize the Traccar device scanner."""
self.hass = hass
self._host = config.get(CONF_HOST)
self._username = config.get(CONF_USERNAME)
self._password = config.get(CONF_PASSWORD)
self.see = see
self._update_info()
track_utc_time_change(self.hass, self._update_info,
second=range(0, 60, 30))
def _update_info(self, now=None) -> None:
"""Update the device info."""
_LOGGER.debug('Updating devices %s', now)
self._devices = requests.get(self._host + '/api/devices', auth=(self._username, self._password),headers = ({'Accept': 'application/json'}))
self._device_data = json.loads(self._devices.text)
self._positions = requests.get(self._host + '/api/positions', auth=(self._username, self._password), headers = ({'Accept': 'application/json'}))
self._positions_data = json.loads(self._positions.text)
for dev_id in self._device_data:
_id=dev_id['name']
_name = dev_id['name']
for position_id in self._positions_data:
if position_id['id'] == dev_id['positionId']:
_gps=(position_id['latitude'], position_id['longitude'])
_gps_accuracy=position_id['accuracy']
_attrs = {
'totalDistance': position_id['attributes']['totalDistance'],
'protocol': position_id['protocol'],
'speed': position_id['speed'],
'address': position_id['address'],
'status': dev_id['status'],
'lastUpdate': dev_id['lastUpdate']
}
self.see(
dev_id=slugify(_id),
host_name=slugify(_name),
gps=_gps,
gps_accuracy=_gps_accuracy,
attributes=_attrs
)