mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 06:47:09 +00:00
Use runtime_data in gdacs (#144309)
This commit is contained in:
parent
5df3a9d76d
commit
33da5465bd
@ -25,22 +25,17 @@ from homeassistant.helpers.event import async_track_time_interval
|
|||||||
from homeassistant.util.unit_conversion import DistanceConverter
|
from homeassistant.util.unit_conversion import DistanceConverter
|
||||||
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||||
|
|
||||||
from .const import ( # noqa: F401
|
from .const import CONF_CATEGORIES, DEFAULT_SCAN_INTERVAL, PLATFORMS # noqa: F401
|
||||||
CONF_CATEGORIES,
|
|
||||||
DEFAULT_SCAN_INTERVAL,
|
|
||||||
DOMAIN,
|
|
||||||
FEED,
|
|
||||||
PLATFORMS,
|
|
||||||
)
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
type GdacsConfigEntry = ConfigEntry[GdacsFeedEntityManager]
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, config_entry: GdacsConfigEntry
|
||||||
|
) -> bool:
|
||||||
"""Set up the GDACS component as config entry."""
|
"""Set up the GDACS component as config entry."""
|
||||||
hass.data.setdefault(DOMAIN, {})
|
|
||||||
feeds = hass.data[DOMAIN].setdefault(FEED, {})
|
|
||||||
|
|
||||||
radius = config_entry.data[CONF_RADIUS]
|
radius = config_entry.data[CONF_RADIUS]
|
||||||
if hass.config.units is US_CUSTOMARY_SYSTEM:
|
if hass.config.units is US_CUSTOMARY_SYSTEM:
|
||||||
radius = DistanceConverter.convert(
|
radius = DistanceConverter.convert(
|
||||||
@ -48,16 +43,15 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
)
|
)
|
||||||
# Create feed entity manager for all platforms.
|
# Create feed entity manager for all platforms.
|
||||||
manager = GdacsFeedEntityManager(hass, config_entry, radius)
|
manager = GdacsFeedEntityManager(hass, config_entry, radius)
|
||||||
feeds[config_entry.entry_id] = manager
|
config_entry.runtime_data = manager
|
||||||
_LOGGER.debug("Feed entity manager added for %s", config_entry.entry_id)
|
_LOGGER.debug("Feed entity manager added for %s", config_entry.entry_id)
|
||||||
await manager.async_init()
|
await manager.async_init()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: GdacsConfigEntry) -> bool:
|
||||||
"""Unload an GDACS component config entry."""
|
"""Unload an GDACS component config entry."""
|
||||||
manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED].pop(entry.entry_id)
|
await entry.runtime_data.async_stop()
|
||||||
await manager.async_stop()
|
|
||||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||||
|
|
||||||
|
|
||||||
@ -65,7 +59,7 @@ class GdacsFeedEntityManager:
|
|||||||
"""Feed Entity Manager for GDACS feed."""
|
"""Feed Entity Manager for GDACS feed."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, hass: HomeAssistant, config_entry: ConfigEntry, radius_in_km: float
|
self, hass: HomeAssistant, config_entry: GdacsConfigEntry, radius_in_km: float
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Feed Entity Manager."""
|
"""Initialize the Feed Entity Manager."""
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
|
@ -10,8 +10,6 @@ DOMAIN = "gdacs"
|
|||||||
|
|
||||||
PLATFORMS = [Platform.GEO_LOCATION, Platform.SENSOR]
|
PLATFORMS = [Platform.GEO_LOCATION, Platform.SENSOR]
|
||||||
|
|
||||||
FEED = "feed"
|
|
||||||
|
|
||||||
CONF_CATEGORIES = "categories"
|
CONF_CATEGORIES = "categories"
|
||||||
|
|
||||||
DEFAULT_ICON = "mdi:alert"
|
DEFAULT_ICON = "mdi:alert"
|
||||||
|
@ -7,26 +7,23 @@ from typing import Any
|
|||||||
from aio_georss_client.status_update import StatusUpdate
|
from aio_georss_client.status_update import StatusUpdate
|
||||||
|
|
||||||
from homeassistant.components.diagnostics import async_redact_data
|
from homeassistant.components.diagnostics import async_redact_data
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from . import GdacsFeedEntityManager
|
from . import GdacsConfigEntry
|
||||||
from .const import DOMAIN, FEED
|
|
||||||
|
|
||||||
TO_REDACT = {CONF_LATITUDE, CONF_LONGITUDE}
|
TO_REDACT = {CONF_LATITUDE, CONF_LONGITUDE}
|
||||||
|
|
||||||
|
|
||||||
async def async_get_config_entry_diagnostics(
|
async def async_get_config_entry_diagnostics(
|
||||||
hass: HomeAssistant, config_entry: ConfigEntry
|
hass: HomeAssistant, config_entry: GdacsConfigEntry
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
"""Return diagnostics for a config entry."""
|
"""Return diagnostics for a config entry."""
|
||||||
data: dict[str, Any] = {
|
data: dict[str, Any] = {
|
||||||
"info": async_redact_data(config_entry.data, TO_REDACT),
|
"info": async_redact_data(config_entry.data, TO_REDACT),
|
||||||
}
|
}
|
||||||
|
|
||||||
manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED][config_entry.entry_id]
|
status_info: StatusUpdate = config_entry.runtime_data.status_info()
|
||||||
status_info: StatusUpdate = manager.status_info()
|
|
||||||
if status_info:
|
if status_info:
|
||||||
data["service"] = {
|
data["service"] = {
|
||||||
"status": status_info.status,
|
"status": status_info.status,
|
||||||
|
@ -10,7 +10,6 @@ from typing import Any
|
|||||||
from aio_georss_gdacs.feed_entry import GdacsFeedEntry
|
from aio_georss_gdacs.feed_entry import GdacsFeedEntry
|
||||||
|
|
||||||
from homeassistant.components.geo_location import GeolocationEvent
|
from homeassistant.components.geo_location import GeolocationEvent
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import UnitOfLength
|
from homeassistant.const import UnitOfLength
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import entity_registry as er
|
from homeassistant.helpers import entity_registry as er
|
||||||
@ -19,8 +18,8 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|||||||
from homeassistant.util.unit_conversion import DistanceConverter
|
from homeassistant.util.unit_conversion import DistanceConverter
|
||||||
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM
|
||||||
|
|
||||||
from . import GdacsFeedEntityManager
|
from . import GdacsConfigEntry, GdacsFeedEntityManager
|
||||||
from .const import DEFAULT_ICON, DOMAIN, FEED
|
from .const import DEFAULT_ICON
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -53,11 +52,11 @@ SOURCE = "gdacs"
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: GdacsConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the GDACS Feed platform."""
|
"""Set up the GDACS Feed platform."""
|
||||||
manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED][entry.entry_id]
|
manager = entry.runtime_data
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_add_geolocation(
|
def async_add_geolocation(
|
||||||
|
@ -10,15 +10,14 @@ from typing import Any
|
|||||||
from aio_georss_client.status_update import StatusUpdate
|
from aio_georss_client.status_update import StatusUpdate
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
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
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
from . import GdacsFeedEntityManager
|
from . import GdacsConfigEntry, GdacsFeedEntityManager
|
||||||
from .const import DOMAIN, FEED
|
from .const import DOMAIN
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -38,12 +37,11 @@ PARALLEL_UPDATES = 0
|
|||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
entry: ConfigEntry,
|
entry: GdacsConfigEntry,
|
||||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the GDACS Feed platform."""
|
"""Set up the GDACS Feed platform."""
|
||||||
manager: GdacsFeedEntityManager = hass.data[DOMAIN][FEED][entry.entry_id]
|
sensor = GdacsSensor(entry, entry.runtime_data)
|
||||||
sensor = GdacsSensor(entry, manager)
|
|
||||||
async_add_entities([sensor])
|
async_add_entities([sensor])
|
||||||
|
|
||||||
|
|
||||||
@ -57,7 +55,7 @@ class GdacsSensor(SensorEntity):
|
|||||||
_attr_translation_key = "alerts"
|
_attr_translation_key = "alerts"
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, config_entry: ConfigEntry, manager: GdacsFeedEntityManager
|
self, config_entry: GdacsConfigEntry, manager: GdacsFeedEntityManager
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize entity."""
|
"""Initialize entity."""
|
||||||
assert config_entry.unique_id
|
assert config_entry.unique_id
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.gdacs import CONF_CATEGORIES, DOMAIN
|
from homeassistant.components.gdacs.const import CONF_CATEGORIES, DOMAIN
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_LATITUDE,
|
CONF_LATITUDE,
|
||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
|
@ -5,7 +5,7 @@ from unittest.mock import patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.gdacs import CONF_CATEGORIES, DOMAIN
|
from homeassistant.components.gdacs.const import CONF_CATEGORIES, DOMAIN
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_LATITUDE,
|
CONF_LATITUDE,
|
||||||
CONF_LONGITUDE,
|
CONF_LONGITUDE,
|
||||||
|
@ -5,7 +5,7 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from homeassistant.components.gdacs import DEFAULT_SCAN_INTERVAL, DOMAIN, FEED
|
from homeassistant.components.gdacs.const import DEFAULT_SCAN_INTERVAL
|
||||||
from homeassistant.components.gdacs.geo_location import (
|
from homeassistant.components.gdacs.geo_location import (
|
||||||
ATTR_ALERT_LEVEL,
|
ATTR_ALERT_LEVEL,
|
||||||
ATTR_COUNTRY,
|
ATTR_COUNTRY,
|
||||||
@ -251,10 +251,7 @@ async def test_setup_imperial(
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Test conversion of 200 miles to kilometers.
|
# Test conversion of 200 miles to kilometers.
|
||||||
feeds = hass.data[DOMAIN][FEED]
|
manager = config_entry.runtime_data
|
||||||
assert feeds is not None
|
|
||||||
assert len(feeds) == 1
|
|
||||||
manager = list(feeds.values())[0]
|
|
||||||
# Ensure that the filter value in km is correctly set.
|
# Ensure that the filter value in km is correctly set.
|
||||||
assert manager._feed_manager._feed._filter_radius == 321.8688
|
assert manager._feed_manager._feed._filter_radius == 321.8688
|
||||||
|
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
from homeassistant.components.gdacs import DOMAIN, FEED
|
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
|
||||||
@ -14,8 +13,7 @@ async def test_component_unload_config_entry(hass: HomeAssistant, config_entry)
|
|||||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert mock_feed_manager_update.call_count == 1
|
assert mock_feed_manager_update.call_count == 1
|
||||||
assert hass.data[DOMAIN][FEED][config_entry.entry_id] is not None
|
|
||||||
# Unload config entry.
|
# Unload config entry.
|
||||||
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
assert hass.data[DOMAIN][FEED].get(config_entry.entry_id) is None
|
|
||||||
|
@ -4,9 +4,8 @@ from unittest.mock import patch
|
|||||||
|
|
||||||
from freezegun import freeze_time
|
from freezegun import freeze_time
|
||||||
|
|
||||||
from homeassistant.components import gdacs
|
|
||||||
from homeassistant.components.gdacs import DEFAULT_SCAN_INTERVAL
|
from homeassistant.components.gdacs import DEFAULT_SCAN_INTERVAL
|
||||||
from homeassistant.components.gdacs.const import CONF_CATEGORIES
|
from homeassistant.components.gdacs.const import CONF_CATEGORIES, DOMAIN
|
||||||
from homeassistant.components.gdacs.sensor import (
|
from homeassistant.components.gdacs.sensor import (
|
||||||
ATTR_CREATED,
|
ATTR_CREATED,
|
||||||
ATTR_LAST_UPDATE,
|
ATTR_LAST_UPDATE,
|
||||||
@ -73,7 +72,7 @@ async def test_setup(hass: HomeAssistant) -> None:
|
|||||||
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL.seconds,
|
CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL.seconds,
|
||||||
}
|
}
|
||||||
config_entry = MockConfigEntry(
|
config_entry = MockConfigEntry(
|
||||||
domain=gdacs.DOMAIN,
|
domain=DOMAIN,
|
||||||
title=f"{latitude}, {longitude}",
|
title=f"{latitude}, {longitude}",
|
||||||
data=entry_data,
|
data=entry_data,
|
||||||
unique_id="my_very_unique_id",
|
unique_id="my_very_unique_id",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user