Skip to content

Commit f28e9f6

Browse files
Use runtime_data in pvpc_hourly_pricing (home-assistant#150565)
1 parent 6a4bf4e commit f28e9f6

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
"""The pvpc_hourly_pricing integration to collect Spain official electric prices."""
22

3-
from homeassistant.config_entries import ConfigEntry
43
from homeassistant.const import CONF_API_TOKEN, Platform
54
from homeassistant.core import HomeAssistant
65
from homeassistant.helpers import entity_registry as er
76

8-
from .const import ATTR_POWER, ATTR_POWER_P3, DOMAIN
9-
from .coordinator import ElecPricesDataUpdateCoordinator
7+
from .const import ATTR_POWER, ATTR_POWER_P3
8+
from .coordinator import ElecPricesDataUpdateCoordinator, PVPCConfigEntry
109
from .helpers import get_enabled_sensor_keys
1110

1211
PLATFORMS: list[Platform] = [Platform.SENSOR]
1312

1413

15-
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
14+
async def async_setup_entry(hass: HomeAssistant, entry: PVPCConfigEntry) -> bool:
1615
"""Set up pvpc hourly pricing from a config entry."""
1716
entity_registry = er.async_get(hass)
1817
sensor_keys = get_enabled_sensor_keys(
@@ -22,13 +21,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2221
coordinator = ElecPricesDataUpdateCoordinator(hass, entry, sensor_keys)
2322
await coordinator.async_config_entry_first_refresh()
2423

25-
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
24+
entry.runtime_data = coordinator
2625
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
2726
entry.async_on_unload(entry.add_update_listener(async_update_options))
2827
return True
2928

3029

31-
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
30+
async def async_update_options(hass: HomeAssistant, entry: PVPCConfigEntry) -> None:
3231
"""Handle options update."""
3332
if any(
3433
entry.data.get(attrib) != entry.options.get(attrib)
@@ -41,9 +40,6 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
4140
await hass.config_entries.async_reload(entry.entry_id)
4241

4342

44-
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
43+
async def async_unload_entry(hass: HomeAssistant, entry: PVPCConfigEntry) -> bool:
4544
"""Unload a config entry."""
46-
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
47-
if unload_ok:
48-
hass.data[DOMAIN].pop(entry.entry_id)
49-
return unload_ok
45+
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)

homeassistant/components/pvpc_hourly_pricing/coordinator.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@
1717

1818
_LOGGER = logging.getLogger(__name__)
1919

20+
type PVPCConfigEntry = ConfigEntry[ElecPricesDataUpdateCoordinator]
21+
2022

2123
class ElecPricesDataUpdateCoordinator(DataUpdateCoordinator[EsiosApiData]):
2224
"""Class to manage fetching Electricity prices data from API."""
2325

24-
config_entry: ConfigEntry
26+
config_entry: PVPCConfigEntry
2527

2628
def __init__(
27-
self, hass: HomeAssistant, entry: ConfigEntry, sensor_keys: set[str]
29+
self, hass: HomeAssistant, entry: PVPCConfigEntry, sensor_keys: set[str]
2830
) -> None:
2931
"""Initialize."""
3032
self.api = PVPCData(

homeassistant/components/pvpc_hourly_pricing/sensor.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
SensorEntityDescription,
1515
SensorStateClass,
1616
)
17-
from homeassistant.config_entries import ConfigEntry
1817
from homeassistant.const import CURRENCY_EURO, UnitOfEnergy
1918
from homeassistant.core import HomeAssistant, callback
2019
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
@@ -24,7 +23,7 @@
2423
from homeassistant.helpers.update_coordinator import CoordinatorEntity
2524

2625
from .const import DOMAIN
27-
from .coordinator import ElecPricesDataUpdateCoordinator
26+
from .coordinator import ElecPricesDataUpdateCoordinator, PVPCConfigEntry
2827
from .helpers import make_sensor_unique_id
2928

3029
_LOGGER = logging.getLogger(__name__)
@@ -149,11 +148,11 @@
149148

150149
async def async_setup_entry(
151150
hass: HomeAssistant,
152-
entry: ConfigEntry,
151+
entry: PVPCConfigEntry,
153152
async_add_entities: AddConfigEntryEntitiesCallback,
154153
) -> None:
155154
"""Set up the electricity price sensor from config_entry."""
156-
coordinator: ElecPricesDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
155+
coordinator = entry.runtime_data
157156
sensors = [ElecPriceSensor(coordinator, SENSOR_TYPES[0], entry.unique_id)]
158157
if coordinator.api.using_private_api:
159158
sensors.extend(

0 commit comments

Comments
 (0)