Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for ReCollect Waste integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiorecollect.client import Client
8 from aiorecollect.errors import RecollectError
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import (
12  ConfigEntry,
13  ConfigFlow,
14  ConfigFlowResult,
15  OptionsFlow,
16 )
17 from homeassistant.const import CONF_FRIENDLY_NAME
18 from homeassistant.core import callback
19 from homeassistant.helpers import aiohttp_client
20 
21 from .const import CONF_PLACE_ID, CONF_SERVICE_ID, DOMAIN, LOGGER
22 
23 DATA_SCHEMA = vol.Schema(
24  {vol.Required(CONF_PLACE_ID): str, vol.Required(CONF_SERVICE_ID): str}
25 )
26 
27 
28 class RecollectWasteConfigFlow(ConfigFlow, domain=DOMAIN):
29  """Handle a config flow for ReCollect Waste."""
30 
31  VERSION = 2
32 
33  @staticmethod
34  @callback
36  config_entry: ConfigEntry,
37  ) -> RecollectWasteOptionsFlowHandler:
38  """Define the config flow to handle options."""
40 
41  async def async_step_user(
42  self, user_input: dict[str, Any] | None = None
43  ) -> ConfigFlowResult:
44  """Handle configuration via the UI."""
45  if user_input is None:
46  return self.async_show_formasync_show_formasync_show_form(
47  step_id="user", data_schema=DATA_SCHEMA, errors={}
48  )
49 
50  unique_id = f"{user_input[CONF_PLACE_ID]}, {user_input[CONF_SERVICE_ID]}"
51 
52  await self.async_set_unique_idasync_set_unique_id(unique_id)
53  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
54 
55  session = aiohttp_client.async_get_clientsession(self.hass)
56  client = Client(
57  user_input[CONF_PLACE_ID], user_input[CONF_SERVICE_ID], session=session
58  )
59 
60  try:
61  await client.async_get_pickup_events()
62  except RecollectError as err:
63  LOGGER.error("Error during setup of integration: %s", err)
64  return self.async_show_formasync_show_formasync_show_form(
65  step_id="user",
66  data_schema=DATA_SCHEMA,
67  errors={"base": "invalid_place_or_service_id"},
68  )
69 
70  return self.async_create_entryasync_create_entryasync_create_entry(
71  title=unique_id,
72  data={
73  CONF_PLACE_ID: user_input[CONF_PLACE_ID],
74  CONF_SERVICE_ID: user_input[CONF_SERVICE_ID],
75  },
76  )
77 
78 
80  """Handle a Recollect Waste options flow."""
81 
82  async def async_step_init(
83  self, user_input: dict[str, Any] | None = None
84  ) -> ConfigFlowResult:
85  """Manage the options."""
86  if user_input is not None:
87  return self.async_create_entryasync_create_entry(title="", data=user_input)
88 
89  return self.async_show_formasync_show_form(
90  step_id="init",
91  data_schema=vol.Schema(
92  {
93  vol.Optional(
94  CONF_FRIENDLY_NAME,
95  default=self.config_entryconfig_entryconfig_entry.options.get(CONF_FRIENDLY_NAME),
96  ): bool
97  }
98  ),
99  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:43
RecollectWasteOptionsFlowHandler async_get_options_flow(ConfigEntry config_entry)
Definition: config_flow.py:37
ConfigFlowResult async_step_init(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:84
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)
None config_entry(self, ConfigEntry value)
_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)