diff --git a/homeassistant/components/dwd_weather_warnings/__init__.py b/homeassistant/components/dwd_weather_warnings/__init__.py index f71b81d862b..7a56299a35b 100644 --- a/homeassistant/components/dwd_weather_warnings/__init__.py +++ b/homeassistant/components/dwd_weather_warnings/__init__.py @@ -3,8 +3,9 @@ from __future__ import annotations from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr -from .const import PLATFORMS +from .const import DOMAIN, PLATFORMS from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator @@ -12,6 +13,9 @@ async def async_setup_entry( hass: HomeAssistant, entry: DwdWeatherWarningsConfigEntry ) -> bool: """Set up a config entry.""" + device_registry = dr.async_get(hass) + if device_registry.async_get_device(identifiers={(DOMAIN, entry.entry_id)}): + device_registry.async_clear_config_entry(entry.entry_id) coordinator = DwdWeatherWarningsCoordinator(hass) await coordinator.async_config_entry_first_refresh() diff --git a/homeassistant/components/dwd_weather_warnings/sensor.py b/homeassistant/components/dwd_weather_warnings/sensor.py index 4f1b64a5b44..c6aa5727b74 100644 --- a/homeassistant/components/dwd_weather_warnings/sensor.py +++ b/homeassistant/components/dwd_weather_warnings/sensor.py @@ -36,7 +36,6 @@ from .const import ( ATTR_REGION_NAME, ATTR_WARNING_COUNT, CURRENT_WARNING_SENSOR, - DEFAULT_NAME, DOMAIN, ) from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator @@ -61,12 +60,12 @@ async def async_setup_entry( """Set up entities from config entry.""" coordinator = entry.runtime_data + unique_id = entry.unique_id + assert unique_id + async_add_entities( - [ - DwdWeatherWarningsSensor(coordinator, entry, description) - for description in SENSOR_TYPES - ], - True, + DwdWeatherWarningsSensor(coordinator, description, unique_id) + for description in SENSOR_TYPES ) @@ -81,18 +80,18 @@ class DwdWeatherWarningsSensor( def __init__( self, coordinator: DwdWeatherWarningsCoordinator, - entry: DwdWeatherWarningsConfigEntry, description: SensorEntityDescription, + unique_id: str, ) -> None: """Initialize a DWD-Weather-Warnings sensor.""" super().__init__(coordinator) self.entity_description = description - self._attr_unique_id = f"{entry.unique_id}-{description.key}" + self._attr_unique_id = f"{unique_id}-{description.key}" self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, entry.entry_id)}, - name=f"{DEFAULT_NAME} {entry.title}", + identifiers={(DOMAIN, unique_id)}, + name=coordinator.api.warncell_name, entry_type=DeviceEntryType.SERVICE, ) diff --git a/tests/components/dwd_weather_warnings/test_init.py b/tests/components/dwd_weather_warnings/test_init.py index e5b82d0c453..54f57ead77c 100644 --- a/tests/components/dwd_weather_warnings/test_init.py +++ b/tests/components/dwd_weather_warnings/test_init.py @@ -12,7 +12,8 @@ from homeassistant.components.dwd_weather_warnings.coordinator import ( from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_HOME from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers.device_registry import DeviceEntryType from . import init_integration @@ -36,6 +37,41 @@ async def test_load_unload_entry( assert entry.state is ConfigEntryState.NOT_LOADED +async def test_removing_old_device( + hass: HomeAssistant, + mock_identifier_entry: MockConfigEntry, + mock_dwdwfsapi: MagicMock, + device_registry: dr.DeviceRegistry, +) -> None: + """Test removing old device when reloading the integration.""" + + mock_identifier_entry.add_to_hass(hass) + + device_registry.async_get_or_create( + identifiers={(DOMAIN, mock_identifier_entry.entry_id)}, + config_entry_id=mock_identifier_entry.entry_id, + entry_type=DeviceEntryType.SERVICE, + name="test", + ) + + assert ( + device_registry.async_get_device( + identifiers={(DOMAIN, mock_identifier_entry.entry_id)} + ) + is not None + ) + + await hass.config_entries.async_setup(mock_identifier_entry.entry_id) + await hass.async_block_till_done() + + assert ( + device_registry.async_get_device( + identifiers={(DOMAIN, mock_identifier_entry.entry_id)} + ) + is None + ) + + async def test_load_invalid_registry_entry( hass: HomeAssistant, mock_tracker_entry: MockConfigEntry ) -> None: