Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure Agent devices."""
2 
3 from contextlib import suppress
4 from typing import Any
5 
6 from agent import AgentConnectionError, AgentError
7 from agent.a import Agent
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_HOST, CONF_PORT
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
13 
14 from .const import DOMAIN, SERVER_URL
15 from .helpers import generate_url
16 
17 DEFAULT_PORT = 8090
18 
19 
20 class AgentFlowHandler(ConfigFlow, domain=DOMAIN):
21  """Handle an Agent config flow."""
22 
23  async def async_step_user(
24  self, user_input: dict[str, Any] | None = None
25  ) -> ConfigFlowResult:
26  """Handle an Agent config flow."""
27  errors = {}
28 
29  if user_input is not None:
30  host = user_input[CONF_HOST]
31  port = user_input[CONF_PORT]
32 
33  server_origin = generate_url(host, port)
34  agent_client = Agent(server_origin, async_get_clientsession(self.hass))
35 
36  with suppress(AgentConnectionError, AgentError):
37  await agent_client.update()
38 
39  await agent_client.close()
40 
41  if agent_client.is_available:
42  await self.async_set_unique_idasync_set_unique_id(agent_client.unique)
43 
44  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
45  updates={
46  CONF_HOST: user_input[CONF_HOST],
47  CONF_PORT: user_input[CONF_PORT],
48  SERVER_URL: server_origin,
49  }
50  )
51 
52  device_config = {
53  CONF_HOST: host,
54  CONF_PORT: port,
55  SERVER_URL: server_origin,
56  }
57 
58  return self.async_create_entryasync_create_entryasync_create_entry(
59  title=agent_client.name, data=device_config
60  )
61 
62  errors["base"] = "cannot_connect"
63 
64  data = {
65  vol.Required(CONF_HOST): str,
66  vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
67  }
68 
69  return self.async_show_formasync_show_formasync_show_form(
70  step_id="user",
71  data_schema=vol.Schema(data),
72  errors=errors,
73  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:25
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)
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)
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)