Mobile app to use device tracker config entry (#24238)

* Mobile app to use device tracker config entry

* Lint

* Re-use device_info

* Lint
This commit is contained in:
Paulus Schoutsen committed 2019-05-31 23:05:36 -07:00
1 parent 35ffac1e01
commit fe2e5089ab
14 files changed
+318 -165

No files matched your search

+7 -42
View File
@@ -6,10 +6,6 @@ import voluptuous as vol
from homeassistant.components.cloud import (async_remote_ui_url,
CloudNotAvailable)
from homeassistant.components.device_tracker import (ATTR_ATTRIBUTES,
ATTR_DEV_ID,
DOMAIN as DT_DOMAIN,
SERVICE_SEE as DT_SEE)
from homeassistant.components.frontend import MANIFEST_JSON
from homeassistant.components.zone.const import DOMAIN as ZONE_DOMAIN
@@ -24,15 +20,12 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.template import attach
from homeassistant.helpers.typing import HomeAssistantType
from homeassistant.util import slugify
from .const import (ATTR_ALTITUDE, ATTR_BATTERY, ATTR_COURSE, ATTR_DEVICE_ID,
from .const import (ATTR_DEVICE_ID,
ATTR_DEVICE_NAME, ATTR_EVENT_DATA, ATTR_EVENT_TYPE,
ATTR_GPS, ATTR_GPS_ACCURACY, ATTR_LOCATION_NAME,
ATTR_MANUFACTURER, ATTR_MODEL, ATTR_OS_VERSION,
ATTR_SENSOR_TYPE, ATTR_SENSOR_UNIQUE_ID, ATTR_SPEED,
ATTR_SENSOR_TYPE, ATTR_SENSOR_UNIQUE_ID,
ATTR_SUPPORTS_ENCRYPTION, ATTR_TEMPLATE,
ATTR_TEMPLATE_VARIABLES, ATTR_VERTICAL_ACCURACY,
ATTR_TEMPLATE_VARIABLES,
ATTR_WEBHOOK_DATA, ATTR_WEBHOOK_ENCRYPTED,
ATTR_WEBHOOK_ENCRYPTED_DATA, ATTR_WEBHOOK_TYPE,
CONF_CLOUDHOOK_URL, CONF_REMOTE_UI_URL, CONF_SECRET,
@@ -45,7 +38,7 @@ from .const import (ATTR_ALTITUDE, ATTR_BATTERY, ATTR_COURSE, ATTR_DEVICE_ID,
WEBHOOK_TYPE_REGISTER_SENSOR, WEBHOOK_TYPE_RENDER_TEMPLATE,
WEBHOOK_TYPE_UPDATE_LOCATION,
WEBHOOK_TYPE_UPDATE_REGISTRATION,
WEBHOOK_TYPE_UPDATE_SENSOR_STATES)
WEBHOOK_TYPE_UPDATE_SENSOR_STATES, SIGNAL_LOCATION_UPDATE)
from .helpers import (_decrypt_payload, empty_okay_response, error_response,
@@ -151,37 +144,9 @@ async def handle_webhook(hass: HomeAssistantType, webhook_id: str,
headers=headers)
if webhook_type == WEBHOOK_TYPE_UPDATE_LOCATION:
see_payload = {
ATTR_DEV_ID: slugify(registration[ATTR_DEVICE_NAME]),
ATTR_GPS: data[ATTR_GPS],
ATTR_GPS_ACCURACY: data[ATTR_GPS_ACCURACY],
}
for key in (ATTR_LOCATION_NAME, ATTR_BATTERY):
value = data.get(key)
if value is not None:
see_payload[key] = value
attrs = {}
for key in (ATTR_ALTITUDE, ATTR_COURSE,
ATTR_SPEED, ATTR_VERTICAL_ACCURACY):
value = data.get(key)
if value is not None:
attrs[key] = value
if attrs:
see_payload[ATTR_ATTRIBUTES] = attrs
try:
await hass.services.async_call(DT_DOMAIN,
DT_SEE, see_payload,
blocking=True, context=context)
# noqa: E722 pylint: disable=broad-except
except (vol.Invalid, ServiceNotFound, Exception) as ex:
_LOGGER.error("Error when updating location during mobile_app "
"webhook (device name: %s): %s",
registration[ATTR_DEVICE_NAME], ex)
hass.helpers.dispatcher.async_dispatcher_send(
SIGNAL_LOCATION_UPDATE.format(config_entry.entry_id), data
)
return empty_okay_response(headers=headers)
if webhook_type == WEBHOOK_TYPE_UPDATE_REGISTRATION: