core/homeassistant/components/device_tracker/locative.py

99 lines
3.4 KiB
Python
Raw Normal View History

"""
2016-03-07 17:12:06 +00:00
Support for the Locative platform.
2015-10-13 18:50:15 +00:00
For more details about this platform, please refer to the documentation at
2015-12-21 15:54:14 +00:00
https://home-assistant.io/components/device_tracker.locative/
"""
2015-12-23 10:52:52 +00:00
import logging
2016-02-19 05:27:50 +00:00
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, STATE_NOT_HOME
2016-05-14 07:58:36 +00:00
from homeassistant.components.http import HomeAssistantView
2016-09-11 23:12:28 +00:00
# pylint: disable=unused-import
from homeassistant.components.device_tracker import ( # NOQA
DOMAIN, PLATFORM_SCHEMA)
2015-12-23 10:52:52 +00:00
_LOGGER = logging.getLogger(__name__)
2015-12-31 19:02:50 +00:00
DEPENDENCIES = ['http']
def setup_scanner(hass, config, see):
2016-03-07 17:12:06 +00:00
"""Setup an endpoint for the Locative application."""
2016-05-14 07:58:36 +00:00
hass.wsgi.register_view(LocativeView(hass, see))
return True
2016-05-14 07:58:36 +00:00
class LocativeView(HomeAssistantView):
"""View to handle locative requests."""
2016-09-11 23:12:28 +00:00
url = '/api/locative'
name = 'api:locative'
2016-05-14 07:58:36 +00:00
def __init__(self, hass, see):
"""Initialize Locative url endpoints."""
super().__init__(hass)
self.see = see
def get(self, request):
"""Locative message received as GET."""
return self.post(request)
def post(self, request):
"""Locative message received."""
# pylint: disable=too-many-return-statements
data = request.values
if 'latitude' not in data or 'longitude' not in data:
2016-09-11 23:12:28 +00:00
return ('Latitude and longitude not specified.',
2016-05-14 07:58:36 +00:00
HTTP_UNPROCESSABLE_ENTITY)
if 'device' not in data:
2016-09-11 23:12:28 +00:00
_LOGGER.error('Device id not specified.')
return ('Device id not specified.',
2016-05-14 07:58:36 +00:00
HTTP_UNPROCESSABLE_ENTITY)
if 'id' not in data:
2016-09-11 23:12:28 +00:00
_LOGGER.error('Location id not specified.')
return ('Location id not specified.',
2016-05-14 07:58:36 +00:00
HTTP_UNPROCESSABLE_ENTITY)
if 'trigger' not in data:
2016-09-11 23:12:28 +00:00
_LOGGER.error('Trigger is not specified.')
return ('Trigger is not specified.',
2016-05-14 07:58:36 +00:00
HTTP_UNPROCESSABLE_ENTITY)
device = data['device'].replace('-', '')
location_name = data['id'].lower()
direction = data['trigger']
if direction == 'enter':
self.see(dev_id=device, location_name=location_name)
2016-09-11 23:12:28 +00:00
return 'Setting location to {}'.format(location_name)
2016-05-14 07:58:36 +00:00
elif direction == 'exit':
current_state = self.hass.states.get(
2016-09-11 23:12:28 +00:00
'{}.{}'.format(DOMAIN, device))
2016-05-14 07:58:36 +00:00
if current_state is None or current_state.state == location_name:
self.see(dev_id=device, location_name=STATE_NOT_HOME)
2016-09-11 23:12:28 +00:00
return 'Setting location to not home'
2016-05-14 07:58:36 +00:00
else:
# 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.
return 'Ignoring exit from {} (already in {})'.format(
location_name, current_state)
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.
2016-09-11 23:12:28 +00:00
return 'Received test message.'
2015-12-23 10:52:52 +00:00
else:
2016-09-11 23:12:28 +00:00
_LOGGER.error('Received unidentified message from Locative: %s',
2016-05-14 07:58:36 +00:00
direction)
2016-09-11 23:12:28 +00:00
return ('Received unidentified message: {}'.format(direction),
2016-05-14 07:58:36 +00:00
HTTP_UNPROCESSABLE_ENTITY)