Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for OwnTracks."""
2 
3 import secrets
4 from typing import Any
5 
6 from homeassistant.components import cloud, webhook
7 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
8 from homeassistant.const import CONF_WEBHOOK_ID
9 
10 from .const import DOMAIN
11 from .helper import supports_encryption
12 
13 CONF_SECRET = "secret"
14 CONF_CLOUDHOOK = "cloudhook"
15 
16 
17 class OwnTracksFlow(ConfigFlow, domain=DOMAIN):
18  """Set up OwnTracks."""
19 
20  VERSION = 1
21 
22  async def async_step_user(
23  self, user_input: dict[str, Any] | None = None
24  ) -> ConfigFlowResult:
25  """Handle a user initiated set up flow to create OwnTracks webhook."""
26  if user_input is None:
27  return self.async_show_formasync_show_formasync_show_form(step_id="user")
28 
29  try:
30  webhook_id, webhook_url, cloudhook = await self._get_webhook_id_get_webhook_id()
32  return self.async_abortasync_abortasync_abort(reason="cloud_not_connected")
33 
34  secret = secrets.token_hex(16)
35 
37  secret_desc = (
38  f"The encryption key is {secret} (on Android under Preferences >"
39  " Advanced)"
40  )
41  else:
42  secret_desc = "Encryption is not supported because nacl is not installed."
43 
44  return self.async_create_entryasync_create_entryasync_create_entry(
45  title="OwnTracks",
46  data={
47  CONF_WEBHOOK_ID: webhook_id,
48  CONF_SECRET: secret,
49  CONF_CLOUDHOOK: cloudhook,
50  },
51  description_placeholders={
52  "secret": secret_desc,
53  "webhook_url": webhook_url,
54  "android_url": "https://play.google.com/store/apps/details?id=org.owntracks.android",
55  "ios_url": "https://itunes.apple.com/us/app/owntracks/id692424691?mt=8",
56  "docs_url": "https://www.home-assistant.io/integrations/owntracks/",
57  },
58  )
59 
60  async def _get_webhook_id(self):
61  """Generate webhook ID."""
62  webhook_id = webhook.async_generate_id()
63  if cloud.async_active_subscription(self.hass):
64  webhook_url = await cloud.async_create_cloudhook(self.hass, webhook_id)
65  cloudhook = True
66  else:
67  webhook_url = webhook.async_generate_url(self.hass, webhook_id)
68  cloudhook = False
69 
70  return webhook_id, webhook_url, cloudhook
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:24
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)
ConfigFlowResult async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_FlowResultT async_show_form(self, *str|None step_id=None, vol.Schema|None data_schema=None, dict[str, str]|None errors=None, Mapping[str, str]|None description_placeholders=None, bool|None last_step=None, str|None preview=None)
_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)