Mobile App add device tracker to person registering app (#30460)
This commit is contained in:
1 parent
e233dd7cbe
commit
95cd0a2c68
8 files changed
+174
-43
No files matched your search
@@ -1,7 +1,11 @@
|
||||
"""Config flow for Mobile App."""
|
||||
from homeassistant import config_entries
|
||||
import uuid
|
||||
|
||||
from .const import ATTR_DEVICE_ID, ATTR_DEVICE_NAME, DOMAIN
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components import person
|
||||
from homeassistant.helpers import entity_registry
|
||||
|
||||
from .const import ATTR_APP_ID, ATTR_DEVICE_ID, ATTR_DEVICE_NAME, CONF_USER_ID, DOMAIN
|
||||
|
||||
|
||||
@config_entries.HANDLERS.register(DOMAIN)
|
||||
@@ -23,7 +27,25 @@ class MobileAppFlowHandler(config_entries.ConfigFlow):
|
||||
|
||||
async def async_step_registration(self, user_input=None):
|
||||
"""Handle a flow initialized during registration."""
|
||||
await self.async_set_unique_id(user_input[ATTR_DEVICE_ID])
|
||||
if ATTR_DEVICE_ID in user_input:
|
||||
# Unique ID is combi of app + device ID.
|
||||
await self.async_set_unique_id(
|
||||
f"{user_input[ATTR_APP_ID]}-{user_input[ATTR_DEVICE_ID]}"
|
||||
)
|
||||
else:
|
||||
user_input[ATTR_DEVICE_ID] = str(uuid.uuid4()).replace("-", "")
|
||||
|
||||
# Register device tracker entity and add to person registering app
|
||||
ent_reg = await entity_registry.async_get_registry(self.hass)
|
||||
devt_entry = ent_reg.async_get_or_create(
|
||||
"device_tracker",
|
||||
DOMAIN,
|
||||
user_input[ATTR_DEVICE_ID],
|
||||
suggested_object_id=user_input[ATTR_DEVICE_NAME],
|
||||
)
|
||||
await person.async_add_user_device_tracker(
|
||||
self.hass, user_input[CONF_USER_ID], devt_entry.entity_id
|
||||
)
|
||||
|
||||
return self.async_create_entry(
|
||||
title=user_input[ATTR_DEVICE_NAME], data=user_input
|
||||
|
||||
@@ -25,7 +25,6 @@ ATTR_DEVICE_ID = "device_id"
|
||||
ATTR_DEVICE_NAME = "device_name"
|
||||
ATTR_MANUFACTURER = "manufacturer"
|
||||
ATTR_MODEL = "model"
|
||||
ATTR_MODEL_ID = "model_id"
|
||||
ATTR_OS_NAME = "os_name"
|
||||
ATTR_OS_VERSION = "os_version"
|
||||
ATTR_PUSH_TOKEN = "push_token"
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
"""Provides an HTTP API for mobile_app."""
|
||||
import secrets
|
||||
from typing import Dict
|
||||
import uuid
|
||||
|
||||
from aiohttp.web import Request, Response
|
||||
from nacl.secret import SecretBox
|
||||
@@ -21,7 +20,6 @@ from .const import (
|
||||
ATTR_DEVICE_NAME,
|
||||
ATTR_MANUFACTURER,
|
||||
ATTR_MODEL,
|
||||
ATTR_MODEL_ID,
|
||||
ATTR_OS_NAME,
|
||||
ATTR_OS_VERSION,
|
||||
ATTR_SUPPORTS_ENCRYPTION,
|
||||
@@ -50,7 +48,7 @@ class RegistrationsView(HomeAssistantView):
|
||||
vol.Required(ATTR_DEVICE_NAME): cv.string,
|
||||
vol.Required(ATTR_MANUFACTURER): cv.string,
|
||||
vol.Required(ATTR_MODEL): cv.string,
|
||||
vol.Optional(ATTR_MODEL_ID): cv.string, # Added in 0.104
|
||||
vol.Optional(ATTR_DEVICE_ID): cv.string, # Added in 0.104
|
||||
vol.Required(ATTR_OS_NAME): cv.string,
|
||||
vol.Optional(ATTR_OS_VERSION): cv.string,
|
||||
vol.Required(ATTR_SUPPORTS_ENCRYPTION, default=False): cv.boolean,
|
||||
@@ -70,14 +68,6 @@ class RegistrationsView(HomeAssistantView):
|
||||
CONF_CLOUDHOOK_URL
|
||||
] = await hass.components.cloud.async_create_cloudhook(webhook_id)
|
||||
|
||||
model_id = data.get(ATTR_MODEL_ID)
|
||||
|
||||
if model_id is None:
|
||||
data[ATTR_DEVICE_ID] = str(uuid.uuid4()).replace("-", "")
|
||||
|
||||
else:
|
||||
data[ATTR_DEVICE_ID] = f"{data[ATTR_APP_ID]}-{model_id}"
|
||||
|
||||
data[CONF_WEBHOOK_ID] = webhook_id
|
||||
|
||||
if data[ATTR_SUPPORTS_ENCRYPTION] and supports_encryption():
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/mobile_app",
|
||||
"requirements": ["PyNaCl==1.3.0"],
|
||||
"dependencies": ["http", "webhook"],
|
||||
"dependencies": ["http", "webhook", "person"],
|
||||
"after_dependencies": ["cloud"],
|
||||
"codeowners": ["@robbiet480"]
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Support for tracking people."""
|
||||
import logging
|
||||
from typing import List, Optional
|
||||
from typing import List, Optional, cast
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
@@ -24,9 +24,8 @@ from homeassistant.const import (
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import Event, State, callback
|
||||
from homeassistant.helpers import collection, entity_registry
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.core import Event, HomeAssistant, State, callback, split_entity_id
|
||||
from homeassistant.helpers import collection, config_validation as cv, entity_registry
|
||||
from homeassistant.helpers.entity_component import EntityComponent
|
||||
from homeassistant.helpers.event import async_track_state_change
|
||||
from homeassistant.helpers.restore_state import RestoreEntity
|
||||
@@ -77,6 +76,29 @@ async def async_create_person(hass, name, *, user_id=None, device_trackers=None)
|
||||
)
|
||||
|
||||
|
||||
@bind_hass
|
||||
async def async_add_user_device_tracker(
|
||||
hass: HomeAssistant, user_id: str, device_tracker_entity_id: str
|
||||
):
|
||||
"""Add a device tracker to a person linked to a user."""
|
||||
coll = cast(PersonStorageCollection, hass.data[DOMAIN][1])
|
||||
|
||||
for person in coll.async_items():
|
||||
if person.get(ATTR_USER_ID) != user_id:
|
||||
continue
|
||||
|
||||
device_trackers = person["device_trackers"]
|
||||
|
||||
if device_tracker_entity_id in device_trackers:
|
||||
return
|
||||
|
||||
await coll.async_update_item(
|
||||
person[collection.CONF_ID],
|
||||
{"device_trackers": device_trackers + [device_tracker_entity_id]},
|
||||
)
|
||||
break
|
||||
|
||||
|
||||
CREATE_FIELDS = {
|
||||
vol.Required("name"): vol.All(str, vol.Length(min=1)),
|
||||
vol.Optional("user_id"): vol.Any(str, None),
|
||||
@@ -124,6 +146,36 @@ class PersonStorageCollection(collection.StorageCollection):
|
||||
self.async_add_listener(self._collection_changed)
|
||||
self.yaml_collection = yaml_collection
|
||||
|
||||
async def async_load(self) -> None:
|
||||
"""Load the Storage collection."""
|
||||
await super().async_load()
|
||||
self.hass.bus.async_listen(
|
||||
entity_registry.EVENT_ENTITY_REGISTRY_UPDATED, self._entity_registry_updated
|
||||
)
|
||||
|
||||
async def _entity_registry_updated(self, event) -> None:
|
||||
"""Handle entity registry updated."""
|
||||
if event.data["action"] != "remove":
|
||||
return
|
||||
|
||||
entity_id = event.data["entity_id"]
|
||||
|
||||
if split_entity_id(entity_id)[0] != "device_tracker":
|
||||
return
|
||||
|
||||
for person in list(self.data.values()):
|
||||
if entity_id not in person["device_trackers"]:
|
||||
continue
|
||||
|
||||
await self.async_update_item(
|
||||
person[collection.CONF_ID],
|
||||
{
|
||||
"device_trackers": [
|
||||
devt for devt in person["device_trackers"] if devt != entity_id
|
||||
]
|
||||
},
|
||||
)
|
||||
|
||||
async def _process_create_data(self, data: dict) -> dict:
|
||||
"""Validate the config is valid."""
|
||||
data = self.CREATE_SCHEMA(data)
|
||||
|
||||
Reference in new issue
Block a user