Create DWD device with unique_id instead of entry_id (#116498)

Co-authored-by: Matthias Alphart <farmio@alphart.net>
This commit is contained in:
Joost Lekkerkerker 2024-06-09 16:36:36 +02:00 committed by GitHub
parent 04a5a1d18b
commit b4c9b3f109
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 12 deletions

View File

@ -3,8 +3,9 @@
from __future__ import annotations from __future__ import annotations
from homeassistant.core import HomeAssistant 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 from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator
@ -12,6 +13,9 @@ async def async_setup_entry(
hass: HomeAssistant, entry: DwdWeatherWarningsConfigEntry hass: HomeAssistant, entry: DwdWeatherWarningsConfigEntry
) -> bool: ) -> bool:
"""Set up a config entry.""" """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) coordinator = DwdWeatherWarningsCoordinator(hass)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()

View File

@ -36,7 +36,6 @@ from .const import (
ATTR_REGION_NAME, ATTR_REGION_NAME,
ATTR_WARNING_COUNT, ATTR_WARNING_COUNT,
CURRENT_WARNING_SENSOR, CURRENT_WARNING_SENSOR,
DEFAULT_NAME,
DOMAIN, DOMAIN,
) )
from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator from .coordinator import DwdWeatherWarningsConfigEntry, DwdWeatherWarningsCoordinator
@ -61,12 +60,12 @@ async def async_setup_entry(
"""Set up entities from config entry.""" """Set up entities from config entry."""
coordinator = entry.runtime_data coordinator = entry.runtime_data
unique_id = entry.unique_id
assert unique_id
async_add_entities( async_add_entities(
[ DwdWeatherWarningsSensor(coordinator, description, unique_id)
DwdWeatherWarningsSensor(coordinator, entry, description) for description in SENSOR_TYPES
for description in SENSOR_TYPES
],
True,
) )
@ -81,18 +80,18 @@ class DwdWeatherWarningsSensor(
def __init__( def __init__(
self, self,
coordinator: DwdWeatherWarningsCoordinator, coordinator: DwdWeatherWarningsCoordinator,
entry: DwdWeatherWarningsConfigEntry,
description: SensorEntityDescription, description: SensorEntityDescription,
unique_id: str,
) -> None: ) -> None:
"""Initialize a DWD-Weather-Warnings sensor.""" """Initialize a DWD-Weather-Warnings sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self.entity_description = description 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( self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, entry.entry_id)}, identifiers={(DOMAIN, unique_id)},
name=f"{DEFAULT_NAME} {entry.title}", name=coordinator.api.warncell_name,
entry_type=DeviceEntryType.SERVICE, entry_type=DeviceEntryType.SERVICE,
) )

View File

@ -12,7 +12,8 @@ from homeassistant.components.dwd_weather_warnings.coordinator import (
from homeassistant.config_entries import ConfigEntryState from homeassistant.config_entries import ConfigEntryState
from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_HOME from homeassistant.const import ATTR_LATITUDE, ATTR_LONGITUDE, STATE_HOME
from homeassistant.core import HomeAssistant 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 from . import init_integration
@ -36,6 +37,41 @@ async def test_load_unload_entry(
assert entry.state is ConfigEntryState.NOT_LOADED 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( async def test_load_invalid_registry_entry(
hass: HomeAssistant, mock_tracker_entry: MockConfigEntry hass: HomeAssistant, mock_tracker_entry: MockConfigEntry
) -> None: ) -> None: