Remove old entity unique id migration from sabnzbd (#131064)

This commit is contained in:
Jan-Philipp Benecke 2024-11-20 19:55:54 +01:00 committed by GitHub
parent b188f8284c
commit 309dd5ed1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 2 additions and 144 deletions

View File

@ -12,8 +12,7 @@ from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.const import Platform from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError from homeassistant.exceptions import ConfigEntryNotReady, HomeAssistantError
from homeassistant.helpers import config_validation as cv, device_registry as dr from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries
import homeassistant.helpers.issue_registry as ir import homeassistant.helpers.issue_registry as ir
from .const import ( from .const import (
@ -62,52 +61,6 @@ def async_get_entry_id_for_service_call(hass: HomeAssistant, call: ServiceCall)
raise ValueError(f"No api for API key: {call_data_api_key}") raise ValueError(f"No api for API key: {call_data_api_key}")
def update_device_identifiers(hass: HomeAssistant, entry: ConfigEntry):
"""Update device identifiers to new identifiers."""
device_registry = dr.async_get(hass)
device_entry = device_registry.async_get_device(identifiers={(DOMAIN, DOMAIN)})
if device_entry and entry.entry_id in device_entry.config_entries:
new_identifiers = {(DOMAIN, entry.entry_id)}
_LOGGER.debug(
"Updating device id <%s> with new identifiers <%s>",
device_entry.id,
new_identifiers,
)
device_registry.async_update_device(
device_entry.id, new_identifiers=new_identifiers
)
async def migrate_unique_id(hass: HomeAssistant, entry: ConfigEntry):
"""Migrate entities to new unique ids (with entry_id)."""
@callback
def async_migrate_callback(entity_entry: RegistryEntry) -> dict | None:
"""Define a callback to migrate appropriate SabnzbdSensor entities to new unique IDs.
Old: description.key
New: {entry_id}_description.key
"""
entry_id = entity_entry.config_entry_id
if entry_id is None:
return None
if entity_entry.unique_id.startswith(entry_id):
return None
new_unique_id = f"{entry_id}_{entity_entry.unique_id}"
_LOGGER.debug(
"Migrating entity %s from old unique ID '%s' to new unique ID '%s'",
entity_entry.entity_id,
entity_entry.unique_id,
new_unique_id,
)
return {"new_unique_id": new_unique_id}
await async_migrate_entries(hass, entry.entry_id, async_migrate_callback)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up the SabNzbd Component.""" """Set up the SabNzbd Component."""
@ -115,9 +68,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if not sab_api: if not sab_api:
raise ConfigEntryNotReady raise ConfigEntryNotReady
await migrate_unique_id(hass, entry)
update_device_identifiers(hass, entry)
coordinator = SabnzbdUpdateCoordinator(hass, entry, sab_api) coordinator = SabnzbdUpdateCoordinator(hass, entry, sab_api)
await coordinator.async_config_entry_first_refresh() await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator

View File

@ -113,20 +113,6 @@ SENSOR_TYPES: tuple[SabnzbdSensorEntityDescription, ...] = (
), ),
) )
OLD_SENSOR_KEYS = [
"current_status",
"speed",
"queue_size",
"queue_remaining",
"disk_size",
"disk_free",
"queue_count",
"day_size",
"week_size",
"month_size",
"total_size",
]
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,

View File

@ -1,93 +1,15 @@
"""Tests for the SABnzbd Integration.""" """Tests for the SABnzbd Integration."""
from unittest.mock import patch
import pytest import pytest
from homeassistant.components.sabnzbd.const import ( from homeassistant.components.sabnzbd.const import (
ATTR_API_KEY, ATTR_API_KEY,
DEFAULT_NAME,
DOMAIN, DOMAIN,
SERVICE_PAUSE, SERVICE_PAUSE,
SERVICE_RESUME, SERVICE_RESUME,
) )
from homeassistant.components.sabnzbd.sensor import OLD_SENSOR_KEYS
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.const import CONF_API_KEY, CONF_NAME, CONF_URL
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import ( from homeassistant.helpers import issue_registry as ir
device_registry as dr,
entity_registry as er,
issue_registry as ir,
)
from tests.common import MockConfigEntry
MOCK_ENTRY_ID = "mock_entry_id"
MOCK_UNIQUE_ID = "someuniqueid"
MOCK_DEVICE_ID = "somedeviceid"
MOCK_DATA_VERSION_1 = {
CONF_API_KEY: "api_key",
CONF_URL: "http://127.0.0.1:8080",
CONF_NAME: "name",
}
MOCK_ENTRY_VERSION_1 = MockConfigEntry(
domain=DOMAIN, data=MOCK_DATA_VERSION_1, entry_id=MOCK_ENTRY_ID, version=1
)
async def test_unique_id_migrate(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test that config flow entry is migrated correctly."""
# Start with the config entry at Version 1.
mock_entry = MOCK_ENTRY_VERSION_1
mock_entry.add_to_hass(hass)
mock_d_entry = device_registry.async_get_or_create(
config_entry_id=mock_entry.entry_id,
identifiers={(DOMAIN, DOMAIN)},
name=DEFAULT_NAME,
entry_type=dr.DeviceEntryType.SERVICE,
)
entity_id_sensor_key = []
for sensor_key in OLD_SENSOR_KEYS:
mock_entity_id = f"{SENSOR_DOMAIN}.{DOMAIN}_{sensor_key}"
entity_registry.async_get_or_create(
SENSOR_DOMAIN,
DOMAIN,
unique_id=sensor_key,
config_entry=mock_entry,
device_id=mock_d_entry.id,
)
entity = entity_registry.async_get(mock_entity_id)
assert entity.entity_id == mock_entity_id
assert entity.unique_id == sensor_key
entity_id_sensor_key.append((mock_entity_id, sensor_key))
with patch(
"homeassistant.components.sabnzbd.sab.SabnzbdApi.check_available",
return_value=True,
):
await hass.config_entries.async_setup(mock_entry.entry_id)
await hass.async_block_till_done()
for mock_entity_id, sensor_key in entity_id_sensor_key:
entity = entity_registry.async_get(mock_entity_id)
assert entity.unique_id == f"{MOCK_ENTRY_ID}_{sensor_key}"
assert device_registry.async_get(mock_d_entry.id).identifiers == {
(DOMAIN, MOCK_ENTRY_ID)
}
@pytest.mark.parametrize( @pytest.mark.parametrize(