-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
/
__init__.py
69 lines (49 loc) · 2.36 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""The Whirlpool Appliances integration."""
from dataclasses import dataclass
import logging
from aiohttp import ClientError
from whirlpool.appliancesmanager import AppliancesManager
from whirlpool.auth import Auth
from whirlpool.backendselector import BackendSelector
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_PASSWORD, CONF_REGION, CONF_USERNAME, Platform
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from .const import CONF_BRAND, CONF_BRANDS_MAP, CONF_REGIONS_MAP, DOMAIN
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
type WhirlpoolConfigEntry = ConfigEntry[WhirlpoolData]
async def async_setup_entry(hass: HomeAssistant, entry: WhirlpoolConfigEntry) -> bool:
"""Set up Whirlpool Sixth Sense from a config entry."""
hass.data.setdefault(DOMAIN, {})
session = async_get_clientsession(hass)
region = CONF_REGIONS_MAP[entry.data.get(CONF_REGION, "EU")]
brand = CONF_BRANDS_MAP[entry.data.get(CONF_BRAND, "Whirlpool")]
backend_selector = BackendSelector(brand, region)
auth = Auth(
backend_selector, entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], session
)
try:
await auth.do_auth(store=False)
except (ClientError, TimeoutError) as ex:
raise ConfigEntryNotReady("Cannot connect") from ex
if not auth.is_access_token_valid():
_LOGGER.error("Authentication failed")
raise ConfigEntryAuthFailed("Incorrect Password")
appliances_manager = AppliancesManager(backend_selector, auth, session)
if not await appliances_manager.fetch_appliances():
_LOGGER.error("Cannot fetch appliances")
return False
entry.runtime_data = WhirlpoolData(appliances_manager, auth, backend_selector)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: WhirlpoolConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
@dataclass
class WhirlpoolData:
"""Whirlpool integaration shared data."""
appliances_manager: AppliancesManager
auth: Auth
backend_selector: BackendSelector