Home Assistant Unofficial Reference 2024.12.1
config_flow.py
Go to the documentation of this file.
1 """Adds config flow for Tibber integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import aiohttp
8 import tibber
9 import voluptuous as vol
10 
11 from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
12 from homeassistant.const import CONF_ACCESS_TOKEN
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN
16 
17 DATA_SCHEMA = vol.Schema({vol.Required(CONF_ACCESS_TOKEN): str})
18 ERR_TIMEOUT = "timeout"
19 ERR_CLIENT = "cannot_connect"
20 ERR_TOKEN = "invalid_access_token"
21 TOKEN_URL = "https://developer.tibber.com/settings/access-token"
22 
23 
24 class TibberConfigFlow(ConfigFlow, domain=DOMAIN):
25  """Handle a config flow for Tibber integration."""
26 
27  VERSION = 1
28 
29  async def async_step_user(
30  self, user_input: dict[str, Any] | None = None
31  ) -> ConfigFlowResult:
32  """Handle the initial step."""
33 
34  self._async_abort_entries_match_async_abort_entries_match()
35 
36  if user_input is not None:
37  access_token = user_input[CONF_ACCESS_TOKEN].replace(" ", "")
38 
39  tibber_connection = tibber.Tibber(
40  access_token=access_token,
41  websession=async_get_clientsession(self.hass),
42  )
43 
44  errors = {}
45 
46  try:
47  await tibber_connection.update_info()
48  except TimeoutError:
49  errors[CONF_ACCESS_TOKEN] = ERR_TIMEOUT
50  except tibber.InvalidLoginError:
51  errors[CONF_ACCESS_TOKEN] = ERR_TOKEN
52  except (
53  aiohttp.ClientError,
54  tibber.RetryableHttpExceptionError,
55  tibber.FatalHttpExceptionError,
56  ):
57  errors[CONF_ACCESS_TOKEN] = ERR_CLIENT
58 
59  if errors:
60  return self.async_show_formasync_show_formasync_show_form(
61  step_id="user",
62  data_schema=DATA_SCHEMA,
63  description_placeholders={"url": TOKEN_URL},
64  errors=errors,
65  )
66 
67  unique_id = tibber_connection.user_id
68  await self.async_set_unique_idasync_set_unique_id(unique_id)
69  self._abort_if_unique_id_configured_abort_if_unique_id_configured()
70 
71  return self.async_create_entryasync_create_entryasync_create_entry(
72  title=tibber_connection.name,
73  data={CONF_ACCESS_TOKEN: access_token},
74  )
75 
76  return self.async_show_formasync_show_formasync_show_form(
77  step_id="user",
78  data_schema=DATA_SCHEMA,
79  description_placeholders={"url": TOKEN_URL},
80  errors={},
81  )
ConfigFlowResult async_step_user(self, dict[str, Any]|None user_input=None)
Definition: config_flow.py:31
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)
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)
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)