Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """Support for Tibber."""
2 
3 import logging
4 
5 import aiohttp
6 import tibber
7 
8 from homeassistant.config_entries import ConfigEntry
9 from homeassistant.const import CONF_ACCESS_TOKEN, EVENT_HOMEASSISTANT_STOP, Platform
10 from homeassistant.core import Event, HomeAssistant
11 from homeassistant.exceptions import ConfigEntryNotReady
12 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 from homeassistant.helpers.typing import ConfigType
15 from homeassistant.util import dt as dt_util, ssl as ssl_util
16 
17 from .const import DATA_HASS_CONFIG, DOMAIN
18 from .services import async_setup_services
19 
20 PLATFORMS = [Platform.NOTIFY, Platform.SENSOR]
21 
22 CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
23 
24 _LOGGER = logging.getLogger(__name__)
25 
26 
27 async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
28  """Set up the Tibber component."""
29 
30  hass.data[DATA_HASS_CONFIG] = config
31 
33 
34  return True
35 
36 
37 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
38  """Set up a config entry."""
39 
40  tibber_connection = tibber.Tibber(
41  access_token=entry.data[CONF_ACCESS_TOKEN],
42  websession=async_get_clientsession(hass),
43  time_zone=dt_util.get_default_time_zone(),
44  ssl=ssl_util.get_default_context(),
45  )
46  hass.data[DOMAIN] = tibber_connection
47 
48  async def _close(event: Event) -> None:
49  await tibber_connection.rt_disconnect()
50 
51  entry.async_on_unload(hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close))
52 
53  try:
54  await tibber_connection.update_info()
55 
56  except (
57  TimeoutError,
58  aiohttp.ClientError,
59  tibber.RetryableHttpExceptionError,
60  ) as err:
61  raise ConfigEntryNotReady("Unable to connect") from err
62  except tibber.InvalidLoginError as exp:
63  _LOGGER.error("Failed to login. %s", exp)
64  return False
65  except tibber.FatalHttpExceptionError:
66  return False
67 
68  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
69 
70  return True
71 
72 
73 async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
74  """Unload a config entry."""
75  unload_ok = await hass.config_entries.async_unload_platforms(
76  config_entry, PLATFORMS
77  )
78  if unload_ok:
79  tibber_connection = hass.data[DOMAIN]
80  await tibber_connection.rt_disconnect()
81  return unload_ok
None async_setup_services(HomeAssistant hass)
Definition: __init__.py:72
bool async_setup(HomeAssistant hass, ConfigType config)
Definition: __init__.py:27
bool async_unload_entry(HomeAssistant hass, ConfigEntry config_entry)
Definition: __init__.py:73
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:37
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)