core/homeassistant/components/device_tracker/owntracks.py

55 lines
1.4 KiB
Python
Raw Normal View History

2015-09-20 07:27:50 +00:00
"""
homeassistant.components.device_tracker.owntracks
2015-09-20 19:13:51 +00:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-09-20 07:27:50 +00:00
OwnTracks platform for the device tracker.
device_tracker:
platform: owntracks
"""
import json
2015-09-21 03:09:53 +00:00
import logging
2015-09-20 07:27:50 +00:00
import homeassistant.components.mqtt as mqtt
DEPENDENCIES = ['mqtt']
LOCATION_TOPIC = 'owntracks/+/+'
def setup_scanner(hass, config, see):
2015-09-20 19:13:51 +00:00
""" Set up a OwnTracksks tracker. """
2015-09-20 07:27:50 +00:00
def owntracks_location_update(topic, payload, qos):
""" MQTT message received. """
2015-09-20 18:46:01 +00:00
# Docs on available data:
# http://owntracks.org/booklet/tech/json/#_typelocation
try:
data = json.loads(payload)
except ValueError:
# If invalid JSON
2015-09-21 03:09:53 +00:00
logging.getLogger(__name__).error(
'Unable to parse payload as JSON: %s', payload)
2015-09-20 18:46:01 +00:00
return
2015-09-21 03:09:53 +00:00
2015-10-03 16:09:31 +00:00
if not isinstance(data, dict) or data.get('_type') != 'location':
2015-09-20 18:46:01 +00:00
return
2015-09-21 03:09:53 +00:00
parts = topic.split('/')
kwargs = {
'dev_id': '{}_{}'.format(parts[1], parts[2]),
'host_name': parts[1],
'gps': (data['lat'], data['lon']),
}
if 'acc' in data:
kwargs['gps_accuracy'] = data['acc']
if 'batt' in data:
kwargs['battery'] = data['batt']
see(**kwargs)
2015-09-20 07:27:50 +00:00
mqtt.subscribe(hass, LOCATION_TOPIC, owntracks_location_update, 1)
return True