mirror of
https://github.com/home-assistant/core.git
synced 2026-09-06 13:32:08 +01:00
Use runtime_data in ovo_energy (#167141)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
2179a5405a
commit
b11292385f
@@ -7,21 +7,20 @@ import logging
|
||||
import aiohttp
|
||||
from ovoenergy import OVOEnergy
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_PASSWORD, 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_ACCOUNT, DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
||||
from .coordinator import OVOEnergyDataUpdateCoordinator
|
||||
from .const import CONF_ACCOUNT
|
||||
from .coordinator import OVOEnergyConfigEntry, OVOEnergyDataUpdateCoordinator
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: OVOEnergyConfigEntry) -> bool:
|
||||
"""Set up OVO Energy from a config entry."""
|
||||
|
||||
client = OVOEnergy(
|
||||
@@ -45,26 +44,15 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
||||
coordinator = OVOEnergyDataUpdateCoordinator(hass, entry, client)
|
||||
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
hass.data[DOMAIN][entry.entry_id] = {
|
||||
DATA_CLIENT: client,
|
||||
DATA_COORDINATOR: coordinator,
|
||||
}
|
||||
|
||||
# Fetch initial data so we have data when entities subscribe
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
# Setup components
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: OVOEnergyConfigEntry) -> bool:
|
||||
"""Unload OVO Energy config entry."""
|
||||
# Unload sensors
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
@@ -2,6 +2,4 @@
|
||||
|
||||
DOMAIN = "ovo_energy"
|
||||
|
||||
DATA_CLIENT = "ovo_client"
|
||||
DATA_COORDINATOR = "coordinator"
|
||||
CONF_ACCOUNT = "account"
|
||||
|
||||
@@ -21,16 +21,18 @@ from .const import CONF_ACCOUNT
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
type OVOEnergyConfigEntry = ConfigEntry[OVOEnergyDataUpdateCoordinator]
|
||||
|
||||
|
||||
class OVOEnergyDataUpdateCoordinator(DataUpdateCoordinator[OVODailyUsage]):
|
||||
"""Class to manage fetching OVO Energy data."""
|
||||
|
||||
config_entry: ConfigEntry
|
||||
config_entry: OVOEnergyConfigEntry
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
config_entry: OVOEnergyConfigEntry,
|
||||
client: OVOEnergy,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from ovoenergy import OVOEnergy
|
||||
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
@@ -16,15 +14,6 @@ class OVOEnergyEntity(CoordinatorEntity[OVOEnergyDataUpdateCoordinator]):
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: OVOEnergyDataUpdateCoordinator,
|
||||
client: OVOEnergy,
|
||||
) -> None:
|
||||
"""Initialize the OVO Energy entity."""
|
||||
super().__init__(coordinator)
|
||||
self._client = client
|
||||
|
||||
|
||||
class OVOEnergyDeviceEntity(OVOEnergyEntity):
|
||||
"""Defines a OVO Energy device entity."""
|
||||
@@ -34,7 +23,7 @@ class OVOEnergyDeviceEntity(OVOEnergyEntity):
|
||||
"""Return device information about this OVO Energy instance."""
|
||||
return DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
identifiers={(DOMAIN, self._client.account_id)},
|
||||
identifiers={(DOMAIN, self.coordinator.client.account_id)},
|
||||
manufacturer="OVO Energy",
|
||||
name=self._client.username,
|
||||
name=self.coordinator.client.username,
|
||||
)
|
||||
|
||||
@@ -7,7 +7,6 @@ import dataclasses
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Final
|
||||
|
||||
from ovoenergy import OVOEnergy
|
||||
from ovoenergy.models import OVODailyUsage
|
||||
|
||||
from homeassistant.components.sensor import (
|
||||
@@ -16,15 +15,14 @@ from homeassistant.components.sensor import (
|
||||
SensorEntityDescription,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import UnitOfEnergy
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from .const import DATA_CLIENT, DATA_COORDINATOR, DOMAIN
|
||||
from .coordinator import OVOEnergyDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from .coordinator import OVOEnergyConfigEntry, OVOEnergyDataUpdateCoordinator
|
||||
from .entity import OVOEnergyDeviceEntity
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=300)
|
||||
@@ -114,14 +112,11 @@ SENSOR_TYPES_GAS: tuple[OVOEnergySensorEntityDescription, ...] = (
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: OVOEnergyConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up OVO Energy sensor based on a config entry."""
|
||||
coordinator: OVOEnergyDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
||||
DATA_COORDINATOR
|
||||
]
|
||||
client: OVOEnergy = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
|
||||
coordinator = entry.runtime_data
|
||||
|
||||
entities = []
|
||||
|
||||
@@ -139,7 +134,7 @@ async def async_setup_entry(
|
||||
coordinator.data.electricity[-1].cost.currency_unit
|
||||
),
|
||||
)
|
||||
entities.append(OVOEnergySensor(coordinator, description, client))
|
||||
entities.append(OVOEnergySensor(coordinator, description))
|
||||
if coordinator.data.gas:
|
||||
for description in SENSOR_TYPES_GAS:
|
||||
if (
|
||||
@@ -153,7 +148,7 @@ async def async_setup_entry(
|
||||
-1
|
||||
].cost.currency_unit,
|
||||
)
|
||||
entities.append(OVOEnergySensor(coordinator, description, client))
|
||||
entities.append(OVOEnergySensor(coordinator, description))
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
@@ -167,11 +162,12 @@ class OVOEnergySensor(OVOEnergyDeviceEntity, SensorEntity):
|
||||
self,
|
||||
coordinator: OVOEnergyDataUpdateCoordinator,
|
||||
description: OVOEnergySensorEntityDescription,
|
||||
client: OVOEnergy,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator, client)
|
||||
self._attr_unique_id = f"{DOMAIN}_{client.account_id}_{description.key}"
|
||||
super().__init__(coordinator)
|
||||
self._attr_unique_id = (
|
||||
f"{DOMAIN}_{coordinator.client.account_id}_{description.key}"
|
||||
)
|
||||
self.entity_description = description
|
||||
|
||||
@property
|
||||
|
||||
Reference in New Issue
Block a user