From a0ead2b861edb25c8c6e8b3ee661416cf8f4e985 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 16 Feb 2024 16:46:37 +0100 Subject: [PATCH] Remove hourly weather entity from met.no (#97023) --- homeassistant/components/met/weather.py | 16 ++++++-------- tests/components/met/test_weather.py | 28 +++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/met/weather.py b/homeassistant/components/met/weather.py index 11b044311d2..e4a63e326a6 100644 --- a/homeassistant/components/met/weather.py +++ b/homeassistant/components/met/weather.py @@ -68,16 +68,15 @@ async def async_setup_entry( if TYPE_CHECKING: assert isinstance(name, str) - entities = [MetWeather(coordinator, config_entry, False, name, is_metric)] + entities = [MetWeather(coordinator, config_entry, name, is_metric)] - # Add hourly entity to legacy config entries - if entity_registry.async_get_entity_id( + # Remove hourly entity from legacy config entries + if hourly_entity_id := entity_registry.async_get_entity_id( WEATHER_DOMAIN, DOMAIN, _calculate_unique_id(config_entry.data, True), ): - name = f"{name} hourly" - entities.append(MetWeather(coordinator, config_entry, True, name, is_metric)) + entity_registry.async_remove(hourly_entity_id) async_add_entities(entities) @@ -121,17 +120,14 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]): self, coordinator: MetDataUpdateCoordinator, config_entry: ConfigEntry, - hourly: bool, name: str, is_metric: bool, ) -> None: """Initialise the platform with a data instance and site.""" super().__init__(coordinator) - self._attr_unique_id = _calculate_unique_id(config_entry.data, hourly) + self._attr_unique_id = _calculate_unique_id(config_entry.data, False) self._config = config_entry.data self._is_metric = is_metric - self._hourly = hourly - self._attr_entity_registry_enabled_default = not hourly self._attr_device_info = DeviceInfo( name="Forecast", entry_type=DeviceEntryType.SERVICE, @@ -237,7 +233,7 @@ class MetWeather(SingleCoordinatorWeatherEntity[MetDataUpdateCoordinator]): @property def forecast(self) -> list[Forecast] | None: """Return the forecast array.""" - return self._forecast(self._hourly) + return self._forecast(False) @callback def _async_forecast_daily(self) -> list[Forecast] | None: diff --git a/tests/components/met/test_weather.py b/tests/components/met/test_weather.py index 432c288383a..d1714e7b69e 100644 --- a/tests/components/met/test_weather.py +++ b/tests/components/met/test_weather.py @@ -29,10 +29,10 @@ async def test_legacy_config_entry( ) await hass.config_entries.flow.async_init("met", context={"source": "onboarding"}) await hass.async_block_till_done() - assert len(hass.states.async_entity_ids("weather")) == 2 + assert len(hass.states.async_entity_ids("weather")) == 1 entry = hass.config_entries.async_entries()[0] - assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 2 + assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 1 async def test_tracking_home(hass: HomeAssistant, mock_weather) -> None: @@ -81,3 +81,27 @@ async def test_not_tracking_home(hass: HomeAssistant, mock_weather) -> None: await hass.config_entries.async_remove(entry.entry_id) await hass.async_block_till_done() assert len(hass.states.async_entity_ids("weather")) == 0 + + +async def test_remove_hourly_entity(hass: HomeAssistant, mock_weather) -> None: + """Test removing the hourly entity.""" + + # Pre-create registry entry for disabled by default hourly weather + registry = er.async_get(hass) + registry.async_get_or_create( + WEATHER_DOMAIN, + DOMAIN, + "10-20-hourly", + suggested_object_id="forecast_somewhere_hourly", + disabled_by=None, + ) + assert list(registry.entities.keys()) == ["weather.forecast_somewhere_hourly"] + + await hass.config_entries.flow.async_init( + "met", + context={"source": config_entries.SOURCE_USER}, + data={"name": "Somewhere", "latitude": 10, "longitude": 20, "elevation": 0}, + ) + await hass.async_block_till_done() + assert hass.states.async_entity_ids("weather") == ["weather.forecast_somewhere"] + assert list(registry.entities.keys()) == ["weather.forecast_somewhere"]