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,13 +7,15 @@ from homeassistant.helpers.typing import ConfigType, HomeAssistantType
from .const import (ATTR_DEVICE_ID, ATTR_DEVICE_NAME,
ATTR_MANUFACTURER, ATTR_MODEL, ATTR_OS_VERSION,
DATA_BINARY_SENSOR, DATA_CONFIG_ENTRIES, DATA_DELETED_IDS,
DATA_DEVICES, DATA_SENSOR, DATA_STORE, DOMAIN, STORAGE_KEY,
STORAGE_VERSION)
DATA_DEVICES, DATA_DEVICE_TRACKER, DATA_SENSOR, DATA_STORE,
DOMAIN, STORAGE_KEY, STORAGE_VERSION)
from .http_api import RegistrationsView
from .webhook import handle_webhook
from .websocket_api import register_websocket_handlers
PLATFORMS = 'sensor', 'binary_sensor', 'device_tracker'
async def async_setup(hass: HomeAssistantType, config: ConfigType):
"""Set up the mobile app component."""
@@ -24,7 +26,6 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
DATA_BINARY_SENSOR: {},
DATA_CONFIG_ENTRIES: {},
DATA_DELETED_IDS: [],
DATA_DEVICES: {},
DATA_SENSOR: {}
}
@@ -33,6 +34,7 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType):
DATA_CONFIG_ENTRIES: {},
DATA_DELETED_IDS: app_config.get(DATA_DELETED_IDS, []),
DATA_DEVICES: {},
DATA_DEVICE_TRACKER: {},
DATA_SENSOR: app_config.get(DATA_SENSOR, {}),
DATA_STORE: store,
}
@@ -83,10 +85,8 @@ async def async_setup_entry(hass, entry):
webhook_register(hass, DOMAIN, registration_name, webhook_id,
handle_webhook)
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry,
DATA_BINARY_SENSOR))
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, DATA_SENSOR))
for domain in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, domain))
return True
@@ -25,6 +25,7 @@ DATA_BINARY_SENSOR = 'binary_sensor'
DATA_CONFIG_ENTRIES = 'config_entries'
DATA_DELETED_IDS = 'deleted_ids'
DATA_DEVICES = 'devices'
DATA_DEVICE_TRACKER = 'device_tracker'
DATA_SENSOR = 'sensor'
DATA_STORE = 'store'
@@ -160,6 +161,7 @@ SENSOR_TYPES = [ATTR_SENSOR_TYPE_BINARY_SENSOR, ATTR_SENSOR_TYPE_SENSOR]
COMBINED_CLASSES = sorted(set(BINARY_SENSOR_CLASSES + SENSOR_CLASSES))
SIGNAL_SENSOR_UPDATE = DOMAIN + '_sensor_update'
SIGNAL_LOCATION_UPDATE = DOMAIN + '_location_update_{}'
REGISTER_SENSOR_SCHEMA = vol.Schema({
vol.Optional(ATTR_SENSOR_ATTRIBUTES, default={}): dict,
@@ -0,0 +1,137 @@
"""Device tracker platform that adds support for OwnTracks over MQTT."""
import logging
from homeassistant.core import callback
from homeassistant.components.device_tracker.const import (
DOMAIN, SOURCE_TYPE_GPS)
from homeassistant.components.device_tracker.config_entry import (
DeviceTrackerEntity
)
from .const import (
DOMAIN as MA_DOMAIN,
ATTR_ALTITUDE,
ATTR_BATTERY,
ATTR_COURSE,
ATTR_DEVICE_ID,
ATTR_DEVICE_NAME,
ATTR_GPS_ACCURACY,
ATTR_GPS,
ATTR_LOCATION_NAME,
ATTR_SPEED,
ATTR_VERTICAL_ACCURACY,
SIGNAL_LOCATION_UPDATE,
)
from .helpers import device_info
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up OwnTracks based off an entry."""
@callback
def _receive_data(data):
"""Receive set location."""
dev_id = entry.data[ATTR_DEVICE_ID]
device = hass.data[MA_DOMAIN][DOMAIN].get(dev_id)
if device is not None:
device.update_data(data)
return
device = hass.data[MA_DOMAIN][DOMAIN][dev_id] = MobileAppEntity(
entry, data
)
async_add_entities([device])
hass.helpers.dispatcher.async_dispatcher_connect(
SIGNAL_LOCATION_UPDATE.format(entry.entry_id), _receive_data)
return True
class MobileAppEntity(DeviceTrackerEntity):
"""Represent a tracked device."""
def __init__(self, entry, data):
"""Set up OwnTracks entity."""
self._entry = entry
self._data = data
@property
def unique_id(self):
"""Return the unique ID."""
return self._entry.data[ATTR_DEVICE_ID]
@property
def battery_level(self):
"""Return the battery level of the device."""
return self._data.get(ATTR_BATTERY)
@property
def device_state_attributes(self):
"""Return device specific attributes."""
attrs = {}
for key in (ATTR_ALTITUDE, ATTR_COURSE,
ATTR_SPEED, ATTR_VERTICAL_ACCURACY):
value = self._data.get(key)
if value is not None:
attrs[key] = value
return attrs
@property
def location_accuracy(self):
"""Return the gps accuracy of the device."""
return self._data.get(ATTR_GPS_ACCURACY)
@property
def latitude(self):
"""Return latitude value of the device."""
gps = self._data.get(ATTR_GPS)
if gps is None:
return None
return gps[0]
@property
def longitude(self):
"""Return longitude value of the device."""
gps = self._data.get(ATTR_GPS)
if gps is None:
return None
return gps[1]
@property
def location_name(self):
"""Return a location name for the current location of the device."""
return self._data.get(ATTR_LOCATION_NAME)
@property
def name(self):
"""Return the name of the device."""
return self._entry.data[ATTR_DEVICE_NAME]
@property
def should_poll(self):
"""No polling needed."""
return False
@property
def source_type(self):
"""Return the source type, eg gps or router, of the device."""
return SOURCE_TYPE_GPS
@property
def device_info(self):
"""Return the device info."""
return device_info(self._entry.data)
@callback
def update_data(self, data):
"""Mark the device as seen."""
self._data = data
self.async_write_ha_state()
+3 -13
View File
@@ -6,11 +6,11 @@ from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity
from .const import (ATTR_DEVICE_ID, ATTR_DEVICE_NAME, ATTR_MANUFACTURER,
ATTR_MODEL, ATTR_OS_VERSION, ATTR_SENSOR_ATTRIBUTES,
from .const import (ATTR_SENSOR_ATTRIBUTES,
ATTR_SENSOR_DEVICE_CLASS, ATTR_SENSOR_ICON,
ATTR_SENSOR_NAME, ATTR_SENSOR_TYPE, ATTR_SENSOR_UNIQUE_ID,
DOMAIN, SIGNAL_SENSOR_UPDATE)
from .helpers import device_info
def sensor_id(webhook_id, unique_id):
@@ -76,17 +76,7 @@ class MobileAppEntity(Entity):
@property
def device_info(self):
"""Return device registry information for this entity."""
return {
'identifiers': {
(ATTR_DEVICE_ID, self._registration[ATTR_DEVICE_ID]),
(CONF_WEBHOOK_ID, self._registration[CONF_WEBHOOK_ID])
},
'manufacturer': self._registration[ATTR_MANUFACTURER],
'model': self._registration[ATTR_MODEL],
'device_name': self._registration[ATTR_DEVICE_NAME],
'sw_version': self._registration[ATTR_OS_VERSION],
'config_entries': self._device.config_entries
}
return device_info(self._registration)
async def async_update(self):
"""Get the latest state of the sensor."""
+14 -1
View File
@@ -9,7 +9,7 @@ from homeassistant.core import Context
from homeassistant.helpers.json import JSONEncoder
from homeassistant.helpers.typing import HomeAssistantType
from .const import (ATTR_APP_DATA, ATTR_APP_ID, ATTR_APP_NAME,
from .const import (ATTR_APP_DATA, ATTR_APP_ID, ATTR_APP_NAME, ATTR_DEVICE_ID,
ATTR_APP_VERSION, ATTR_DEVICE_NAME, ATTR_MANUFACTURER,
ATTR_MODEL, ATTR_OS_VERSION, ATTR_SUPPORTS_ENCRYPTION,
CONF_SECRET, CONF_USER_ID, DATA_BINARY_SENSOR,
@@ -148,3 +148,16 @@ def webhook_response(data, *, registration: Dict, status: int = 200,
return Response(text=data, status=status, content_type='application/json',
headers=headers)
def device_info(registration: Dict) -> Dict:
"""Return the device info for this registration."""
return {
'identifiers': {
(DOMAIN, registration[ATTR_DEVICE_ID]),
},
'manufacturer': registration[ATTR_MANUFACTURER],
'model': registration[ATTR_MODEL],
'device_name': registration[ATTR_DEVICE_NAME],
'sw_version': registration[ATTR_OS_VERSION],
}
@@ -7,7 +7,6 @@
"PyNaCl==1.3.0"
],
"dependencies": [
"device_tracker",
"http",
"webhook"
],
+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: