Merge pull request #413 from balloob/owntracks

initial owntracks support
pull/435/head
Paulus Schoutsen 2015-09-20 12:00:11 -07:00
commit 98a1addc18
9 changed files with 171 additions and 38 deletions

View File

@ -297,11 +297,15 @@ def process_ha_core_config(hass, config):
else:
_LOGGER.error('Received invalid time zone %s', time_zone_str)
for key, attr in ((CONF_LATITUDE, 'latitude'),
(CONF_LONGITUDE, 'longitude'),
(CONF_NAME, 'location_name')):
for key, attr, typ in ((CONF_LATITUDE, 'latitude', float),
(CONF_LONGITUDE, 'longitude', float),
(CONF_NAME, 'location_name', str)):
if key in config:
setattr(hac, attr, config[key])
try:
setattr(hac, attr, typ(config[key]))
except ValueError:
_LOGGER.error('Received invalid %s value for %s: %s',
typ.__name__, key, attr)
set_time_zone(config.get(CONF_TIME_ZONE))

View File

@ -17,7 +17,12 @@ device_tracker:
# New found devices auto found
track_new_devices: yes
# Maximum distance from home we consider people home
range_home: 100
"""
# pylint: disable=too-many-instance-attributes, too-many-arguments
# pylint: disable=too-many-locals
import csv
from datetime import timedelta
import logging
@ -52,7 +57,7 @@ CONF_TRACK_NEW = "track_new_devices"
DEFAULT_CONF_TRACK_NEW = True
CONF_CONSIDER_HOME = 'consider_home'
DEFAULT_CONF_CONSIDER_HOME = 180 # seconds
DEFAULT_CONSIDER_HOME = 180 # seconds
CONF_SCAN_INTERVAL = "interval_seconds"
DEFAULT_SCAN_INTERVAL = 12
@ -60,6 +65,9 @@ DEFAULT_SCAN_INTERVAL = 12
CONF_AWAY_HIDE = 'hide_if_away'
DEFAULT_AWAY_HIDE = False
CONF_HOME_RANGE = 'home_range'
DEFAULT_HOME_RANGE = 100
SERVICE_SEE = 'see'
ATTR_LATITUDE = 'latitude'
@ -69,6 +77,7 @@ ATTR_DEV_ID = 'dev_id'
ATTR_HOST_NAME = 'host_name'
ATTR_LOCATION_NAME = 'location_name'
ATTR_GPS = 'gps'
ATTR_BATTERY = 'battery'
DISCOVERY_PLATFORMS = {
discovery.SERVICE_NETGEAR: 'netgear',
@ -106,13 +115,17 @@ def setup(hass, config):
os.remove(csv_path)
conf = config.get(DOMAIN, {})
consider_home = util.convert(conf.get(CONF_CONSIDER_HOME), int,
DEFAULT_CONF_CONSIDER_HOME)
consider_home = timedelta(
seconds=util.convert(conf.get(CONF_CONSIDER_HOME), int,
DEFAULT_CONSIDER_HOME))
track_new = util.convert(conf.get(CONF_TRACK_NEW), bool,
DEFAULT_CONF_TRACK_NEW)
home_range = util.convert(conf.get(CONF_HOME_RANGE), int,
DEFAULT_HOME_RANGE)
devices = load_config(yaml_path, hass, timedelta(seconds=consider_home))
tracker = DeviceTracker(hass, consider_home, track_new, devices)
devices = load_config(yaml_path, hass, consider_home, home_range)
tracker = DeviceTracker(hass, consider_home, track_new, home_range,
devices)
def setup_platform(p_type, p_config, disc_info=None):
""" Setup a device tracker platform. """
@ -168,12 +181,13 @@ def setup(hass, config):
class DeviceTracker(object):
""" Track devices """
def __init__(self, hass, consider_home, track_new, devices):
def __init__(self, hass, consider_home, track_new, home_range, devices):
self.hass = hass
self.devices = {dev.dev_id: dev for dev in devices}
self.mac_to_dev = {dev.mac: dev for dev in devices if dev.mac}
self.consider_home = timedelta(seconds=consider_home)
self.consider_home = consider_home
self.track_new = track_new
self.home_range = home_range
self.lock = threading.Lock()
for device in devices:
@ -205,8 +219,8 @@ class DeviceTracker(object):
# If no device can be found, create it
device = Device(
self.hass, self.consider_home, self.track_new, dev_id, mac,
(host_name or dev_id).replace('_', ' '))
self.hass, self.consider_home, self.home_range, self.track_new,
dev_id, mac, (host_name or dev_id).replace('_', ' '))
self.devices[dev_id] = device
if mac is not None:
self.mac_to_dev[mac] = device
@ -239,19 +253,20 @@ class DeviceTracker(object):
class Device(Entity):
""" Tracked device. """
# pylint: disable=too-many-instance-attributes, too-many-arguments
host_name = None
location_name = None
gps = None
gps_accuracy = 0
last_seen = None
battery = None
# Track if the last update of this device was HOME
last_update_home = False
_state = STATE_NOT_HOME
def __init__(self, hass, consider_home, track, dev_id, mac, name=None,
picture=None, away_hide=False):
def __init__(self, hass, consider_home, home_range, track, dev_id, mac,
name=None, picture=None, away_hide=False):
self.hass = hass
self.entity_id = ENTITY_ID_FORMAT.format(dev_id)
@ -259,6 +274,8 @@ class Device(Entity):
# detected anymore.
self.consider_home = consider_home
# Distance in meters
self.home_range = home_range
# Device ID
self.dev_id = dev_id
self.mac = mac
@ -273,6 +290,13 @@ class Device(Entity):
self.config_picture = picture
self.away_hide = away_hide
@property
def gps_home(self):
""" Return if device is within range of home. """
distance = max(
0, self.hass.config.distance(*self.gps) - self.gps_accuracy)
return self.gps is not None and distance <= self.home_range
@property
def name(self):
""" Returns the name of the entity. """
@ -292,8 +316,11 @@ class Device(Entity):
attr[ATTR_ENTITY_PICTURE] = self.config_picture
if self.gps:
attr[ATTR_LATITUDE] = self.gps[0],
attr[ATTR_LONGITUDE] = self.gps[1],
attr[ATTR_LATITUDE] = self.gps[0]
attr[ATTR_LONGITUDE] = self.gps[1]
if self.battery:
attr[ATTR_BATTERY] = self.battery
return attr
@ -302,12 +329,23 @@ class Device(Entity):
""" If device should be hidden. """
return self.away_hide and self.state != STATE_HOME
def seen(self, host_name=None, location_name=None, gps=None):
def seen(self, host_name=None, location_name=None, gps=None,
gps_accuracy=0, battery=None):
""" Mark the device as seen. """
self.last_seen = dt_util.utcnow()
self.host_name = host_name
self.location_name = location_name
self.gps = gps
self.gps_accuracy = gps_accuracy
self.battery = battery
if gps is None:
self.gps = None
else:
try:
self.gps = tuple(float(val) for val in gps)
except ValueError:
_LOGGER.warning('Could not parse gps value for %s: %s',
self.dev_id, gps)
self.gps = None
self.update()
def stale(self, now=None):
@ -321,6 +359,8 @@ class Device(Entity):
return
elif self.location_name:
self._state = self.location_name
elif self.gps is not None:
self._state = STATE_HOME if self.gps_home else STATE_NOT_HOME
elif self.stale():
self._state = STATE_NOT_HOME
self.last_update_home = False
@ -338,18 +378,18 @@ def convert_csv_config(csv_path, yaml_path):
(util.slugify(row['name']) or DEVICE_DEFAULT_NAME).lower(),
used_ids)
used_ids.add(dev_id)
device = Device(None, None, row['track'] == '1', dev_id,
device = Device(None, None, None, row['track'] == '1', dev_id,
row['device'], row['name'], row['picture'])
update_config(yaml_path, dev_id, device)
return True
def load_config(path, hass, consider_home):
def load_config(path, hass, consider_home, home_range):
""" Load devices from YAML config file. """
if not os.path.isfile(path):
return []
return [
Device(hass, consider_home, device.get('track', False),
Device(hass, consider_home, home_range, device.get('track', False),
str(dev_id).lower(), str(device.get('mac')).upper(),
device.get('name'), device.get('picture'),
device.get(CONF_AWAY_HIDE, DEFAULT_AWAY_HIDE))

View File

@ -0,0 +1,42 @@
"""
homeassistant.components.device_tracker.owntracks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
OwnTracks platform for the device tracker.
device_tracker:
platform: owntracks
"""
import json
import homeassistant.components.mqtt as mqtt
DEPENDENCIES = ['mqtt']
LOCATION_TOPIC = 'owntracks/+/+'
def setup_scanner(hass, config, see):
""" Set up a MQTT tracker. """
def owntracks_location_update(topic, payload, qos):
""" MQTT message received. """
# Docs on available data:
# http://owntracks.org/booklet/tech/json/#_typelocation
parts = topic.split('/')
try:
data = json.loads(payload)
except ValueError:
# If invalid JSON
return
if data.get('_type') != 'location':
return
dev_id = '{}_{}'.format(parts[1], parts[2])
see(dev_id=dev_id, host_name=parts[1], gps=(data['lat'], data['lon']),
gps_accuracy=data['acc'], battery=data['batt'])
mqtt.subscribe(hass, LOCATION_TOPIC, owntracks_location_update, 1)
return True

View File

@ -26,6 +26,7 @@ from homeassistant.exceptions import (
HomeAssistantError, InvalidEntityFormatError)
import homeassistant.util as util
import homeassistant.util.dt as date_util
import homeassistant.util.location as location
import homeassistant.helpers.temperature as temp_helper
from homeassistant.config import get_default_config_dir
@ -676,6 +677,10 @@ class Config(object):
# Directory that holds the configuration
self.config_dir = get_default_config_dir()
def distance(self, lat, lon):
""" Calculate distance from Home Assistant in meters. """
return location.distance(self.latitude, self.longitude, lat, lon)
def path(self, *path):
""" Returns path to the file within the config dir. """
return os.path.join(self.config_dir, *path)

View File

@ -1,5 +1,6 @@
"""Module with location helpers."""
import collections
from math import radians, cos, sin, asin, sqrt
import requests
@ -28,3 +29,20 @@ def detect_location_info():
'BS', 'BZ', 'KY', 'PW', 'US', 'AS', 'VI')
return LocationInfo(**data)
# From: http://stackoverflow.com/a/4913653/646416
def distance(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance in meters between two points specified
in decimal degrees on the earth using the Haversine algorithm.
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = (radians(val) for val in (lon1, lat1, lon2, lat2))
dlon = lon2 - lon1
dlat = lat2 - lat1
angle = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
# Radius of earth in meters.
radius = 6371000
return 2 * radius * asin(sqrt(angle))

View File

@ -5,14 +5,15 @@ cd "$(dirname "$0")/.."
echo "Checking style with flake8..."
flake8 --exclude www_static homeassistant
STATUS=$?
FLAKE8_STATUS=$?
echo "Checking style with pylint..."
pylint homeassistant
PYLINT_STATUS=$?
if [ $STATUS -eq 0 ]
if [ $FLAKE8_STATUS -eq 0 ]
then
exit $?
exit $FLAKE8_STATUS
else
exit $STATUS
exit $PYLINT_STATUS
fi

21
script/release Executable file
View File

@ -0,0 +1,21 @@
# Pushes a new version to PyPi
cd "$(dirname "$0")/.."
head -n 3 homeassistant/const.py | tail -n 1 | grep dev
if [ $? -eq 0 ]
then
echo "Release version should not contain dev tag"
exit 1
fi
CURRENT_BRANCH=`git rev-parse --abbrev-ref HEAD`
if [ "$CURRENT_BRANCH" != "master" ]
then
echo "You have to be on the master branch to release."
exit 1
fi
python3 setup.py sdist bdist_wheel upload

View File

@ -7,19 +7,21 @@ cd "$(dirname "$0")/.."
script/lint
STATUS=$?
LINT_STATUS=$?
echo "Running tests..."
if [ "$1" = "coverage" ]; then
py.test --cov --cov-report=
TEST_STATUS=$?
else
py.test
TEST_STATUS=$?
fi
if [ $STATUS -eq 0 ]
if [ $LINT_STATUS -eq 0 ]
then
exit $?
exit $TEST_STATUS
else
exit $STATUS
exit $LINT_STATUS
fi

View File

@ -103,12 +103,12 @@ class TestComponentsDeviceTracker(unittest.TestCase):
def test_reading_yaml_config(self):
dev_id = 'test'
device = device_tracker.Device(
self.hass, timedelta(seconds=180), True, dev_id, 'AB:CD:EF:GH:IJ',
'Test name', 'http://test.picture', True)
self.hass, timedelta(seconds=180), 0, True, dev_id,
'AB:CD:EF:GH:IJ', 'Test name', 'http://test.picture', True)
device_tracker.update_config(self.yaml_devices, dev_id, device)
self.assertTrue(device_tracker.setup(self.hass, {}))
config = device_tracker.load_config(self.yaml_devices, self.hass,
device.consider_home)[0]
device.consider_home, 0)[0]
self.assertEqual(device.dev_id, config.dev_id)
self.assertEqual(device.track, config.track)
self.assertEqual(device.mac, config.mac)
@ -126,7 +126,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
self.assertTrue(device_tracker.setup(self.hass, {
device_tracker.DOMAIN: {CONF_PLATFORM: 'test'}}))
config = device_tracker.load_config(self.yaml_devices, self.hass,
timedelta(seconds=0))[0]
timedelta(seconds=0), 0)[0]
self.assertEqual('dev1', config.dev_id)
self.assertEqual(True, config.track)
@ -176,7 +176,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
picture = 'http://placehold.it/200x200'
device = device_tracker.Device(
self.hass, timedelta(seconds=180), True, dev_id, None,
self.hass, timedelta(seconds=180), 0, True, dev_id, None,
friendly_name, picture, away_hide=True)
device_tracker.update_config(self.yaml_devices, dev_id, device)
@ -191,7 +191,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
dev_id = 'test_entity'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
device = device_tracker.Device(
self.hass, timedelta(seconds=180), True, dev_id, None,
self.hass, timedelta(seconds=180), 0, True, dev_id, None,
away_hide=True)
device_tracker.update_config(self.yaml_devices, dev_id, device)
@ -208,7 +208,7 @@ class TestComponentsDeviceTracker(unittest.TestCase):
dev_id = 'test_entity'
entity_id = device_tracker.ENTITY_ID_FORMAT.format(dev_id)
device = device_tracker.Device(
self.hass, timedelta(seconds=180), True, dev_id, None,
self.hass, timedelta(seconds=180), 0, True, dev_id, None,
away_hide=True)
device_tracker.update_config(self.yaml_devices, dev_id, device)