From 5a01521ff831db08f0403c5b78361c607719b8cd Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 6 May 2025 12:06:00 +0200 Subject: [PATCH] Use runtime_data in geonetnz_volcano (#144320) --- .../components/geonetnz_volcano/__init__.py | 19 ++++++++++--------- .../components/geonetnz_volcano/const.py | 2 -- .../components/geonetnz_volcano/sensor.py | 8 +++----- .../components/geonetnz_volcano/test_init.py | 4 +--- 4 files changed, 14 insertions(+), 19 deletions(-) diff --git a/homeassistant/components/geonetnz_volcano/__init__.py b/homeassistant/components/geonetnz_volcano/__init__.py index b08d6d62c55..c3ceeab33f8 100644 --- a/homeassistant/components/geonetnz_volcano/__init__.py +++ b/homeassistant/components/geonetnz_volcano/__init__.py @@ -29,7 +29,6 @@ from .const import ( DEFAULT_RADIUS, DEFAULT_SCAN_INTERVAL, DOMAIN, - FEED, IMPERIAL_UNITS, PLATFORMS, ) @@ -52,6 +51,8 @@ CONFIG_SCHEMA = vol.Schema( extra=vol.ALLOW_EXTRA, ) +type GeonetnzVolcanoConfigEntry = ConfigEntry[GeonetnzVolcanoFeedEntityManager] + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the GeoNet NZ Volcano component.""" @@ -84,11 +85,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: return True -async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: +async def async_setup_entry( + hass: HomeAssistant, config_entry: GeonetnzVolcanoConfigEntry +) -> bool: """Set up the GeoNet NZ Volcano component as config entry.""" - hass.data.setdefault(DOMAIN, {}) - hass.data[DOMAIN].setdefault(FEED, {}) - radius = config_entry.data[CONF_RADIUS] unit_system = config_entry.data[CONF_UNIT_SYSTEM] if unit_system == IMPERIAL_UNITS: @@ -97,16 +97,17 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b ) # Create feed entity manager for all platforms. manager = GeonetnzVolcanoFeedEntityManager(hass, config_entry, radius, unit_system) - hass.data[DOMAIN][FEED][config_entry.entry_id] = manager + config_entry.runtime_data = manager _LOGGER.debug("Feed entity manager added for %s", config_entry.entry_id) await manager.async_init() return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry( + hass: HomeAssistant, entry: GeonetnzVolcanoConfigEntry +) -> bool: """Unload an GeoNet NZ Volcano component config entry.""" - manager = hass.data[DOMAIN][FEED].pop(entry.entry_id) - await manager.async_stop() + await entry.runtime_data.async_stop() return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/geonetnz_volcano/const.py b/homeassistant/components/geonetnz_volcano/const.py index be04a25d27a..98ac69fec19 100644 --- a/homeassistant/components/geonetnz_volcano/const.py +++ b/homeassistant/components/geonetnz_volcano/const.py @@ -6,8 +6,6 @@ from homeassistant.const import Platform DOMAIN = "geonetnz_volcano" -FEED = "feed" - ATTR_ACTIVITY = "activity" ATTR_DISTANCE = "distance" ATTR_EXTERNAL_ID = "external_id" diff --git a/homeassistant/components/geonetnz_volcano/sensor.py b/homeassistant/components/geonetnz_volcano/sensor.py index bde04acb895..159806778ce 100644 --- a/homeassistant/components/geonetnz_volcano/sensor.py +++ b/homeassistant/components/geonetnz_volcano/sensor.py @@ -5,7 +5,6 @@ from __future__ import annotations import logging from homeassistant.components.sensor import SensorEntity -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, UnitOfLength from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -13,14 +12,13 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util import dt as dt_util from homeassistant.util.unit_conversion import DistanceConverter +from . import GeonetnzVolcanoConfigEntry from .const import ( ATTR_ACTIVITY, ATTR_DISTANCE, ATTR_EXTERNAL_ID, ATTR_HAZARDS, DEFAULT_ICON, - DOMAIN, - FEED, IMPERIAL_UNITS, ) @@ -32,11 +30,11 @@ ATTR_LAST_UPDATE_SUCCESSFUL = "feed_last_update_successful" async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: GeonetnzVolcanoConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up the GeoNet NZ Volcano Feed platform.""" - manager = hass.data[DOMAIN][FEED][entry.entry_id] + manager = entry.runtime_data @callback def async_add_sensor(feed_manager, external_id, unit_system): diff --git a/tests/components/geonetnz_volcano/test_init.py b/tests/components/geonetnz_volcano/test_init.py index fe113434dc6..49b4af2abec 100644 --- a/tests/components/geonetnz_volcano/test_init.py +++ b/tests/components/geonetnz_volcano/test_init.py @@ -2,7 +2,6 @@ from unittest.mock import AsyncMock, patch -from homeassistant.components.geonetnz_volcano import DOMAIN, FEED from homeassistant.core import HomeAssistant @@ -17,8 +16,7 @@ async def test_component_unload_config_entry(hass: HomeAssistant, config_entry) assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert mock_feed_manager_update.call_count == 1 - assert hass.data[DOMAIN][FEED][config_entry.entry_id] is not None + # Unload config entry. assert await hass.config_entries.async_unload(config_entry.entry_id) await hass.async_block_till_done() - assert hass.data[DOMAIN][FEED].get(config_entry.entry_id) is None