Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Mobile App."""
2 
3 from typing import Any
4 import uuid
5 
6 from homeassistant.components import person
7 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
8 from homeassistant.const import ATTR_DEVICE_ID
9 from homeassistant.helpers import entity_registry as er
10 
11 from .const import ATTR_APP_ID, ATTR_DEVICE_NAME, CONF_USER_ID, DOMAIN
12 
13 
14 class MobileAppFlowHandler(ConfigFlow, domain=DOMAIN):
15  """Handle a Mobile App config flow."""
16 
17  VERSION = 1
18 
19  async def async_step_user(
20  self, user_input: dict[str, Any] | None = None
21  ) -> ConfigFlowResult:
22  """Handle a flow initialized by the user."""
23  placeholders = {
24  "apps_url": "https://www.home-assistant.io/integrations/mobile_app/#apps"
25  }
26 
27  return self.async_abortasync_abortasync_abort(
28  reason="install_app", description_placeholders=placeholders
29  )
30 
32  self, user_input: dict[str, Any]
33  ) -> ConfigFlowResult:
34  """Handle a flow initialized during registration."""
35  if ATTR_DEVICE_ID in user_input:
36  # Unique ID is combi of app + device ID.
37  await self.async_set_unique_idasync_set_unique_id(
38  f"{user_input[ATTR_APP_ID]}-{user_input[ATTR_DEVICE_ID]}"
39  )
40  else:
41  user_input[ATTR_DEVICE_ID] = str(uuid.uuid4()).replace("-", "")
42 
43  # Register device tracker entity and add to person registering app
44  entity_registry = er.async_get(self.hass)
45  devt_entry = entity_registry.async_get_or_create(
46  "device_tracker",
47  DOMAIN,
48  user_input[ATTR_DEVICE_ID],
49  suggested_object_id=user_input[ATTR_DEVICE_NAME],
50  )
51  await person.async_add_user_device_tracker(
52  self.hass, user_input[CONF_USER_ID], devt_entry.entity_id
53  )
54 
55  return self.async_create_entryasync_create_entryasync_create_entry(
56  title=user_input[ATTR_DEVICE_NAME], data=user_input
57  )
ConfigFlowResult async_step_registration(self, dict[str, Any] user_input)
Definition: config_flow.py:33
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:21
ConfigEntry|None async_set_unique_id(self, str|None unique_id=None, *bool raise_on_progress=True)
ConfigFlowResult async_create_entry(self, *str title, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None, Mapping[str, Any]|None options=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
str
_FlowResultT async_create_entry(self, *str|None title=None, Mapping[str, Any] data, str|None description=None, Mapping[str, str]|None description_placeholders=None)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)