Mobile App add device tracker to person registering app (#30460)

This commit is contained in:
Paulus Schoutsen authored and GitHub committed 2020-01-04 23:15:50 +01:00
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