Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the Netgear integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any, cast
7 from urllib.parse import urlparse
8 
9 from pynetgear import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_USER
10 import voluptuous as vol
11 
12 from homeassistant.components import ssdp
13 from homeassistant.config_entries import (
14  ConfigEntry,
15  ConfigFlow,
16  ConfigFlowResult,
17  OptionsFlow,
18 )
19 from homeassistant.const import (
20  CONF_HOST,
21  CONF_PASSWORD,
22  CONF_PORT,
23  CONF_SSL,
24  CONF_USERNAME,
25 )
26 from homeassistant.core import callback
27 from homeassistant.util.network import is_ipv4_address
28 
29 from .const import (
30  CONF_CONSIDER_HOME,
31  DEFAULT_CONSIDER_HOME,
32  DEFAULT_NAME,
33  DOMAIN,
34  MODELS_PORT_80,
35  MODELS_PORT_5555,
36  PORT_80,
37  PORT_5555,
38 )
39 from .errors import CannotLoginException
40 from .router import get_api
41 
42 _LOGGER = logging.getLogger(__name__)
43 
44 
45 def _discovery_schema_with_defaults(discovery_info):
46  return vol.Schema(_ordered_shared_schema(discovery_info))
47 
48 
50  user_schema = {vol.Optional(CONF_HOST, default=user_input.get(CONF_HOST, "")): str}
51  user_schema.update(_ordered_shared_schema(user_input))
52 
53  return vol.Schema(user_schema)
54 
55 
56 def _ordered_shared_schema(schema_input):
57  return {
58  vol.Optional(CONF_USERNAME, default=schema_input.get(CONF_USERNAME, "")): str,
59  vol.Required(CONF_PASSWORD, default=schema_input.get(CONF_PASSWORD, "")): str,
60  }
61 
62 
64  """Options for the component."""
65 
66  async def async_step_init(
67  self, user_input: dict[str, int] | None = None
68  ) -> ConfigFlowResult:
69  """Manage the options."""
70  if user_input is not None:
71  return self.async_create_entryasync_create_entry(title="", data=user_input)
72 
73  settings_schema = vol.Schema(
74  {
75  vol.Optional(
76  CONF_CONSIDER_HOME,
77  default=self.config_entryconfig_entryconfig_entry.options.get(
78  CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME.total_seconds()
79  ),
80  ): int,
81  }
82  )
83 
84  return self.async_show_formasync_show_form(step_id="init", data_schema=settings_schema)
85 
86 
87 class NetgearFlowHandler(ConfigFlow, domain=DOMAIN):
88  """Handle a config flow."""
89 
90  VERSION = 1
91 
92  def __init__(self) -> None:
93  """Initialize the netgear config flow."""
94  self.placeholdersplaceholders = {
95  CONF_HOST: DEFAULT_HOST,
96  CONF_PORT: DEFAULT_PORT,
97  CONF_USERNAME: DEFAULT_USER,
98  CONF_SSL: False,
99  }
100  self.discovereddiscovered = False
101 
102  @staticmethod
103  @callback
105  config_entry: ConfigEntry,
106  ) -> OptionsFlowHandler:
107  """Get the options flow."""
108  return OptionsFlowHandler()
109 
110  async def _show_setup_form(
111  self,
112  user_input: dict[str, Any] | None = None,
113  errors: dict[str, str] | None = None,
114  ) -> ConfigFlowResult:
115  """Show the setup form to the user."""
116  if not user_input:
117  user_input = {}
118 
119  if self.discovereddiscovered:
120  data_schema = _discovery_schema_with_defaults(user_input)
121  else:
122  data_schema = _user_schema_with_defaults(user_input)
123 
124  return self.async_show_formasync_show_formasync_show_form(
125  step_id="user",
126  data_schema=data_schema,
127  errors=errors or {},
128  description_placeholders=self.placeholdersplaceholders,
129  )
130 
131  async def async_step_ssdp(
132  self, discovery_info: ssdp.SsdpServiceInfo
133  ) -> ConfigFlowResult:
134  """Initialize flow from ssdp."""
135  updated_data: dict[str, str | int | bool] = {}
136 
137  device_url = urlparse(discovery_info.ssdp_location)
138  if hostname := device_url.hostname:
139  hostname = cast(str, hostname)
140  updated_data[CONF_HOST] = hostname
141 
142  if not is_ipv4_address(str(hostname)):
143  return self.async_abortasync_abortasync_abort(reason="not_ipv4_address")
144 
145  _LOGGER.debug("Netgear ssdp discovery info: %s", discovery_info)
146 
147  if ssdp.ATTR_UPNP_SERIAL not in discovery_info.upnp:
148  return self.async_abortasync_abortasync_abort(reason="no_serial")
149 
150  await self.async_set_unique_idasync_set_unique_id(discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL])
151  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates=updated_data)
152 
153  if device_url.scheme == "https":
154  updated_data[CONF_SSL] = True
155  else:
156  updated_data[CONF_SSL] = False
157 
158  updated_data[CONF_PORT] = DEFAULT_PORT
159  for model in MODELS_PORT_80:
160  if discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NUMBER, "").startswith(
161  model
162  ) or discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NAME, "").startswith(
163  model
164  ):
165  updated_data[CONF_PORT] = PORT_80
166  for model in MODELS_PORT_5555:
167  if discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NUMBER, "").startswith(
168  model
169  ) or discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NAME, "").startswith(
170  model
171  ):
172  updated_data[CONF_PORT] = PORT_5555
173  updated_data[CONF_SSL] = True
174 
175  self.placeholdersplaceholders.update(updated_data)
176  self.discovereddiscovered = True
177 
178  return await self.async_step_userasync_step_userasync_step_user()
179 
180  async def async_step_user(
181  self, user_input: dict[str, Any] | None = None
182  ) -> ConfigFlowResult:
183  """Handle a flow initiated by the user."""
184  errors = {}
185 
186  if user_input is None:
187  return await self._show_setup_form_show_setup_form()
188 
189  host = user_input.get(CONF_HOST, self.placeholdersplaceholders[CONF_HOST])
190  port = self.placeholdersplaceholders[CONF_PORT]
191  ssl = self.placeholdersplaceholders[CONF_SSL]
192  username = user_input.get(CONF_USERNAME, self.placeholdersplaceholders[CONF_USERNAME])
193  password = user_input[CONF_PASSWORD]
194  if not username:
195  username = self.placeholdersplaceholders[CONF_USERNAME]
196 
197  # Open connection and check authentication
198  try:
199  api = await self.hass.async_add_executor_job(
200  get_api, password, host, username, port, ssl
201  )
202  except CannotLoginException:
203  errors["base"] = "config"
204  return await self._show_setup_form_show_setup_form(user_input, errors)
205 
206  config_data = {
207  CONF_USERNAME: username,
208  CONF_PASSWORD: password,
209  CONF_HOST: host,
210  CONF_PORT: api.port,
211  CONF_SSL: api.ssl,
212  }
213 
214  # Check if already configured
215  info = await self.hass.async_add_executor_job(api.get_info)
216  if info is None:
217  errors["base"] = "info"
218  return await self._show_setup_form_show_setup_form(user_input, errors)
219 
220  await self.async_set_unique_idasync_set_unique_id(info["SerialNumber"], raise_on_progress=False)
221  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates=config_data)
222 
223  if info.get("ModelName") is not None and info.get("DeviceName") is not None:
224  name = f"{info['ModelName']} - {info['DeviceName']}"
225  else:
226  name = info.get("ModelName", DEFAULT_NAME)
227 
228  return self.async_create_entryasync_create_entryasync_create_entry(
229  title=name,
230  data=config_data,
231  )
ConfigFlowResult async_step_ssdp(self, ssdp.SsdpServiceInfo discovery_info)
Definition: config_flow.py:133
ConfigFlowResult _show_setup_form(self, dict[str, Any]|None user_input=None, dict[str, str]|None errors=None)
Definition: config_flow.py:114
OptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:106
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:182
ConfigFlowResult async_step_init(self, dict[str, int]|None user_input=None)
Definition: config_flow.py:68
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_step_user(self, dict[str, Any]|None user_input=None)
ConfigFlowResult async_abort(self, *str reason, Mapping[str, str]|None description_placeholders=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)
None config_entry(self, ConfigEntry value)
str
_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)
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
def _discovery_schema_with_defaults(discovery_info)
Definition: config_flow.py:45
bool is_ipv4_address(str address)
Definition: network.py:73