Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for epson integration."""
2 
3 import logging
4 from typing import Any
5 
6 import voluptuous as vol
7 
8 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
9 from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
10 from homeassistant.helpers.selector import SelectSelector, SelectSelectorConfig
11 
12 from . import validate_projector
13 from .const import CONF_CONNECTION_TYPE, DOMAIN, HTTP, SERIAL
14 from .exceptions import CannotConnect, PoweredOff
15 
16 ALLOWED_CONNECTION_TYPE = [HTTP, SERIAL]
17 
18 DATA_SCHEMA = vol.Schema(
19  {
20  vol.Required(CONF_CONNECTION_TYPE, default=HTTP): SelectSelector(
22  options=ALLOWED_CONNECTION_TYPE, translation_key="connection_type"
23  )
24  ),
25  vol.Required(CONF_HOST): str,
26  vol.Required(CONF_NAME, default=DOMAIN): str,
27  }
28 )
29 
30 _LOGGER = logging.getLogger(__name__)
31 
32 
33 class EpsonConfigFlow(ConfigFlow, domain=DOMAIN):
34  """Handle a config flow for epson."""
35 
36  VERSION = 1
37  MINOR_VERSION = 2
38 
39  async def async_step_user(
40  self, user_input: dict[str, Any] | None = None
41  ) -> ConfigFlowResult:
42  """Handle the initial step."""
43  errors = {}
44  if user_input is not None:
45  # Epson projector doesn't appear to need to be on for serial
46  check_power = user_input[CONF_CONNECTION_TYPE] != SERIAL
47  projector = None
48  try:
49  projector = await validate_projector(
50  hass=self.hass,
51  conn_type=user_input[CONF_CONNECTION_TYPE],
52  host=user_input[CONF_HOST],
53  check_power=True,
54  check_powered_on=check_power,
55  )
56  except CannotConnect:
57  errors["base"] = "cannot_connect"
58  except PoweredOff:
59  _LOGGER.warning(
60  "You need to turn ON projector for initial configuration"
61  )
62  errors["base"] = "powered_off"
63  else:
64  serial_no = await projector.get_serial_number()
65  await self.async_set_unique_idasync_set_unique_id(serial_no)
66  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
67  user_input.pop(CONF_PORT, None)
68  return self.async_create_entryasync_create_entryasync_create_entry(
69  title=user_input.pop(CONF_NAME), data=user_input
70  )
71  finally:
72  if projector:
73  projector.close()
74  return self.async_show_formasync_show_formasync_show_form(
75  step_id="user", data_schema=DATA_SCHEMA, errors=errors
76  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:41
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)
def validate_projector(HomeAssistant hass, str host, str conn_type, bool check_power=True, bool check_powered_on=True)
Definition: __init__.py:30