diff --git a/homeassistant/components/gdacs/__init__.py b/homeassistant/components/gdacs/__init__.py index 557af9474ed2..1530e4712d8c 100644 --- a/homeassistant/components/gdacs/__init__.py +++ b/homeassistant/components/gdacs/__init__.py @@ -3,9 +3,8 @@ from datetime import timedelta import logging from aio_georss_gdacs import GdacsFeedManager -import voluptuous as vol -from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry +from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_LATITUDE, CONF_LONGITUDE, @@ -14,71 +13,22 @@ from homeassistant.const import ( UnitOfLength, ) from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import aiohttp_client, config_validation as cv +from homeassistant.helpers import aiohttp_client from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.event import async_track_time_interval -from homeassistant.helpers.typing import ConfigType from homeassistant.util.unit_conversion import DistanceConverter from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM -from .const import ( +from .const import ( # noqa: F401 CONF_CATEGORIES, - DEFAULT_RADIUS, DEFAULT_SCAN_INTERVAL, DOMAIN, FEED, PLATFORMS, - VALID_CATEGORIES, ) _LOGGER = logging.getLogger(__name__) -CONFIG_SCHEMA = vol.Schema( - { - DOMAIN: vol.Schema( - { - vol.Inclusive(CONF_LATITUDE, "coordinates"): cv.latitude, - vol.Inclusive(CONF_LONGITUDE, "coordinates"): cv.longitude, - vol.Optional(CONF_RADIUS, default=DEFAULT_RADIUS): vol.Coerce(float), - vol.Optional( - CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL - ): cv.time_period, - vol.Optional(CONF_CATEGORIES, default=[]): vol.All( - cv.ensure_list, [vol.In(VALID_CATEGORIES)] - ), - } - ) - }, - extra=vol.ALLOW_EXTRA, -) - - -async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: - """Set up the GDACS component.""" - if DOMAIN not in config: - return True - - conf = config[DOMAIN] - latitude = conf.get(CONF_LATITUDE, hass.config.latitude) - longitude = conf.get(CONF_LONGITUDE, hass.config.longitude) - scan_interval = conf[CONF_SCAN_INTERVAL] - categories = conf[CONF_CATEGORIES] - - hass.async_create_task( - hass.config_entries.flow.async_init( - DOMAIN, - context={"source": SOURCE_IMPORT}, - data={ - CONF_LATITUDE: latitude, - CONF_LONGITUDE: longitude, - CONF_RADIUS: conf[CONF_RADIUS], - CONF_SCAN_INTERVAL: scan_interval, - CONF_CATEGORIES: categories, - }, - ) - ) - return True - async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Set up the GDACS component as config entry.""" diff --git a/homeassistant/components/gdacs/config_flow.py b/homeassistant/components/gdacs/config_flow.py index fb2b84169376..b59b3bcc7750 100644 --- a/homeassistant/components/gdacs/config_flow.py +++ b/homeassistant/components/gdacs/config_flow.py @@ -10,10 +10,7 @@ from homeassistant.const import ( CONF_RADIUS, CONF_SCAN_INTERVAL, ) -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN -from homeassistant.data_entry_flow import AbortFlow, FlowResultType from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue from .const import CONF_CATEGORIES, DEFAULT_RADIUS, DEFAULT_SCAN_INTERVAL, DOMAIN @@ -33,26 +30,6 @@ class GdacsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): step_id="user", data_schema=DATA_SCHEMA, errors=errors or {} ) - async def async_step_import(self, import_config): - """Import a config entry from configuration.yaml.""" - result = await self.async_step_user(import_config) - if result["type"] == FlowResultType.CREATE_ENTRY: - async_create_issue( - self.hass, - HOMEASSISTANT_DOMAIN, - f"deprecated_yaml_{DOMAIN}", - breaks_in_ha_version="2024.2.0", - is_fixable=False, - issue_domain=DOMAIN, - severity=IssueSeverity.WARNING, - translation_key="deprecated_yaml", - translation_placeholders={ - "domain": DOMAIN, - "integration_title": "Global Disaster Alert and Coordination System", - }, - ) - return result - async def async_step_user(self, user_input=None): """Handle the start of the config flow.""" _LOGGER.debug("User input: %s", user_input) @@ -67,25 +44,7 @@ class GdacsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): identifier = f"{user_input[CONF_LATITUDE]}, {user_input[CONF_LONGITUDE]}" await self.async_set_unique_id(identifier) - try: - self._abort_if_unique_id_configured() - except AbortFlow: - if self.context["source"] == config_entries.SOURCE_IMPORT: - async_create_issue( - self.hass, - HOMEASSISTANT_DOMAIN, - f"deprecated_yaml_{DOMAIN}", - breaks_in_ha_version="2024.2.0", - is_fixable=False, - issue_domain=DOMAIN, - severity=IssueSeverity.WARNING, - translation_key="deprecated_yaml", - translation_placeholders={ - "domain": DOMAIN, - "integration_title": "Global Disaster Alert and Coordination System", - }, - ) - raise + self._abort_if_unique_id_configured() scan_interval = user_input.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL) user_input[CONF_SCAN_INTERVAL] = scan_interval.total_seconds() diff --git a/tests/components/gdacs/conftest.py b/tests/components/gdacs/conftest.py index 47185cf53870..ee82a3131b13 100644 --- a/tests/components/gdacs/conftest.py +++ b/tests/components/gdacs/conftest.py @@ -14,7 +14,7 @@ from tests.common import MockConfigEntry @pytest.fixture -def config_entry(): +def config_entry() -> MockConfigEntry: """Create a mock GDACS config entry.""" return MockConfigEntry( domain=DOMAIN, diff --git a/tests/components/gdacs/test_config_flow.py b/tests/components/gdacs/test_config_flow.py index f8dfa0cd7fd5..ad673815acef 100644 --- a/tests/components/gdacs/test_config_flow.py +++ b/tests/components/gdacs/test_config_flow.py @@ -1,5 +1,4 @@ """Define tests for the GDACS config flow.""" -from datetime import timedelta from unittest.mock import patch import pytest @@ -12,8 +11,7 @@ from homeassistant.const import ( CONF_RADIUS, CONF_SCAN_INTERVAL, ) -from homeassistant.core import DOMAIN as HOMEASSISTANT_DOMAIN, HomeAssistant -import homeassistant.helpers.issue_registry as ir +from homeassistant.core import HomeAssistant @pytest.fixture(name="gdacs_setup", autouse=True) @@ -44,56 +42,6 @@ async def test_show_form(hass: HomeAssistant) -> None: assert result["step_id"] == "user" -async def test_step_import(hass: HomeAssistant) -> None: - """Test that the import step works.""" - conf = { - CONF_LATITUDE: -41.2, - CONF_LONGITUDE: 174.7, - CONF_RADIUS: 25, - CONF_SCAN_INTERVAL: timedelta(minutes=4), - CONF_CATEGORIES: ["Drought", "Earthquake"], - } - - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=conf - ) - assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY - assert result["title"] == "-41.2, 174.7" - assert result["data"] == { - CONF_LATITUDE: -41.2, - CONF_LONGITUDE: 174.7, - CONF_RADIUS: 25, - CONF_SCAN_INTERVAL: 240.0, - CONF_CATEGORIES: ["Drought", "Earthquake"], - } - - issue_registry = ir.async_get(hass) - issue = issue_registry.async_get_issue( - HOMEASSISTANT_DOMAIN, "deprecated_yaml_gdacs" - ) - assert issue.translation_key == "deprecated_yaml" - - -async def test_step_import_already_exist( - hass: HomeAssistant, config_entry: config_entries.ConfigEntry -) -> None: - """Test that errors are shown when duplicates are added.""" - conf = {CONF_LATITUDE: -41.2, CONF_LONGITUDE: 174.7, CONF_RADIUS: 25} - config_entry.add_to_hass(hass) - - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=conf - ) - assert result["type"] == data_entry_flow.FlowResultType.ABORT - assert result["reason"] == "already_configured" - - issue_registry = ir.async_get(hass) - issue = issue_registry.async_get_issue( - HOMEASSISTANT_DOMAIN, "deprecated_yaml_gdacs" - ) - assert issue.translation_key == "deprecated_yaml" - - async def test_step_user(hass: HomeAssistant) -> None: """Test that the user step works.""" hass.config.latitude = -41.2 diff --git a/tests/components/gdacs/test_geo_location.py b/tests/components/gdacs/test_geo_location.py index dfdce7635df8..9fd8c5c0134f 100644 --- a/tests/components/gdacs/test_geo_location.py +++ b/tests/components/gdacs/test_geo_location.py @@ -4,7 +4,6 @@ from unittest.mock import patch from freezegun import freeze_time -from homeassistant.components import gdacs from homeassistant.components.gdacs import DEFAULT_SCAN_INTERVAL, DOMAIN, FEED from homeassistant.components.gdacs.geo_location import ( ATTR_ALERT_LEVEL, @@ -33,18 +32,21 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM from . import _generate_mock_feed_entry -from tests.common import async_fire_time_changed +from tests.common import MockConfigEntry, async_fire_time_changed -CONFIG = {gdacs.DOMAIN: {CONF_RADIUS: 200}} +CONFIG = {CONF_RADIUS: 200} -async def test_setup(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> None: +async def test_setup( + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + config_entry: MockConfigEntry, +) -> None: """Test the general setup of the integration.""" # Set up some mock feed entries for this test. mock_entry_1 = _generate_mock_feed_entry( @@ -94,8 +96,12 @@ async def test_setup(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> "aio_georss_client.feed.GeoRssFeed.update" ) as mock_feed_update: mock_feed_update.return_value = "OK", [mock_entry_1, mock_entry_2, mock_entry_3] - assert await async_setup_component(hass, gdacs.DOMAIN, CONFIG) - await hass.async_block_till_done() + + hass.config_entries.async_update_entry( + config_entry, data=config_entry.data | CONFIG + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) # Artificially trigger update and collect events. hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done() @@ -202,7 +208,9 @@ async def test_setup(hass: HomeAssistant, entity_registry: er.EntityRegistry) -> assert len(entity_registry.entities) == 1 -async def test_setup_imperial(hass: HomeAssistant) -> None: +async def test_setup_imperial( + hass: HomeAssistant, config_entry: MockConfigEntry +) -> None: """Test the setup of the integration using imperial unit system.""" hass.config.units = US_CUSTOMARY_SYSTEM # Set up some mock feed entries for this test. @@ -224,9 +232,13 @@ async def test_setup_imperial(hass: HomeAssistant) -> None: "aio_georss_client.feed.GeoRssFeed.last_timestamp", create=True ): mock_feed_update.return_value = "OK", [mock_entry_1] - assert await async_setup_component(hass, gdacs.DOMAIN, CONFIG) - await hass.async_block_till_done() - # Artificially trigger update and collect events. + hass.config_entries.async_update_entry( + config_entry, data=config_entry.data | CONFIG + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup( + config_entry.entry_id + ) # Artificially trigger update and collect events. hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done() diff --git a/tests/components/gdacs/test_sensor.py b/tests/components/gdacs/test_sensor.py index 670d3efce51c..9e585de41dd2 100644 --- a/tests/components/gdacs/test_sensor.py +++ b/tests/components/gdacs/test_sensor.py @@ -5,6 +5,7 @@ from freezegun import freeze_time from homeassistant.components import gdacs from homeassistant.components.gdacs import DEFAULT_SCAN_INTERVAL +from homeassistant.components.gdacs.const import CONF_CATEGORIES from homeassistant.components.gdacs.sensor import ( ATTR_CREATED, ATTR_LAST_UPDATE, @@ -16,18 +17,18 @@ from homeassistant.components.gdacs.sensor import ( from homeassistant.const import ( ATTR_ICON, ATTR_UNIT_OF_MEASUREMENT, + CONF_LATITUDE, + CONF_LONGITUDE, CONF_RADIUS, + CONF_SCAN_INTERVAL, EVENT_HOMEASSISTANT_START, ) from homeassistant.core import HomeAssistant -from homeassistant.setup import async_setup_component import homeassistant.util.dt as dt_util from . import _generate_mock_feed_entry -from tests.common import async_fire_time_changed - -CONFIG = {gdacs.DOMAIN: {CONF_RADIUS: 200}} +from tests.common import MockConfigEntry, async_fire_time_changed async def test_setup(hass: HomeAssistant) -> None: @@ -60,7 +61,24 @@ async def test_setup(hass: HomeAssistant) -> None: "aio_georss_client.feed.GeoRssFeed.update" ) as mock_feed_update: mock_feed_update.return_value = "OK", [mock_entry_1, mock_entry_2, mock_entry_3] - assert await async_setup_component(hass, gdacs.DOMAIN, CONFIG) + latitude = 32.87336 + longitude = -117.22743 + radius = 200 + entry_data = { + CONF_RADIUS: radius, + CONF_LATITUDE: latitude, + CONF_LONGITUDE: longitude, + CONF_CATEGORIES: [], + CONF_SCAN_INTERVAL: DEFAULT_SCAN_INTERVAL.seconds, + } + config_entry = MockConfigEntry( + domain=gdacs.DOMAIN, + title=f"{latitude}, {longitude}", + data=entry_data, + unique_id="my_very_unique_id", + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) # Artificially trigger update and collect events. hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done()