Use runtime_data in pvpc_hourly_pricing (#150565)

This commit is contained in:
G Johansson
2025-08-14 01:03:04 +02:00
committed by GitHub
parent 6a4bf4ec72
commit f28e9f60ee
3 changed files with 14 additions and 17 deletions

View File

@@ -1,18 +1,17 @@
"""The pvpc_hourly_pricing integration to collect Spain official electric prices.""" """The pvpc_hourly_pricing integration to collect Spain official electric prices."""
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_TOKEN, Platform from homeassistant.const import CONF_API_TOKEN, Platform
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er from homeassistant.helpers import entity_registry as er
from .const import ATTR_POWER, ATTR_POWER_P3, DOMAIN from .const import ATTR_POWER, ATTR_POWER_P3
from .coordinator import ElecPricesDataUpdateCoordinator from .coordinator import ElecPricesDataUpdateCoordinator, PVPCConfigEntry
from .helpers import get_enabled_sensor_keys from .helpers import get_enabled_sensor_keys
PLATFORMS: list[Platform] = [Platform.SENSOR] PLATFORMS: list[Platform] = [Platform.SENSOR]
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: PVPCConfigEntry) -> bool:
"""Set up pvpc hourly pricing from a config entry.""" """Set up pvpc hourly pricing from a config entry."""
entity_registry = er.async_get(hass) entity_registry = er.async_get(hass)
sensor_keys = get_enabled_sensor_keys( sensor_keys = get_enabled_sensor_keys(
@@ -22,13 +21,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
coordinator = ElecPricesDataUpdateCoordinator(hass, entry, sensor_keys) coordinator = ElecPricesDataUpdateCoordinator(hass, entry, sensor_keys)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
entry.async_on_unload(entry.add_update_listener(async_update_options)) entry.async_on_unload(entry.add_update_listener(async_update_options))
return True return True
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None: async def async_update_options(hass: HomeAssistant, entry: PVPCConfigEntry) -> None:
"""Handle options update.""" """Handle options update."""
if any( if any(
entry.data.get(attrib) != entry.options.get(attrib) entry.data.get(attrib) != entry.options.get(attrib)
@@ -41,9 +40,6 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: PVPCConfigEntry) -> bool:
"""Unload a config entry.""" """Unload a config entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@@ -17,14 +17,16 @@ from .const import ATTR_POWER, ATTR_POWER_P3, ATTR_TARIFF, DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type PVPCConfigEntry = ConfigEntry[ElecPricesDataUpdateCoordinator]
class ElecPricesDataUpdateCoordinator(DataUpdateCoordinator[EsiosApiData]): class ElecPricesDataUpdateCoordinator(DataUpdateCoordinator[EsiosApiData]):
"""Class to manage fetching Electricity prices data from API.""" """Class to manage fetching Electricity prices data from API."""
config_entry: ConfigEntry config_entry: PVPCConfigEntry
def __init__( def __init__(
self, hass: HomeAssistant, entry: ConfigEntry, sensor_keys: set[str] self, hass: HomeAssistant, entry: PVPCConfigEntry, sensor_keys: set[str]
) -> None: ) -> None:
"""Initialize.""" """Initialize."""
self.api = PVPCData( self.api = PVPCData(

View File

@@ -14,7 +14,6 @@ from homeassistant.components.sensor import (
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CURRENCY_EURO, UnitOfEnergy from homeassistant.const import CURRENCY_EURO, UnitOfEnergy
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
@@ -24,7 +23,7 @@ from homeassistant.helpers.typing import StateType
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from .const import DOMAIN
from .coordinator import ElecPricesDataUpdateCoordinator from .coordinator import ElecPricesDataUpdateCoordinator, PVPCConfigEntry
from .helpers import make_sensor_unique_id from .helpers import make_sensor_unique_id
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@@ -149,11 +148,11 @@ _PRICE_SENSOR_ATTRIBUTES_MAP = {
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
entry: ConfigEntry, entry: PVPCConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback, async_add_entities: AddConfigEntryEntitiesCallback,
) -> None: ) -> None:
"""Set up the electricity price sensor from config_entry.""" """Set up the electricity price sensor from config_entry."""
coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator = entry.runtime_data
sensors = [ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)] sensors = [ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)]
if coordinator.api.using_private_api: if coordinator.api.using_private_api:
sensors.extend( sensors.extend(