-
-
Notifications
You must be signed in to change notification settings - Fork 32k
/
Copy path__init__.py
65 lines (44 loc) · 2.03 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
"""Set up ohme integration."""
from dataclasses import dataclass
from ohme import ApiException, AuthException, OhmeApiClient
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from .const import DOMAIN, PLATFORMS
from .coordinator import OhmeAdvancedSettingsCoordinator, OhmeChargeSessionCoordinator
type OhmeConfigEntry = ConfigEntry[OhmeRuntimeData]
@dataclass()
class OhmeRuntimeData:
"""Dataclass to hold ohme coordinators."""
charge_session_coordinator: OhmeChargeSessionCoordinator
advanced_settings_coordinator: OhmeAdvancedSettingsCoordinator
async def async_setup_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> bool:
"""Set up Ohme from a config entry."""
client = OhmeApiClient(entry.data[CONF_EMAIL], entry.data[CONF_PASSWORD])
try:
await client.async_login()
if not await client.async_update_device_info():
raise ConfigEntryNotReady(
translation_key="device_info_failed", translation_domain=DOMAIN
)
except AuthException as e:
raise ConfigEntryAuthFailed(
translation_key="auth_failed", translation_domain=DOMAIN
) from e
except ApiException as e:
raise ConfigEntryNotReady(
translation_key="api_failed", translation_domain=DOMAIN
) from e
coordinators = (
OhmeChargeSessionCoordinator(hass, client),
OhmeAdvancedSettingsCoordinator(hass, client),
)
for coordinator in coordinators:
await coordinator.async_config_entry_first_refresh()
entry.runtime_data = OhmeRuntimeData(*coordinators)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> bool:
"""Unload a config entry."""
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)