Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for P1 Monitor integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from p1monitor import P1Monitor, P1MonitorError
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
14  NumberSelector,
15  NumberSelectorConfig,
16  NumberSelectorMode,
17  TextSelector,
18 )
19 
20 from .const import DOMAIN
21 
22 
23 class P1MonitorFlowHandler(ConfigFlow, domain=DOMAIN):
24  """Config flow for P1 Monitor."""
25 
26  VERSION = 2
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 
33  errors = {}
34 
35  if user_input is not None:
36  session = async_get_clientsession(self.hass)
37  try:
38  async with P1Monitor(
39  host=user_input[CONF_HOST],
40  port=user_input[CONF_PORT],
41  session=session,
42  ) as client:
43  await client.smartmeter()
44  except P1MonitorError:
45  errors["base"] = "cannot_connect"
46  else:
47  return self.async_create_entryasync_create_entryasync_create_entry(
48  title="P1 Monitor",
49  data={
50  CONF_HOST: user_input[CONF_HOST],
51  CONF_PORT: user_input[CONF_PORT],
52  },
53  )
54 
55  return self.async_show_formasync_show_formasync_show_form(
56  step_id="user",
57  data_schema=vol.Schema(
58  {
59  vol.Required(CONF_HOST): TextSelector(),
60  vol.Required(CONF_PORT, default=80): vol.All(
63  min=1, max=65535, mode=NumberSelectorMode.BOX
64  ),
65  ),
66  vol.Coerce(int),
67  ),
68  }
69  ),
70  errors=errors,
71  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:30
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)