Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow to configure the IPP integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 from pyipp import (
9  IPP,
10  IPPConnectionError,
11  IPPConnectionUpgradeRequired,
12  IPPError,
13  IPPParseError,
14  IPPResponseError,
15  IPPVersionNotSupportedError,
16 )
17 import voluptuous as vol
18 
19 from homeassistant.components import zeroconf
20 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
21 from homeassistant.const import (
22  CONF_HOST,
23  CONF_NAME,
24  CONF_PORT,
25  CONF_SSL,
26  CONF_UUID,
27  CONF_VERIFY_SSL,
28 )
29 from homeassistant.core import HomeAssistant
30 from homeassistant.helpers.aiohttp_client import async_get_clientsession
31 
32 from .const import CONF_BASE_PATH, CONF_SERIAL, DOMAIN
33 
34 _LOGGER = logging.getLogger(__name__)
35 
36 
37 async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
38  """Validate the user input allows us to connect.
39 
40  Data has the keys from DATA_SCHEMA with values provided by the user.
41  """
42  session = async_get_clientsession(hass)
43  ipp = IPP(
44  host=data[CONF_HOST],
45  port=data[CONF_PORT],
46  base_path=data[CONF_BASE_PATH],
47  tls=data[CONF_SSL],
48  verify_ssl=data[CONF_VERIFY_SSL],
49  session=session,
50  )
51 
52  printer = await ipp.printer()
53 
54  return {CONF_SERIAL: printer.info.serial, CONF_UUID: printer.info.uuid}
55 
56 
57 class IPPFlowHandler(ConfigFlow, domain=DOMAIN):
58  """Handle an IPP config flow."""
59 
60  VERSION = 1
61 
62  def __init__(self) -> None:
63  """Set up the instance."""
64  self.discovery_info: dict[str, Any] = {}
65 
66  async def async_step_user(
67  self, user_input: dict[str, Any] | None = None
68  ) -> ConfigFlowResult:
69  """Handle a flow initiated by the user."""
70  if user_input is None:
71  return self._show_setup_form_show_setup_form()
72 
73  try:
74  info = await validate_input(self.hass, user_input)
75  except IPPConnectionUpgradeRequired:
76  return self._show_setup_form_show_setup_form({"base": "connection_upgrade"})
77  except (IPPConnectionError, IPPResponseError):
78  _LOGGER.debug("IPP Connection/Response Error", exc_info=True)
79  return self._show_setup_form_show_setup_form({"base": "cannot_connect"})
80  except IPPParseError:
81  _LOGGER.debug("IPP Parse Error", exc_info=True)
82  return self.async_abortasync_abortasync_abort(reason="parse_error")
83  except IPPVersionNotSupportedError:
84  return self.async_abortasync_abortasync_abort(reason="ipp_version_error")
85  except IPPError:
86  _LOGGER.debug("IPP Error", exc_info=True)
87  return self.async_abortasync_abortasync_abort(reason="ipp_error")
88 
89  unique_id = user_input[CONF_UUID] = info[CONF_UUID]
90 
91  if not unique_id and info[CONF_SERIAL]:
92  _LOGGER.debug(
93  "Printer UUID is missing from IPP response. Falling back to IPP serial"
94  " number"
95  )
96  unique_id = info[CONF_SERIAL]
97  elif not unique_id:
98  _LOGGER.debug("Unable to determine unique id from IPP response")
99 
100  await self.async_set_unique_idasync_set_unique_id(unique_id)
101  self._abort_if_unique_id_configured_abort_if_unique_id_configured(updates={CONF_HOST: user_input[CONF_HOST]})
102 
103  return self.async_create_entryasync_create_entryasync_create_entry(title=user_input[CONF_HOST], data=user_input)
104 
106  self, discovery_info: zeroconf.ZeroconfServiceInfo
107  ) -> ConfigFlowResult:
108  """Handle zeroconf discovery."""
109  host = discovery_info.host
110 
111  # Avoid probing devices that already have an entry
112  self._async_abort_entries_match_async_abort_entries_match({CONF_HOST: host})
113 
114  port = discovery_info.port
115  zctype = discovery_info.type
116  name = discovery_info.name.replace(f".{zctype}", "")
117  tls = zctype == "_ipps._tcp.local."
118  base_path = discovery_info.properties.get("rp", "ipp/print")
119  unique_id = discovery_info.properties.get("UUID")
120 
121  self.discovery_info.update(
122  {
123  CONF_HOST: host,
124  CONF_PORT: port,
125  CONF_SSL: tls,
126  CONF_VERIFY_SSL: False,
127  CONF_BASE_PATH: f"/{base_path}",
128  CONF_NAME: name,
129  CONF_UUID: unique_id,
130  }
131  )
132 
133  if unique_id:
134  # If we already have the unique id, try to set it now
135  # so we can avoid probing the device if its already
136  # configured or ignored
137  await self._async_set_unique_id_and_abort_if_already_configured_async_set_unique_id_and_abort_if_already_configured(unique_id)
138 
139  self.context.update({"title_placeholders": {"name": name}})
140 
141  try:
142  info = await validate_input(self.hass, self.discovery_info)
143  except IPPConnectionUpgradeRequired:
144  return self.async_abortasync_abortasync_abort(reason="connection_upgrade")
145  except (IPPConnectionError, IPPResponseError):
146  _LOGGER.debug("IPP Connection/Response Error", exc_info=True)
147  return self.async_abortasync_abortasync_abort(reason="cannot_connect")
148  except IPPParseError:
149  _LOGGER.debug("IPP Parse Error", exc_info=True)
150  return self.async_abortasync_abortasync_abort(reason="parse_error")
151  except IPPVersionNotSupportedError:
152  return self.async_abortasync_abortasync_abort(reason="ipp_version_error")
153  except IPPError:
154  _LOGGER.debug("IPP Error", exc_info=True)
155  return self.async_abortasync_abortasync_abort(reason="ipp_error")
156 
157  if not unique_id and info[CONF_UUID]:
158  _LOGGER.debug(
159  "Printer UUID is missing from discovery info. Falling back to IPP UUID"
160  )
161  unique_id = self.discovery_info[CONF_UUID] = info[CONF_UUID]
162  elif not unique_id and info[CONF_SERIAL]:
163  _LOGGER.debug(
164  "Printer UUID is missing from discovery info and IPP response. Falling"
165  " back to IPP serial number"
166  )
167  unique_id = info[CONF_SERIAL]
168  elif not unique_id:
169  _LOGGER.debug(
170  "Unable to determine unique id from discovery info and IPP response"
171  )
172 
173  if unique_id and self.unique_idunique_id != unique_id:
174  await self._async_set_unique_id_and_abort_if_already_configured_async_set_unique_id_and_abort_if_already_configured(unique_id)
175 
176  await self._async_handle_discovery_without_unique_id_async_handle_discovery_without_unique_id()
177  return await self.async_step_zeroconf_confirmasync_step_zeroconf_confirm()
178 
180  self, unique_id: str
181  ) -> None:
182  """Set the unique ID and abort if already configured."""
183  await self.async_set_unique_idasync_set_unique_id(unique_id)
184  self._abort_if_unique_id_configured_abort_if_unique_id_configured(
185  updates={
186  CONF_HOST: self.discovery_info[CONF_HOST],
187  CONF_NAME: self.discovery_info[CONF_NAME],
188  },
189  )
190 
192  self, user_input: dict[str, Any] | None = None
193  ) -> ConfigFlowResult:
194  """Handle a confirmation flow initiated by zeroconf."""
195  if user_input is None:
196  return self.async_show_formasync_show_formasync_show_form(
197  step_id="zeroconf_confirm",
198  description_placeholders={"name": self.discovery_info[CONF_NAME]},
199  errors={},
200  )
201 
202  return self.async_create_entryasync_create_entryasync_create_entry(
203  title=self.discovery_info[CONF_NAME],
204  data=self.discovery_info,
205  )
206 
207  def _show_setup_form(self, errors: dict | None = None) -> ConfigFlowResult:
208  """Show the setup form to the user."""
209  return self.async_show_formasync_show_formasync_show_form(
210  step_id="user",
211  data_schema=vol.Schema(
212  {
213  vol.Required(CONF_HOST): str,
214  vol.Required(CONF_PORT, default=631): int,
215  vol.Required(CONF_BASE_PATH, default="/ipp/print"): str,
216  vol.Required(CONF_SSL, default=False): bool,
217  vol.Required(CONF_VERIFY_SSL, default=False): bool,
218  }
219  ),
220  errors=errors or {},
221  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:68
ConfigFlowResult async_step_zeroconf(self, zeroconf.ZeroconfServiceInfo discovery_info)
Definition: config_flow.py:107
ConfigFlowResult _show_setup_form(self, dict|None errors=None)
Definition: config_flow.py:207
ConfigFlowResult async_step_zeroconf_confirm(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:193
None _async_set_unique_id_and_abort_if_already_configured(self, str unique_id)
Definition: config_flow.py:181
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_abort(self, *str reason, Mapping[str, str]|None description_placeholders=None)
None _async_abort_entries_match(self, dict[str, Any]|None match_dict=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)
_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)
dict[str, Any] validate_input(HomeAssistant hass, dict data)
Definition: config_flow.py:37
IssData update(pyiss.ISS iss)
Definition: __init__.py:33
aiohttp.ClientSession async_get_clientsession(HomeAssistant hass, bool verify_ssl=True, socket.AddressFamily family=socket.AF_UNSPEC, ssl_util.SSLCipherList ssl_cipher=ssl_util.SSLCipherList.PYTHON_DEFAULT)