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

122 lines
4.3 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
from homeassistant.const import (
2015-12-23 03:52:52 -07:00
HTTP_UNPROCESSABLE_ENTITY, HTTP_INTERNAL_SERVER_ERROR, STATE_NOT_HOME)
2015-12-23 03:52:52 -07:00
_LOGGER = logging.getLogger(__name__)
2015-12-23 03:52:52 -07:00
DEPENDENCIES = ['http', 'zone']
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('-', '')
location_name = data['id']
direction = data['trigger']
try:
gps_coords = (float(data['latitude']), float(data['longitude']))
except ValueError:
2015-12-23 03:52:52 -07:00
handler.write_json_message("Invalid latitude / longitude format.",
HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Received invalid latitude / longitude format.")
return
2015-12-23 03:52:52 -07:00
if direction == 'enter':
zones = [state for state in hass.states.entity_ids('zone')]
_LOGGER.info(zones)
if "zone.{}".format(location_name.lower()) in zones:
see(dev_id=device, location_name=location_name)
2015-12-30 12:30:49 -07:00
handler.write_json_message(
"Set new location to {}".format(location_name))
2015-12-23 03:52:52 -07:00
else:
see(dev_id=device, gps=gps_coords)
2015-12-30 12:30:49 -07:00
handler.write_json_message(
"Set new location to {}".format(gps_coords))
2015-12-23 03:52:52 -07:00
elif direction == 'exit':
2015-12-30 12:30:49 -07:00
current_zone = hass.states.get(
"{}.{}".format("device_tracker", device)).state
2015-12-23 03:52:52 -07:00
if current_zone.lower() == location_name.lower():
see(dev_id=device, location_name=STATE_NOT_HOME)
handler.write_json_message("Set new location to not home")
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_json_message(
"Ignoring transition to {}".format(location_name))
2015-12-23 03:52:52 -07:00
else:
2015-12-30 12:30:49 -07:00
handler.write_json_message(
"Received unidentified message: {}".format(direction))
2015-12-23 03:52:52 -07:00
_LOGGER.error("Received unidentified message from Locative: %s",
direction)
def _check_data(handler, data):
if not isinstance(data, dict):
handler.write_json_message("Error while parsing Locative message.",
HTTP_INTERNAL_SERVER_ERROR)
_LOGGER.error("Error while parsing Locative message: "
"data is not a dict.")
return False
2015-12-23 03:52:52 -07:00
if 'latitude' not in data or 'longitude' not in data:
handler.write_json_message("Latitude and longitude not specified.",
HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Latitude and longitude not specified.")
return False
if 'device' not in data:
handler.write_json_message("Device id not specified.",
HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Device id not specified.")
return False
if 'id' not in data:
handler.write_json_message("Location id not specified.",
HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Location id not specified.")
return False
if 'trigger' not in data:
handler.write_json_message("Trigger is not specified.",
HTTP_UNPROCESSABLE_ENTITY)
_LOGGER.error("Trigger is not specified.")
return False
return True