Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for the jvc_projector integration."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from typing import Any
7 
8 from jvcprojector import JvcProjector, JvcProjectorAuthError, JvcProjectorConnectError
9 from jvcprojector.projector import DEFAULT_PORT
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT
14 from homeassistant.helpers.device_registry import format_mac
15 from homeassistant.util.network import is_host_valid
16 
17 from .const import DOMAIN, NAME
18 
19 
20 class JvcProjectorConfigFlow(ConfigFlow, domain=DOMAIN):
21  """Config flow for the JVC Projector integration."""
22 
23  VERSION = 1
24 
25  async def async_step_user(
26  self, user_input: dict[str, Any] | None = None
27  ) -> ConfigFlowResult:
28  """Handle user initiated device additions."""
29  errors = {}
30 
31  if user_input is not None:
32  host = user_input[CONF_HOST]
33  port = user_input[CONF_PORT]
34  password = user_input.get(CONF_PASSWORD)
35 
36  try:
37  if not is_host_valid(host):
38  raise InvalidHost # noqa: TRY301
39 
40  mac = await get_mac_address(host, port, password)
41  except InvalidHost:
42  errors["base"] = "invalid_host"
43  except JvcProjectorConnectError:
44  errors["base"] = "cannot_connect"
45  except JvcProjectorAuthError:
46  errors["base"] = "invalid_auth"
47  else:
48  await self.async_set_unique_idasync_set_unique_id(format_mac(mac))
49  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
50  updates={CONF_HOST: host, CONF_PORT: port, CONF_PASSWORD: password}
51  )
52 
53  return self.async_create_entryasync_create_entryasync_create_entry(
54  title=NAME,
55  data={
56  CONF_HOST: host,
57  CONF_PORT: port,
58  CONF_PASSWORD: password,
59  },
60  )
61 
62  return self.async_show_formasync_show_formasync_show_form(
63  step_id="user",
64  data_schema=vol.Schema(
65  {
66  vol.Required(CONF_HOST): str,
67  vol.Required(CONF_PORT, default=DEFAULT_PORT): int,
68  vol.Optional(CONF_PASSWORD): str,
69  }
70  ),
71  errors=errors,
72  )
73 
74  async def async_step_reauth(
75  self, entry_data: Mapping[str, Any]
76  ) -> ConfigFlowResult:
77  """Perform reauth on password authentication error."""
78  return await self.async_step_reauth_confirmasync_step_reauth_confirm()
79 
81  self, user_input: Mapping[str, Any] | None = None
82  ) -> ConfigFlowResult:
83  """Dialog that informs the user that reauth is required."""
84  errors = {}
85 
86  if user_input is not None:
87  reauth_entry = self._get_reauth_entry_get_reauth_entry()
88  host = reauth_entry.data[CONF_HOST]
89  port = reauth_entry.data[CONF_PORT]
90  password = user_input[CONF_PASSWORD]
91 
92  try:
93  await get_mac_address(host, port, password)
94  except JvcProjectorConnectError:
95  errors["base"] = "cannot_connect"
96  except JvcProjectorAuthError:
97  errors["base"] = "invalid_auth"
98  else:
99  return self.async_update_reload_and_abortasync_update_reload_and_abort(
100  reauth_entry, data_updates=user_input
101  )
102 
103  return self.async_show_formasync_show_formasync_show_form(
104  step_id="reauth_confirm",
105  data_schema=vol.Schema({vol.Optional(CONF_PASSWORD): str}),
106  errors=errors,
107  )
108 
109 
110 class InvalidHost(Exception):
111  """Error indicating invalid network host."""
112 
113 
114 async def get_mac_address(host: str, port: int, password: str | None) -> str:
115  """Get device mac address for config flow."""
116  device = JvcProjector(host, port=port, password=password)
117  try:
118  await device.connect(True)
119  finally:
120  await device.disconnect()
121  return device.mac
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:27
ConfigFlowResult async_step_reauth_confirm(self, Mapping[str, Any]|None user_input=None)
Definition: config_flow.py:82
ConfigFlowResult async_step_reauth(self, Mapping[str, Any] entry_data)
Definition: config_flow.py:76
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_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)
str get_mac_address(str host, int port, str|None password)
Definition: config_flow.py:114
bool is_host_valid(str host)
Definition: network.py:93