2019-02-14 15:01:46 +00:00
|
|
|
"""Support for GPSLogger."""
|
2019-01-14 00:09:47 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2019-01-16 18:56:25 +00:00
|
|
|
from aiohttp import web
|
2019-01-14 00:09:47 +00:00
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-01-16 18:56:25 +00:00
|
|
|
from homeassistant.components.device_tracker import ATTR_BATTERY
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
HTTP_UNPROCESSABLE_ENTITY,
|
|
|
|
HTTP_OK,
|
|
|
|
ATTR_LATITUDE,
|
|
|
|
ATTR_LONGITUDE,
|
|
|
|
CONF_WEBHOOK_ID,
|
|
|
|
)
|
2019-01-16 18:56:25 +00:00
|
|
|
from homeassistant.helpers import config_entry_flow
|
2019-01-14 00:09:47 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
2019-01-27 23:37:19 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER
|
2019-06-10 19:46:38 +00:00
|
|
|
from .const import (
|
|
|
|
DOMAIN,
|
|
|
|
ATTR_ALTITUDE,
|
|
|
|
ATTR_ACCURACY,
|
|
|
|
ATTR_ACTIVITY,
|
|
|
|
ATTR_DEVICE,
|
|
|
|
ATTR_DIRECTION,
|
|
|
|
ATTR_PROVIDER,
|
|
|
|
ATTR_SPEED,
|
|
|
|
)
|
2019-01-14 00:09:47 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-09-03 15:10:56 +00:00
|
|
|
TRACKER_UPDATE = f"{DOMAIN}_tracker_update"
|
2019-01-14 00:09:47 +00:00
|
|
|
|
2019-01-16 18:56:25 +00:00
|
|
|
|
|
|
|
DEFAULT_ACCURACY = 200
|
|
|
|
DEFAULT_BATTERY = -1
|
|
|
|
|
|
|
|
|
|
|
|
def _id(value: str) -> str:
|
|
|
|
"""Coerce id by removing '-'."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return value.replace("-", "")
|
|
|
|
|
|
|
|
|
|
|
|
WEBHOOK_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_DEVICE): _id,
|
|
|
|
vol.Required(ATTR_LATITUDE): cv.latitude,
|
|
|
|
vol.Required(ATTR_LONGITUDE): cv.longitude,
|
|
|
|
vol.Optional(ATTR_ACCURACY, default=DEFAULT_ACCURACY): vol.Coerce(float),
|
|
|
|
vol.Optional(ATTR_ACTIVITY): cv.string,
|
|
|
|
vol.Optional(ATTR_ALTITUDE): vol.Coerce(float),
|
|
|
|
vol.Optional(ATTR_BATTERY, default=DEFAULT_BATTERY): vol.Coerce(float),
|
|
|
|
vol.Optional(ATTR_DIRECTION): vol.Coerce(float),
|
|
|
|
vol.Optional(ATTR_PROVIDER): cv.string,
|
|
|
|
vol.Optional(ATTR_SPEED): vol.Coerce(float),
|
|
|
|
}
|
|
|
|
)
|
2019-01-14 00:09:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, hass_config):
|
|
|
|
"""Set up the GPSLogger component."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.data[DOMAIN] = {"devices": set(), "unsub_device_tracker": {}}
|
2019-01-14 00:09:47 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2019-01-16 18:56:25 +00:00
|
|
|
async def handle_webhook(hass, webhook_id, request):
|
|
|
|
"""Handle incoming webhook with GPSLogger request."""
|
|
|
|
try:
|
|
|
|
data = WEBHOOK_SCHEMA(dict(await request.post()))
|
|
|
|
except vol.MultipleInvalid as error:
|
2019-07-31 19:25:30 +00:00
|
|
|
return web.Response(text=error.error_message, status=HTTP_UNPROCESSABLE_ENTITY)
|
2019-01-14 00:09:47 +00:00
|
|
|
|
2019-01-16 18:56:25 +00:00
|
|
|
attrs = {
|
|
|
|
ATTR_SPEED: data.get(ATTR_SPEED),
|
|
|
|
ATTR_DIRECTION: data.get(ATTR_DIRECTION),
|
|
|
|
ATTR_ALTITUDE: data.get(ATTR_ALTITUDE),
|
|
|
|
ATTR_PROVIDER: data.get(ATTR_PROVIDER),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ACTIVITY: data.get(ATTR_ACTIVITY),
|
2019-01-16 18:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
device = data[ATTR_DEVICE]
|
|
|
|
|
|
|
|
async_dispatcher_send(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass,
|
|
|
|
TRACKER_UPDATE,
|
|
|
|
device,
|
2019-01-16 18:56:25 +00:00
|
|
|
(data[ATTR_LATITUDE], data[ATTR_LONGITUDE]),
|
2019-07-31 19:25:30 +00:00
|
|
|
data[ATTR_BATTERY],
|
|
|
|
data[ATTR_ACCURACY],
|
|
|
|
attrs,
|
2019-01-16 18:56:25 +00:00
|
|
|
)
|
|
|
|
|
2019-09-03 15:10:56 +00:00
|
|
|
return web.Response(text=f"Setting location for {device}", status=HTTP_OK)
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-01-16 18:56:25 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Configure based on config entry."""
|
|
|
|
hass.components.webhook.async_register(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, "GPSLogger", entry.data[CONF_WEBHOOK_ID], handle_webhook
|
|
|
|
)
|
2019-01-27 23:37:19 +00:00
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, DEVICE_TRACKER)
|
|
|
|
)
|
2019-01-16 18:56:25 +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-07-31 19:25:30 +00:00
|
|
|
hass.data[DOMAIN]["unsub_device_tracker"].pop(entry.entry_id)()
|
2019-01-27 23:37:19 +00:00
|
|
|
await hass.config_entries.async_forward_entry_unload(entry, DEVICE_TRACKER)
|
2019-01-16 18:56:25 +00:00
|
|
|
return True
|
|
|
|
|
2019-04-01 12:07:12 +00:00
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
async_remove_entry = config_entry_flow.webhook_async_remove_entry
|