Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for simplepush integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from simplepush import UnknownError, send
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_NAME, CONF_PASSWORD
12 
13 from .const import ATTR_ENCRYPTED, CONF_DEVICE_KEY, CONF_SALT, DEFAULT_NAME, DOMAIN
14 
15 
16 def validate_input(entry: dict[str, str]) -> dict[str, str] | None:
17  """Validate user input."""
18  try:
19  if CONF_PASSWORD in entry:
20  send(
21  key=entry[CONF_DEVICE_KEY],
22  password=entry[CONF_PASSWORD],
23  salt=entry[CONF_SALT],
24  title="HA test",
25  message="Message delivered successfully",
26  )
27  else:
28  send(
29  key=entry[CONF_DEVICE_KEY],
30  title="HA test",
31  message="Message delivered successfully",
32  )
33  except UnknownError:
34  return {"base": "cannot_connect"}
35 
36  return None
37 
38 
39 class SimplePushFlowHandler(ConfigFlow, domain=DOMAIN):
40  """Handle a config flow for simplepush."""
41 
42  async def async_step_user(
43  self, user_input: dict[str, Any] | None = None
44  ) -> ConfigFlowResult:
45  """Handle a flow initiated by the user."""
46  errors: dict[str, str] | None = None
47  if user_input is not None:
48  await self.async_set_unique_idasync_set_unique_id(user_input[CONF_DEVICE_KEY])
49  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
50 
51  self._async_abort_entries_match_async_abort_entries_match(
52  {
53  CONF_NAME: user_input[CONF_NAME],
54  }
55  )
56 
57  if not (
58  errors := await self.hass.async_add_executor_job(
59  validate_input, user_input
60  )
61  ):
62  return self.async_create_entryasync_create_entryasync_create_entry(
63  title=user_input[CONF_NAME],
64  data=user_input,
65  )
66 
67  return self.async_show_formasync_show_formasync_show_form(
68  step_id="user",
69  data_schema=vol.Schema(
70  {
71  vol.Required(CONF_DEVICE_KEY): str,
72  vol.Required(CONF_NAME, default=DEFAULT_NAME): str,
73  vol.Inclusive(CONF_PASSWORD, ATTR_ENCRYPTED): str,
74  vol.Inclusive(CONF_SALT, ATTR_ENCRYPTED): str,
75  }
76  ),
77  errors=errors,
78  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:44
None _abort_if_unique_id_configured(self, dict[str, Any]|None updates=None, bool reload_on_update=True, *str error="already_configured")
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)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
dict[str, str]|None validate_input(dict[str, str] entry)
Definition: config_flow.py:16