Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The Whirlpool Appliances integration."""
2 
3 from dataclasses import dataclass
4 import logging
5 
6 from aiohttp import ClientError
7 from whirlpool.appliancesmanager import AppliancesManager
8 from whirlpool.auth import Auth
9 from whirlpool.backendselector import BackendSelector
10 
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME, Platform
13 from homeassistant.core import HomeAssistant
14 from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
15 from homeassistant.helpers.aiohttp_client import async_get_clientsession
16 
17 from .const import CONF_BRAND, CONF_BRANDS_MAP, CONF_REGIONS_MAP, DOMAIN
18 
19 _LOGGER = logging.getLogger(__name__)
20 
21 PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
22 
23 
24 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
25  """Set up Whirlpool Sixth Sense from a config entry."""
26  hass.data.setdefault(DOMAIN, {})
27 
28  session = async_get_clientsession(hass)
29  region = CONF_REGIONS_MAP[entry.data.get(CONF_REGION, "EU")]
30  brand = CONF_BRANDS_MAP[entry.data.get(CONF_BRAND, "Whirlpool")]
31  backend_selector = BackendSelector(brand, region)
32 
33  auth = Auth(
34  backend_selector, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], session
35  )
36  try:
37  await auth.do_auth(store=False)
38  except (ClientError, TimeoutError) as ex:
39  raise ConfigEntryNotReady("Cannot connect") from ex
40 
41  if not auth.is_access_token_valid():
42  _LOGGER.error("Authentication failed")
43  raise ConfigEntryAuthFailed("Incorrect Password")
44 
45  appliances_manager = AppliancesManager(backend_selector, auth, session)
46  if not await appliances_manager.fetch_appliances():
47  _LOGGER.error("Cannot fetch appliances")
48  return False
49 
50  hass.data[DOMAIN][entry.entry_id] = WhirlpoolData(
51  appliances_manager, auth, backend_selector
52  )
53 
54  await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
55  return True
56 
57 
58 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
59  """Unload a config entry."""
60  unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
61  if unload_ok:
62  hass.data[DOMAIN].pop(entry.entry_id)
63 
64  return unload_ok
65 
66 
67 @dataclass
69  """Whirlpool integaration shared data."""
70 
71  appliances_manager: AppliancesManager
72  auth: Auth
73  backend_selector: BackendSelector
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:58
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:24
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)