2018-11-06 19:12:03 +00:00
|
|
|
"""
|
|
|
|
Support for Geofency.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/geofency/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2019-01-09 04:47:05 +00:00
|
|
|
from aiohttp import web
|
2018-11-06 19:12:03 +00:00
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-01-27 21:18:20 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER
|
2018-11-06 19:12:03 +00:00
|
|
|
from homeassistant.const import HTTP_UNPROCESSABLE_ENTITY, STATE_NOT_HOME, \
|
2019-01-15 11:50:09 +00:00
|
|
|
ATTR_LATITUDE, ATTR_LONGITUDE, CONF_WEBHOOK_ID, HTTP_OK, ATTR_NAME
|
2019-01-09 04:47:05 +00:00
|
|
|
from homeassistant.helpers import config_entry_flow
|
2018-11-06 19:12:03 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.util import slugify
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'geofency'
|
2019-01-09 04:47:05 +00:00
|
|
|
DEPENDENCIES = ['webhook']
|
2018-11-06 19:12:03 +00:00
|
|
|
|
|
|
|
CONF_MOBILE_BEACONS = 'mobile_beacons'
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
vol.Optional(DOMAIN): vol.Schema({
|
|
|
|
vol.Optional(CONF_MOBILE_BEACONS, default=[]): vol.All(
|
|
|
|
cv.ensure_list,
|
|
|
|
[cv.string]
|
|
|
|
),
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2019-01-15 11:50:09 +00:00
|
|
|
ATTR_ADDRESS = 'address'
|
|
|
|
ATTR_BEACON_ID = 'beaconUUID'
|
2018-11-06 19:12:03 +00:00
|
|
|
ATTR_CURRENT_LATITUDE = 'currentLatitude'
|
|
|
|
ATTR_CURRENT_LONGITUDE = 'currentLongitude'
|
2019-01-15 11:50:09 +00:00
|
|
|
ATTR_DEVICE = 'device'
|
|
|
|
ATTR_ENTRY = 'entry'
|
2018-11-06 19:12:03 +00:00
|
|
|
|
|
|
|
BEACON_DEV_PREFIX = 'beacon'
|
|
|
|
|
|
|
|
LOCATION_ENTRY = '1'
|
|
|
|
LOCATION_EXIT = '0'
|
|
|
|
|
|
|
|
TRACKER_UPDATE = '{}_tracker_update'.format(DOMAIN)
|
|
|
|
|
|
|
|
|
2019-01-15 11:50:09 +00:00
|
|
|
def _address(value: str) -> str:
|
|
|
|
r"""Coerce address by replacing '\n' with ' '."""
|
|
|
|
return value.replace('\n', ' ')
|
|
|
|
|
|
|
|
|
|
|
|
WEBHOOK_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(ATTR_ADDRESS): vol.All(cv.string, _address),
|
|
|
|
vol.Required(ATTR_DEVICE): vol.All(cv.string, slugify),
|
|
|
|
vol.Required(ATTR_ENTRY): vol.Any(LOCATION_ENTRY, LOCATION_EXIT),
|
|
|
|
vol.Required(ATTR_LATITUDE): cv.latitude,
|
|
|
|
vol.Required(ATTR_LONGITUDE): cv.longitude,
|
|
|
|
vol.Required(ATTR_NAME): vol.All(cv.string, slugify),
|
|
|
|
vol.Optional(ATTR_CURRENT_LATITUDE): cv.latitude,
|
|
|
|
vol.Optional(ATTR_CURRENT_LONGITUDE): cv.longitude,
|
|
|
|
vol.Optional(ATTR_BEACON_ID): cv.string
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
2018-11-06 19:12:03 +00:00
|
|
|
async def async_setup(hass, hass_config):
|
|
|
|
"""Set up the Geofency component."""
|
|
|
|
config = hass_config[DOMAIN]
|
|
|
|
mobile_beacons = config[CONF_MOBILE_BEACONS]
|
|
|
|
hass.data[DOMAIN] = [slugify(beacon) for beacon in mobile_beacons]
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-01-09 04:47:05 +00:00
|
|
|
async def handle_webhook(hass, webhook_id, request):
|
2019-01-15 11:50:09 +00:00
|
|
|
"""Handle incoming webhook from Geofency."""
|
|
|
|
try:
|
|
|
|
data = WEBHOOK_SCHEMA(dict(await request.post()))
|
|
|
|
except vol.MultipleInvalid as error:
|
2019-01-09 04:47:05 +00:00
|
|
|
return web.Response(
|
2019-01-15 11:50:09 +00:00
|
|
|
body=error.error_message,
|
2019-01-09 04:47:05 +00:00
|
|
|
status=HTTP_UNPROCESSABLE_ENTITY
|
2018-11-06 19:12:03 +00:00
|
|
|
)
|
|
|
|
|
2019-01-09 04:47:05 +00:00
|
|
|
if _is_mobile_beacon(data, hass.data[DOMAIN]):
|
|
|
|
return _set_location(hass, data, None)
|
|
|
|
if data['entry'] == LOCATION_ENTRY:
|
|
|
|
location_name = data['name']
|
|
|
|
else:
|
|
|
|
location_name = STATE_NOT_HOME
|
|
|
|
if ATTR_CURRENT_LATITUDE in data:
|
|
|
|
data[ATTR_LATITUDE] = data[ATTR_CURRENT_LATITUDE]
|
|
|
|
data[ATTR_LONGITUDE] = data[ATTR_CURRENT_LONGITUDE]
|
|
|
|
|
|
|
|
return _set_location(hass, data, location_name)
|
|
|
|
|
|
|
|
|
|
|
|
def _is_mobile_beacon(data, mobile_beacons):
|
|
|
|
"""Check if we have a mobile beacon."""
|
2019-01-15 11:50:09 +00:00
|
|
|
return ATTR_BEACON_ID in data and data['name'] in mobile_beacons
|
2019-01-09 04:47:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _device_name(data):
|
|
|
|
"""Return name of device tracker."""
|
2019-01-15 11:50:09 +00:00
|
|
|
if ATTR_BEACON_ID in data:
|
2019-01-09 04:47:05 +00:00
|
|
|
return "{}_{}".format(BEACON_DEV_PREFIX, data['name'])
|
|
|
|
return data['device']
|
|
|
|
|
|
|
|
|
|
|
|
def _set_location(hass, data, location_name):
|
|
|
|
"""Fire HA event to set location."""
|
|
|
|
device = _device_name(data)
|
|
|
|
|
|
|
|
async_dispatcher_send(
|
|
|
|
hass,
|
|
|
|
TRACKER_UPDATE,
|
|
|
|
device,
|
|
|
|
(data[ATTR_LATITUDE], data[ATTR_LONGITUDE]),
|
|
|
|
location_name,
|
|
|
|
data
|
|
|
|
)
|
|
|
|
|
|
|
|
return web.Response(
|
|
|
|
body="Setting location for {}".format(device),
|
|
|
|
status=HTTP_OK
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Configure based on config entry."""
|
|
|
|
hass.components.webhook.async_register(
|
|
|
|
DOMAIN, 'Geofency', entry.data[CONF_WEBHOOK_ID], handle_webhook)
|
2019-01-27 21:18:20 +00:00
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, DEVICE_TRACKER)
|
|
|
|
)
|
2019-01-09 04:47:05 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload a config entry."""
|
|
|
|
hass.components.webhook.async_unregister(entry.data[CONF_WEBHOOK_ID])
|
2019-01-27 21:18:20 +00:00
|
|
|
|
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
|
2019-01-09 04:47:05 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
config_entry_flow.register_webhook_flow(
|
|
|
|
DOMAIN,
|
|
|
|
'Geofency Webhook',
|
|
|
|
{
|
|
|
|
'docs_url': 'https://www.home-assistant.io/components/geofency/'
|
|
|
|
}
|
|
|
|
)
|