hass-core/homeassistant/components/device_tracker/locative.py

104 lines
3.5 KiB
Python
Raw Normal View History

"""
2015-12-21 08:54:14 -07:00
homeassistant.components.device_tracker.locative
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2015-12-21 08:54:14 -07:00
Locative platform for the device tracker.
2015-10-13 20:50:15 +02:00
For more details about this platform, please refer to the documentation at
2015-12-21 08:54:14 -07:00
https://home-assistant.io/components/device_tracker.locative/
"""
2015-12-23 03:52:52 -07:00
import logging
from functools import partial
2015-12-31 12:02:50 -07:00
from homeassistant.components.device_tracker import DOMAIN
2016-02-18 21:27:50 -08:00
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, STATE_NOT_HOME
2015-12-23 03:52:52 -07:00
_LOGGER = logging.getLogger(__name__)
2015-12-31 12:02:50 -07:00
DEPENDENCIES = ['http']
2015-12-21 08:54:14 -07:00
URL_API_LOCATIVE_ENDPOINT = "/api/locative"
2015-10-12 20:58:24 +02:00
def setup_scanner(hass, config, see):
2015-12-21 08:54:14 -07:00
""" Set up an endpoint for the Locative app. """
2015-10-12 00:28:39 +02:00
# POST would be semantically better, but that currently does not work
2015-12-21 08:54:14 -07:00
# since Locative sends the data as key1=value1&key2=value2
2015-10-12 00:28:39 +02:00
# in the request body, while Home Assistant expects json there.
hass.http.register_path(
2015-12-23 03:52:52 -07:00
'GET', URL_API_LOCATIVE_ENDPOINT,
partial(_handle_get_api_locative, hass, see))
return True
2015-12-23 03:52:52 -07:00
def _handle_get_api_locative(hass, see, handler, path_match, data):
2015-12-21 08:54:14 -07:00
""" Locative message received. """
2015-12-23 03:52:52 -07:00
if not _check_data(handler, data):
return
2015-12-23 03:52:52 -07:00
device = data['device'].replace('-', '')
2015-12-31 00:34:06 -07:00
location_name = data['id'].lower()
2015-12-23 03:52:52 -07:00
direction = data['trigger']
if direction == 'enter':
2015-12-31 12:02:50 -07:00
see(dev_id=device, location_name=location_name)
handler.write_text("Setting location to {}".format(location_name))
2015-12-23 03:52:52 -07:00
elif direction == 'exit':
2016-01-15 13:07:26 -07:00
current_state = hass.states.get("{}.{}".format(DOMAIN, device))
2015-12-23 03:52:52 -07:00
2016-01-15 13:07:26 -07:00
if current_state is None or current_state.state == location_name:
2015-12-23 03:52:52 -07:00
see(dev_id=device, location_name=STATE_NOT_HOME)
2015-12-31 01:43:18 -07:00
handler.write_text("Setting location to not home")
2015-12-23 03:52:52 -07:00
else:
2015-12-30 12:30:49 -07:00
# Ignore the message if it is telling us to exit a zone that we
# aren't currently in. This occurs when a zone is entered before
# the previous zone was exited. The enter message will be sent
# first, then the exit message will be sent second.
handler.write_text(
2015-12-31 01:43:18 -07:00
'Ignoring exit from {} (already in {})'.format(
2015-12-31 12:02:50 -07:00
location_name, current_state))
2015-12-30 17:30:20 -07:00
elif direction == 'test':
# In the app, a test message can be sent. Just return something to
# the user to let them know that it works.
handler.write_text("Received test message.")
2015-12-23 03:52:52 -07:00
else:
handler.write_text(
2015-12-31 00:34:06 -07:00
"Received unidentified message: {}".format(direction),
HTTP_UNPROCESSABLE_ENTITY)
2015-12-23 03:52:52 -07:00
_LOGGER.error("Received unidentified message from Locative: %s",
direction)
def _check_data(handler, data):
if 'latitude' not in data or 'longitude' not in data:
handler.write_text("Latitude and longitude not specified.",
HTTP_UNPROCESSABLE_ENTITY)
2015-12-23 03:52:52 -07:00
_LOGGER.error("Latitude and longitude not specified.")
return False
if 'device' not in data:
handler.write_text("Device id not specified.",
HTTP_UNPROCESSABLE_ENTITY)
2015-12-23 03:52:52 -07:00
_LOGGER.error("Device id not specified.")
return False
if 'id' not in data:
handler.write_text("Location id not specified.",
HTTP_UNPROCESSABLE_ENTITY)
2015-12-23 03:52:52 -07:00
_LOGGER.error("Location id not specified.")
return False
if 'trigger' not in data:
handler.write_text("Trigger is not specified.",
HTTP_UNPROCESSABLE_ENTITY)
2015-12-23 03:52:52 -07:00
_LOGGER.error("Trigger is not specified.")
return False
return True