Remove hourly weather entity from met.no (#97023)

pull/110898/head
Erik Montnemery 2024-02-16 16:46:37 +01:00 committed by GitHub
parent 1260c5a909
commit a0ead2b861
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 12 deletions

View File

@ -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:

View File

@ -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"]