1 """Config flow for Monoprice 6-Zone Amplifier integration."""
3 from __future__
import annotations
8 from pymonoprice
import get_monoprice
9 from serial
import SerialException
10 import voluptuous
as vol
34 _LOGGER = logging.getLogger(__name__)
45 OPTIONS_FOR_DATA: VolDictType = {vol.Optional(source): str
for source
in SOURCES}
47 DATA_SCHEMA = vol.Schema({vol.Required(CONF_PORT): str, **OPTIONS_FOR_DATA})
53 str(idx + 1): data.get(source)
for idx, source
in enumerate(SOURCES)
58 for index, name
in sources_config.items()
59 if (name
is not None and name.strip() !=
"")
64 """Validate the user input allows us to connect.
66 Data has the keys from DATA_SCHEMA with values provided by the user.
69 await hass.async_add_executor_job(get_monoprice, data[CONF_PORT])
70 except SerialException
as err:
71 _LOGGER.error(
"Error connecting to Monoprice controller")
72 raise CannotConnect
from err
77 return {CONF_PORT: data[CONF_PORT], CONF_SOURCES: sources}
81 """Handle a config flow for Monoprice 6-Zone Amplifier."""
86 self, user_input: dict[str, Any] |
None =
None
87 ) -> ConfigFlowResult:
88 """Handle the initial step."""
90 if user_input
is not None:
96 errors[
"base"] =
"cannot_connect"
98 _LOGGER.exception(
"Unexpected exception")
99 errors[
"base"] =
"unknown"
102 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
108 config_entry: ConfigEntry,
109 ) -> MonopriceOptionsFlowHandler:
110 """Define the config flow to handle options."""
116 if str(index)
in previous_sources:
118 source, description={
"suggested_value": previous_sources[
str(index)]}
121 key = vol.Optional(source)
127 """Handle a Monoprice options flow."""
139 self, user_input: dict[str, Any] |
None =
None
140 ) -> ConfigFlowResult:
141 """Manage the options."""
142 if user_input
is not None:
151 for idx, source
in enumerate(SOURCES)
156 data_schema=vol.Schema(options),
161 """Error to indicate we cannot connect."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
MonopriceOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
def _previous_sources(self)
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
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)
ConfigEntry config_entry(self)
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)
def _key_for_source(index, source, previous_sources)
def _sources_from_config(data)
def validate_input(HomeAssistant hass, data)