Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for Dremel 3D Printer (3D20, 3D40, 3D45)."""
2 
3 from __future__ import annotations
4 
5 from json.decoder import JSONDecodeError
6 from typing import Any
7 
8 from dremel3dpy import Dremel3DPrinter
9 from requests.exceptions import ConnectTimeout, HTTPError
10 import voluptuous as vol
11 
12 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
13 from homeassistant.const import CONF_HOST
15 
16 from .const import DOMAIN, LOGGER
17 
18 
19 def _schema_with_defaults(host: str = "") -> vol.Schema:
20  return vol.Schema({vol.Required(CONF_HOST, default=host): cv.string})
21 
22 
23 class Dremel3DPrinterConfigFlow(ConfigFlow, domain=DOMAIN):
24  """Handle a config flow for Dremel 3D Printer."""
25 
26  VERSION = 1
27 
28  async def async_step_user(
29  self, user_input: dict[str, Any] | None = None
30  ) -> ConfigFlowResult:
31  """Handle the initial step."""
32  errors = {}
33 
34  if user_input is None:
35  return self.async_show_formasync_show_formasync_show_form(
36  step_id="user",
37  data_schema=_schema_with_defaults(),
38  )
39  host = user_input[CONF_HOST]
40 
41  try:
42  api = await self.hass.async_add_executor_job(Dremel3DPrinter, host)
43  except (ConnectTimeout, HTTPError, JSONDecodeError):
44  errors = {"base": "cannot_connect"}
45  except Exception: # noqa: BLE001
46  LOGGER.exception("An unknown error has occurred")
47  errors = {"base": "unknown"}
48 
49  if errors:
50  return self.async_show_formasync_show_formasync_show_form(
51  step_id="user",
52  errors=errors,
53  data_schema=_schema_with_defaults(host=host),
54  )
55 
56  await self.async_set_unique_idasync_set_unique_id(api.get_serial_number())
57  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
58  return self.async_create_entryasync_create_entryasync_create_entry(title=api.get_title(), data={CONF_HOST: host})
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:30
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_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)