-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Expand file tree
/
Copy path__init__.py
More file actions
97 lines (72 loc) · 3.22 KB
/
__init__.py
File metadata and controls
97 lines (72 loc) · 3.22 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""Set up ohme integration."""
import logging
from ohme import ApiException, AuthException, OhmeApiClient
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.typing import ConfigType
from .const import DOMAIN, PLATFORMS
from .coordinator import (
OhmeChargeSessionCoordinator,
OhmeConfigEntry,
OhmeDeviceInfoCoordinator,
OhmeRuntimeData,
)
from .history import async_ensure_energy_history, async_remove_energy_history
from .services import async_setup_services
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
_LOGGER = logging.getLogger(__name__)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up Ohme integration."""
async_setup_services(hass)
return True
async def async_setup_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> bool:
"""Set up Ohme from a config entry."""
client = OhmeApiClient(
email=entry.data[CONF_EMAIL],
password=entry.data[CONF_PASSWORD],
session=async_get_clientsession(hass),
)
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
charge_session_coordinator = OhmeChargeSessionCoordinator(hass, entry, client)
device_info_coordinator = OhmeDeviceInfoCoordinator(hass, entry, client)
entry.runtime_data = OhmeRuntimeData(
charge_session_coordinator=charge_session_coordinator,
device_info_coordinator=device_info_coordinator,
)
for coordinator in (charge_session_coordinator, device_info_coordinator):
await coordinator.async_config_entry_first_refresh()
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
try:
result = await async_ensure_energy_history(hass, entry)
except Exception:
_LOGGER.exception("Failed to initialize Ohme energy history sync")
else:
_LOGGER.debug("Initialized Ohme energy history sync: %s", result)
charge_session_coordinator.enable_history_sync()
return True
async def async_unload_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> bool:
"""Unload a config entry."""
entry.runtime_data.charge_session_coordinator.disable_history_sync()
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
async def async_remove_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> None:
"""Remove a config entry and its imported recorder history."""
try:
await async_remove_energy_history(hass, entry)
except Exception:
_LOGGER.exception("Failed to remove Ohme energy history")