mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Create DWD device with unique_id instead of entry_id (#116498)
Co-authored-by: Matthias Alphart <farmio@alphart.net>
This commit is contained in:
parent
04a5a1d18b
commit
b4c9b3f109
@ -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()
|
||||
|
||||
|
@ -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)
|
||||
DwdWeatherWarningsSensor(coordinator, description, unique_id)
|
||||
for description in SENSOR_TYPES
|
||||
],
|
||||
True,
|
||||
)
|
||||
|
||||
|
||||
@ -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,
|
||||
)
|
||||
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user