Skip to content

Commit 0c98b7e

Browse files
Add Ohme historical energy sync draft
1 parent 4c5f09a commit 0c98b7e

14 files changed

Lines changed: 1445 additions & 85 deletions

File tree

homeassistant/components/ohme/__init__.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Set up ohme integration."""
22

3+
import logging
4+
35
from ohme import ApiException, AuthException, OhmeApiClient
46

57
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
@@ -16,10 +18,13 @@
1618
OhmeDeviceInfoCoordinator,
1719
OhmeRuntimeData,
1820
)
21+
from .history import async_ensure_energy_history
1922
from .services import async_setup_services
2023

2124
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
2225

26+
_LOGGER = logging.getLogger(__name__)
27+
2328

2429
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
2530
"""Set up Ohme integration."""
@@ -53,22 +58,32 @@ async def async_setup_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> bool
5358
translation_key="api_failed", translation_domain=DOMAIN
5459
) from e
5560

56-
coordinators = (
57-
OhmeChargeSessionCoordinator(hass, entry, client),
58-
OhmeDeviceInfoCoordinator(hass, entry, client),
61+
charge_session_coordinator = OhmeChargeSessionCoordinator(hass, entry, client)
62+
device_info_coordinator = OhmeDeviceInfoCoordinator(hass, entry, client)
63+
64+
entry.runtime_data = OhmeRuntimeData(
65+
charge_session_coordinator=charge_session_coordinator,
66+
device_info_coordinator=device_info_coordinator,
5967
)
6068

61-
for coordinator in coordinators:
69+
for coordinator in (charge_session_coordinator, device_info_coordinator):
6270
await coordinator.async_config_entry_first_refresh()
6371

64-
entry.runtime_data = OhmeRuntimeData(*coordinators)
65-
6672
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
6773

74+
try:
75+
result = await async_ensure_energy_history(hass, entry)
76+
except Exception:
77+
_LOGGER.exception("Failed to initialize Ohme energy history sync")
78+
else:
79+
_LOGGER.debug("Initialized Ohme energy history sync: %s", result)
80+
81+
charge_session_coordinator.enable_history_sync()
82+
6883
return True
6984

7085

7186
async def async_unload_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> bool:
7287
"""Unload a config entry."""
73-
88+
entry.runtime_data.charge_session_coordinator.disable_history_sync()
7489
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

homeassistant/components/ohme/config_flow.py

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
"""Config flow for ohme integration."""
22

3+
from __future__ import annotations
4+
35
from collections.abc import Mapping
46
from typing import Any
57

68
from ohme import ApiException, AuthException, OhmeApiClient
79
import voluptuous as vol
810

9-
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
11+
from homeassistant.config_entries import (
12+
ConfigEntry,
13+
ConfigFlow,
14+
ConfigFlowResult,
15+
OptionsFlowWithReload,
16+
)
1017
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
1118
from homeassistant.helpers.selector import (
19+
NumberSelector,
20+
NumberSelectorConfig,
21+
NumberSelectorMode,
1222
TextSelector,
1323
TextSelectorConfig,
1424
TextSelectorType,
1525
)
1626

17-
from .const import DOMAIN
27+
from .const import CONF_BACKFILL_DAYS, DEFAULT_BACKFILL_DAYS, DOMAIN
1828

1929
USER_SCHEMA = vol.Schema(
2030
{
@@ -48,6 +58,11 @@
4858
class OhmeConfigFlow(ConfigFlow, domain=DOMAIN):
4959
"""Config flow."""
5060

61+
@staticmethod
62+
def async_get_options_flow(config_entry: ConfigEntry) -> OhmeOptionsFlow:
63+
"""Return the options flow."""
64+
return OhmeOptionsFlow()
65+
5166
async def async_step_user(
5267
self, user_input: dict[str, Any] | None = None
5368
) -> ConfigFlowResult:
@@ -63,7 +78,9 @@ async def async_step_user(
6378
)
6479
if not errors:
6580
return self.async_create_entry(
66-
title=user_input[CONF_EMAIL], data=user_input
81+
title=user_input[CONF_EMAIL],
82+
data=user_input,
83+
options={CONF_BACKFILL_DAYS: DEFAULT_BACKFILL_DAYS},
6784
)
6885

6986
return self.async_show_form(
@@ -137,3 +154,34 @@ async def _validate_account(self, email: str, password: str) -> dict[str, str]:
137154
errors["base"] = "unknown"
138155

139156
return errors
157+
158+
159+
class OhmeOptionsFlow(OptionsFlowWithReload):
160+
"""Handle Ohme options."""
161+
162+
async def async_step_init(
163+
self, user_input: dict[str, Any] | None = None
164+
) -> ConfigFlowResult:
165+
"""Manage integration options."""
166+
if user_input is not None:
167+
return self.async_create_entry(title="", data=user_input)
168+
169+
return self.async_show_form(
170+
step_id="init",
171+
data_schema=vol.Schema(
172+
{
173+
vol.Required(
174+
CONF_BACKFILL_DAYS,
175+
default=self.config_entry.options.get(
176+
CONF_BACKFILL_DAYS, DEFAULT_BACKFILL_DAYS
177+
),
178+
): NumberSelector(
179+
NumberSelectorConfig(
180+
min=0,
181+
mode=NumberSelectorMode.BOX,
182+
step=1,
183+
)
184+
)
185+
}
186+
),
187+
)

homeassistant/components/ohme/const.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
from homeassistant.const import Platform
44

5+
CONF_BACKFILL_DAYS = "backfill_days"
6+
DEFAULT_BACKFILL_DAYS = 365
7+
DEFAULT_RECENT_SYNC_DAYS = 7
58
DOMAIN = "ohme"
69
PLATFORMS = [
710
Platform.BUTTON,

0 commit comments

Comments
 (0)