Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The LaCrosse View integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from lacrosse_view import LaCrosse, LoginError
8 
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.const import Platform
11 from homeassistant.core import HomeAssistant
12 from homeassistant.exceptions import ConfigEntryAuthFailed
13 from homeassistant.helpers.aiohttp_client import async_get_clientsession
14 
15 from .const import DOMAIN
16 from .coordinator import LaCrosseUpdateCoordinator
17 
18 PLATFORMS: list[Platform] = [Platform.SENSOR]
19 _LOGGER = logging.getLogger(__name__)
20 
21 
22 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
23  """Set up LaCrosse View from a config entry."""
24 
25  api = LaCrosse(async_get_clientsession(hass))
26 
27  try:
28  await api.login(entry.data["username"], entry.data["password"])
29  _LOGGER.debug("Log in successful")
30  except LoginError as error:
31  raise ConfigEntryAuthFailed from error
32 
33  coordinator = LaCrosseUpdateCoordinator(hass, api, entry)
34 
35  _LOGGER.debug("First refresh")
36  await coordinator.async_config_entry_first_refresh()
37 
38  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = {
39  "coordinator": coordinator,
40  }
41 
42  _LOGGER.debug("Setting up platforms")
43  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
44 
45  return True
46 
47 
48 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
49  """Unload a config entry."""
50  if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
51  hass.data[DOMAIN].pop(entry.entry_id)
52 
53  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:48
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:22
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)