Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Smarty integration."""
2 
3 from typing import Any
4 
5 from pysmarty2 import Smarty
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import CONF_HOST, CONF_NAME
10 
11 from .const import DOMAIN
12 
13 
14 class SmartyConfigFlow(ConfigFlow, domain=DOMAIN):
15  """Smarty config flow."""
16 
17  def _test_connection(self, host: str) -> str | None:
18  """Test the connection to the Smarty API."""
19  smarty = Smarty(host=host)
20  try:
21  if smarty.update():
22  return None
23  except Exception: # noqa: BLE001
24  return "unknown"
25  else:
26  return "cannot_connect"
27 
28  async def async_step_user(
29  self, user_input: dict[str, Any] | None = None
30  ) -> ConfigFlowResult:
31  """Handle a flow initialized by the user."""
32  errors: dict[str, str] = {}
33 
34  if user_input is not None:
35  self._async_abort_entries_match_async_abort_entries_match(user_input)
36  error = await self.hass.async_add_executor_job(
37  self._test_connection_test_connection, user_input[CONF_HOST]
38  )
39  if not error:
40  return self.async_create_entryasync_create_entryasync_create_entry(
41  title=user_input[CONF_HOST], data=user_input
42  )
43  errors["base"] = error
44  return self.async_show_formasync_show_formasync_show_form(
45  step_id="user",
46  data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
47  errors=errors,
48  )
49 
50  async def async_step_import(
51  self, import_config: dict[str, Any]
52  ) -> ConfigFlowResult:
53  """Handle a flow initialized by import."""
54  error = await self.hass.async_add_executor_job(
55  self._test_connection_test_connection, import_config[CONF_HOST]
56  )
57  if not error:
58  return self.async_create_entryasync_create_entryasync_create_entry(
59  title=import_config[CONF_NAME],
60  data={CONF_HOST: import_config[CONF_HOST]},
61  )
62  return self.async_abortasync_abortasync_abort(reason=error)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:30
ConfigFlowResult async_step_import(self, dict[str, Any] import_config)
Definition: config_flow.py:52
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)
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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)