1 """Adds config flow for Brother Printer."""
3 from __future__
import annotations
7 from brother
import Brother, SnmpError, UnsupportedModelError
8 import voluptuous
as vol
18 from .const
import DOMAIN, PRINTER_TYPES
20 DATA_SCHEMA = vol.Schema(
22 vol.Required(CONF_HOST): str,
23 vol.Optional(CONF_TYPE, default=
"laser"): vol.In(PRINTER_TYPES),
26 RECONFIGURE_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
30 hass: HomeAssistant, user_input: dict[str, Any], expected_mac: str |
None =
None
32 """Validate the user input."""
38 brother = await Brother.create(user_input[CONF_HOST], snmp_engine=snmp_engine)
39 await brother.async_update()
41 if expected_mac
is not None and brother.serial.lower() != expected_mac:
44 return (brother.model, brother.serial)
48 """Handle a config flow for Brother Printer."""
55 self.
hosthost: str |
None =
None
58 self, user_input: dict[str, Any] |
None =
None
59 ) -> ConfigFlowResult:
60 """Handle the initial step."""
63 if user_input
is not None:
67 errors[CONF_HOST] =
"wrong_host"
68 except (ConnectionError, TimeoutError):
69 errors[
"base"] =
"cannot_connect"
71 errors[
"base"] =
"snmp_error"
72 except UnsupportedModelError:
78 title = f
"{model} {serial}"
82 step_id=
"user", data_schema=DATA_SCHEMA, errors=errors
86 self, discovery_info: zeroconf.ZeroconfServiceInfo
87 ) -> ConfigFlowResult:
88 """Handle zeroconf discovery."""
89 self.
hosthost = discovery_info.host
95 model = discovery_info.properties.get(
"product")
98 self.
brotherbrother = await Brother.create(
99 self.
hosthost, snmp_engine=snmp_engine, model=model
102 except UnsupportedModelError:
104 except (ConnectionError, SnmpError, TimeoutError):
113 "title_placeholders": {
114 "serial_number": self.
brotherbrother.serial,
115 "model": self.
brotherbrother.model,
122 self, user_input: dict[str, Any] |
None =
None
123 ) -> ConfigFlowResult:
124 """Handle a flow initiated by zeroconf."""
125 if user_input
is not None:
126 title = f
"{self.brother.model} {self.brother.serial}"
129 data={CONF_HOST: self.
hosthost, CONF_TYPE: user_input[CONF_TYPE]},
132 step_id=
"zeroconf_confirm",
133 data_schema=vol.Schema(
134 {vol.Optional(CONF_TYPE, default=
"laser"): vol.In(PRINTER_TYPES)}
136 description_placeholders={
137 "serial_number": self.
brotherbrother.serial,
138 "model": self.
brotherbrother.model,
143 self, user_input: dict[str, Any] |
None =
None
144 ) -> ConfigFlowResult:
145 """Handle a reconfiguration flow initialized by the user."""
149 if user_input
is not None:
153 errors[CONF_HOST] =
"wrong_host"
154 except (ConnectionError, TimeoutError):
155 errors[
"base"] =
"cannot_connect"
157 errors[
"base"] =
"snmp_error"
158 except AnotherDevice:
159 errors[
"base"] =
"another_device"
163 data_updates={CONF_HOST: user_input[CONF_HOST]},
167 step_id=
"reconfigure",
169 data_schema=RECONFIGURE_SCHEMA,
170 suggested_values=entry.data | (user_input
or {}),
172 description_placeholders={
"printer_name": entry.title},
178 """Error to indicate that hostname/IP address is invalid."""
181 class AnotherDevice(HomeAssistantError):
182 """Error to indicate that hostname/IP address belongs to another device."""
ConfigFlowResult async_step_zeroconf_confirm(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_step_reconfigure(self, dict[str, Any]|None user_input=None)
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_update_reload_and_abort(self, ConfigEntry entry, *str|None|UndefinedType unique_id=UNDEFINED, str|UndefinedType title=UNDEFINED, Mapping[str, Any]|UndefinedType data=UNDEFINED, Mapping[str, Any]|UndefinedType data_updates=UNDEFINED, Mapping[str, Any]|UndefinedType options=UNDEFINED, str|UndefinedType reason=UNDEFINED, bool reload_even_if_entry_is_unchanged=True)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=None)
ConfigEntry _get_reconfigure_entry(self)
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)
vol.Schema add_suggested_values_to_schema(self, vol.Schema data_schema, Mapping[str, Any]|None suggested_values)
_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)
_FlowResultT async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
tuple[str, str] validate_input(HomeAssistant hass, dict[str, Any] user_input, str|None expected_mac=None)
IssData update(pyiss.ISS iss)
SnmpEngine async_get_snmp_engine(HomeAssistant hass)
bool is_host_valid(str host)