Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the LiteJet lighting system."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import pylitejet
8 from serial import SerialException
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import (
12  ConfigEntry,
13  ConfigFlow,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.const import CONF_PORT
18 from homeassistant.core import callback
20 
21 from .const import CONF_DEFAULT_TRANSITION, DOMAIN
22 
23 
25  """Handle LiteJet options."""
26 
27  async def async_step_init(
28  self, user_input: dict[str, Any] | None = None
29  ) -> ConfigFlowResult:
30  """Manage LiteJet options."""
31  if user_input is not None:
32  return self.async_create_entryasync_create_entry(title="", data=user_input)
33 
34  return self.async_show_formasync_show_form(
35  step_id="init",
36  data_schema=vol.Schema(
37  {
38  vol.Optional(
39  CONF_DEFAULT_TRANSITION,
40  default=self.config_entryconfig_entryconfig_entry.options.get(
41  CONF_DEFAULT_TRANSITION, 0
42  ),
43  ): cv.positive_int,
44  }
45  ),
46  )
47 
48 
49 class LiteJetConfigFlow(ConfigFlow, domain=DOMAIN):
50  """LiteJet config flow."""
51 
52  async def async_step_user(
53  self, user_input: dict[str, Any] | None = None
54  ) -> ConfigFlowResult:
55  """Create a LiteJet config entry based upon user input."""
56  errors = {}
57  if user_input is not None:
58  port = user_input[CONF_PORT]
59 
60  try:
61  system = await pylitejet.open(port)
62  except SerialException:
63  errors[CONF_PORT] = "open_failed"
64  else:
65  await system.close()
66  return self.async_create_entryasync_create_entryasync_create_entry(
67  title=port,
68  data={CONF_PORT: port},
69  )
70 
71  return self.async_show_formasync_show_formasync_show_form(
72  step_id="user",
73  data_schema=vol.Schema({vol.Required(CONF_PORT): str}),
74  errors=errors,
75  )
76 
77  @staticmethod
78  @callback
80  config_entry: ConfigEntry,
81  ) -> LiteJetOptionsFlow:
82  """Get the options flow for this handler."""
83  return LiteJetOptionsFlow()
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:54
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:29
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)
OptionsFlow async_get_options_flow(ConfigEntry config_entry)
None config_entry(self, ConfigEntry value)
_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)