Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Config flow for StreamLabs integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from streamlabswater.streamlabswater import StreamlabsClient
8 import voluptuous as vol
9 
10 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11 from homeassistant.const import CONF_API_KEY
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import HomeAssistantError
14 
15 from .const import DOMAIN, LOGGER
16 
17 
18 async def validate_input(hass: HomeAssistant, api_key: str) -> None:
19  """Validate the user input allows us to connect."""
20  client = StreamlabsClient(api_key)
21  response = await hass.async_add_executor_job(client.get_locations)
22  locations = response.get("locations")
23 
24  if locations is None:
25  raise CannotConnect
26 
27 
28 class StreamlabsConfigFlow(ConfigFlow, domain=DOMAIN):
29  """Handle a config flow for StreamLabs."""
30 
31  VERSION = 1
32 
33  async def async_step_user(
34  self, user_input: dict[str, Any] | None = None
35  ) -> ConfigFlowResult:
36  """Handle the initial step."""
37  errors: dict[str, str] = {}
38  if user_input is not None:
39  self._async_abort_entries_match_async_abort_entries_match(user_input)
40  try:
41  await validate_input(self.hass, user_input[CONF_API_KEY])
42  except CannotConnect:
43  errors["base"] = "cannot_connect"
44  except Exception: # noqa: BLE001
45  LOGGER.exception("Unexpected exception")
46  errors["base"] = "unknown"
47  else:
48  return self.async_create_entryasync_create_entryasync_create_entry(title="Streamlabs", data=user_input)
49 
50  return self.async_show_formasync_show_formasync_show_form(
51  step_id="user",
52  data_schema=vol.Schema(
53  {
54  vol.Required(CONF_API_KEY): str,
55  }
56  ),
57  errors=errors,
58  )
59 
60 
62  """Error to indicate we cannot connect."""
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:35
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)
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)
None validate_input(HomeAssistant hass, str api_key)
Definition: config_flow.py:18