2015-10-11 22:14:05 +00:00
|
|
|
"""
|
2016-03-07 17:12:06 +00:00
|
|
|
Support for the Locative platform.
|
2015-10-11 22:14:05 +00:00
|
|
|
|
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-10-11 22:14:05 +00:00
|
|
|
"""
|
2016-10-24 06:48:01 +00:00
|
|
|
import asyncio
|
|
|
|
from functools import partial
|
2015-12-23 10:52:52 +00:00
|
|
|
import logging
|
|
|
|
|
2017-01-02 19:50:42 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_LATITUDE, ATTR_LONGITUDE, STATE_NOT_HOME, HTTP_UNPROCESSABLE_ENTITY)
|
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-10-11 22:14:05 +00:00
|
|
|
|
2015-12-23 10:52:52 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-10-11 22:14:05 +00:00
|
|
|
|
2015-12-31 19:02:50 +00:00
|
|
|
DEPENDENCIES = ['http']
|
2017-03-30 05:19:58 +00:00
|
|
|
URL = '/api/locative'
|
2015-10-11 22:14:05 +00:00
|
|
|
|
|
|
|
|
2017-02-07 19:47:11 +00:00
|
|
|
def setup_scanner(hass, config, see, discovery_info=None):
|
2017-04-14 22:32:04 +00:00
|
|
|
"""Set up an endpoint for the Locative application."""
|
2016-11-25 21:04:06 +00:00
|
|
|
hass.http.register_view(LocativeView(see))
|
2015-10-11 22:14:05 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2016-05-14 07:58:36 +00:00
|
|
|
class LocativeView(HomeAssistantView):
|
2017-04-14 22:32:04 +00:00
|
|
|
"""View to handle Locative requests."""
|
2016-05-14 07:58:36 +00:00
|
|
|
|
2017-03-30 05:19:58 +00:00
|
|
|
url = URL
|
2016-09-11 23:12:28 +00:00
|
|
|
name = 'api:locative'
|
2016-05-14 07:58:36 +00:00
|
|
|
|
2016-11-25 21:04:06 +00:00
|
|
|
def __init__(self, see):
|
2017-04-14 22:32:04 +00:00
|
|
|
"""Initialize Locative URL endpoints."""
|
2016-05-14 07:58:36 +00:00
|
|
|
self.see = see
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
2016-05-14 07:58:36 +00:00
|
|
|
def get(self, request):
|
|
|
|
"""Locative message received as GET."""
|
2017-05-26 20:12:17 +00:00
|
|
|
res = yield from self._handle(request.app['hass'], request.query)
|
2016-10-24 06:48:01 +00:00
|
|
|
return res
|
2016-05-14 07:58:36 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
2016-05-14 07:58:36 +00:00
|
|
|
def post(self, request):
|
|
|
|
"""Locative message received."""
|
2016-10-24 06:48:01 +00:00
|
|
|
data = yield from request.post()
|
2016-11-25 21:04:06 +00:00
|
|
|
res = yield from self._handle(request.app['hass'], data)
|
2016-10-24 06:48:01 +00:00
|
|
|
return res
|
2016-05-14 07:58:36 +00:00
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@asyncio.coroutine
|
2016-11-25 21:04:06 +00:00
|
|
|
def _handle(self, hass, data):
|
2016-10-24 06:48:01 +00:00
|
|
|
"""Handle locative request."""
|
2016-05-14 07:58:36 +00:00
|
|
|
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 '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)
|
|
|
|
|
2017-01-05 08:05:39 +00:00
|
|
|
if 'id' not in data and data['trigger'] != 'test':
|
|
|
|
_LOGGER.error('Location id not specified.')
|
|
|
|
return ('Location id not specified.',
|
|
|
|
HTTP_UNPROCESSABLE_ENTITY)
|
|
|
|
|
2016-05-14 07:58:36 +00:00
|
|
|
device = data['device'].replace('-', '')
|
2017-01-05 08:05:39 +00:00
|
|
|
location_name = data.get('id', data['trigger']).lower()
|
2016-05-14 07:58:36 +00:00
|
|
|
direction = data['trigger']
|
2016-11-05 20:05:15 +00:00
|
|
|
gps_location = (data[ATTR_LATITUDE], data[ATTR_LONGITUDE])
|
2016-05-14 07:58:36 +00:00
|
|
|
|
|
|
|
if direction == 'enter':
|
2017-05-26 15:28:07 +00:00
|
|
|
yield from hass.async_add_job(
|
|
|
|
partial(self.see, dev_id=device, location_name=location_name,
|
|
|
|
gps=gps_location))
|
2016-09-11 23:12:28 +00:00
|
|
|
return 'Setting location to {}'.format(location_name)
|
2016-05-14 07:58:36 +00:00
|
|
|
|
2018-07-23 08:16:05 +00:00
|
|
|
if direction == 'exit':
|
2016-11-25 21:04:06 +00:00
|
|
|
current_state = 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:
|
2016-11-05 20:05:15 +00:00
|
|
|
location_name = STATE_NOT_HOME
|
2017-05-26 15:28:07 +00:00
|
|
|
yield from hass.async_add_job(
|
|
|
|
partial(self.see, dev_id=device,
|
|
|
|
location_name=location_name, gps=gps_location))
|
2016-09-11 23:12:28 +00:00
|
|
|
return 'Setting location to not home'
|
2017-07-06 06:30:01 +00: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.
|
|
|
|
return 'Ignoring exit from {} (already in {})'.format(
|
|
|
|
location_name, current_state)
|
2016-05-14 07:58:36 +00:00
|
|
|
|
2018-07-23 08:16:05 +00:00
|
|
|
if direction == 'test':
|
2016-05-14 07:58:36 +00:00
|
|
|
# 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
|
|
|
|
2017-07-06 06:30:01 +00:00
|
|
|
_LOGGER.error('Received unidentified message from Locative: %s',
|
|
|
|
direction)
|
|
|
|
return ('Received unidentified message: {}'.format(direction),
|
|
|
|
HTTP_UNPROCESSABLE_ENTITY)
|