Skip to content
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 34 additions & 7 deletions homeassistant/components/ohme/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Set up ohme integration."""

import logging

from ohme import ApiException, AuthException, OhmeApiClient

from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
Expand All @@ -16,10 +18,13 @@
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."""
Expand Down Expand Up @@ -53,22 +58,44 @@ async def async_setup_entry(hass: HomeAssistant, entry: OhmeConfigEntry) -> bool
translation_key="api_failed", translation_domain=DOMAIN
) from e

coordinators = (
OhmeChargeSessionCoordinator(hass, entry, client),
OhmeDeviceInfoCoordinator(hass, entry, client),
charge_session_coordinator = OhmeChargeSessionCoordinator(hass, entry, client)
device_info_coordinator = OhmeDeviceInfoCoordinator(hass, entry, client)

if entry.unique_id != client.serial:
hass.config_entries.async_update_entry(entry, unique_id=client.serial)

entry.runtime_data = OhmeRuntimeData(
charge_session_coordinator=charge_session_coordinator,
device_info_coordinator=device_info_coordinator,
)

for coordinator in coordinators:
for coordinator in (charge_session_coordinator, device_info_coordinator):
await coordinator.async_config_entry_first_refresh()

entry.runtime_data = OhmeRuntimeData(*coordinators)

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.seed_history_sync_state()
charge_session_coordinator.enable_history_sync()
Comment thread
tstordyallison marked this conversation as resolved.
Comment thread
tstordyallison marked this conversation as resolved.

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)
Comment on lines +103 to 104


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")
54 changes: 51 additions & 3 deletions homeassistant/components/ohme/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
"""Config flow for ohme integration."""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any

from ohme import ApiException, AuthException, OhmeApiClient
import voluptuous as vol

from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
ConfigFlowResult,
OptionsFlowWithReload,
)
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD
from homeassistant.helpers.selector import (
NumberSelector,
NumberSelectorConfig,
NumberSelectorMode,
TextSelector,
TextSelectorConfig,
TextSelectorType,
)

from .const import DOMAIN
from .const import CONF_BACKFILL_DAYS, DEFAULT_BACKFILL_DAYS, DOMAIN

USER_SCHEMA = vol.Schema(
{
Expand Down Expand Up @@ -48,6 +58,11 @@
class OhmeConfigFlow(ConfigFlow, domain=DOMAIN):
"""Config flow."""

@staticmethod
def async_get_options_flow(config_entry: ConfigEntry) -> OhmeOptionsFlow:
"""Return the options flow."""
return OhmeOptionsFlow()

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
Expand All @@ -63,7 +78,9 @@ async def async_step_user(
)
if not errors:
return self.async_create_entry(
title=user_input[CONF_EMAIL], data=user_input
title=user_input[CONF_EMAIL],
data=user_input,
options={CONF_BACKFILL_DAYS: DEFAULT_BACKFILL_DAYS},
)

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

return errors


class OhmeOptionsFlow(OptionsFlowWithReload):
"""Handle Ohme options."""

async def async_step_init(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Manage integration options."""
if user_input is not None:
return self.async_create_entry(title="", data=user_input)

return self.async_show_form(
step_id="init",
data_schema=vol.Schema(
{
vol.Required(
CONF_BACKFILL_DAYS,
default=self.config_entry.options.get(
CONF_BACKFILL_DAYS, DEFAULT_BACKFILL_DAYS
),
): NumberSelector(
NumberSelectorConfig(
min=0,
mode=NumberSelectorMode.BOX,
step=1,
)
)
}
),
)
3 changes: 3 additions & 0 deletions homeassistant/components/ohme/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

from homeassistant.const import Platform

CONF_BACKFILL_DAYS = "backfill_days"
DEFAULT_BACKFILL_DAYS = 365
DEFAULT_RECENT_SYNC_DAYS = 7
DOMAIN = "ohme"
PLATFORMS = [
Platform.BUTTON,
Expand Down
Loading
Loading