From 4381f31d6fa182e0b67f377419871d210b550e8c Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 11 Sep 2026 11:30:45 +0200 Subject: [PATCH] Refactor reolink util get_device_uid_and_ch (#181922) --- homeassistant/components/reolink/__init__.py | 6 +++--- homeassistant/components/reolink/services.py | 4 +++- homeassistant/components/reolink/util.py | 12 +++--------- tests/components/reolink/test_util.py | 4 +++- 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/reolink/__init__.py b/homeassistant/components/reolink/__init__.py index 58c5f251af7a..d871798807ce 100644 --- a/homeassistant/components/reolink/__init__.py +++ b/homeassistant/components/reolink/__init__.py @@ -355,7 +355,7 @@ async def async_remove_config_entry_device( # This integration does not create child devices. return False host: ReolinkHost = config_entry.runtime_data.host - (_device_uid, ch, is_chime) = get_device_uid_and_ch(device, host) + (_device_uid, ch, is_chime) = get_device_uid_and_ch(device.identifiers, host) if is_chime: await host.api.get_state(cmd="GetDingDongList") @@ -442,7 +442,7 @@ def migrate_entity_ids( devices = dr.async_entries_for_config_entry(device_reg, config_entry_id) ch_device_ids = {} for device in devices: - (device_uid, ch, is_chime) = get_device_uid_and_ch(device, host) + (device_uid, ch, is_chime) = get_device_uid_and_ch(device.identifiers, host) if host.api.supported(None, "UID") and device_uid[0] != host.unique_id: if ch is None: @@ -465,7 +465,7 @@ def migrate_entity_ids( remove_ids = True # NVR/Hub in identifiers, keep that one, remove others for old_id in device.identifiers: (old_device_uid, _old_ch, _old_is_chime) = get_device_uid_and_ch( - old_id, host + {old_id}, host ) if ( not old_device_uid diff --git a/homeassistant/components/reolink/services.py b/homeassistant/components/reolink/services.py index d7e38d45e5c5..da42ff3aef50 100644 --- a/homeassistant/components/reolink/services.py +++ b/homeassistant/components/reolink/services.py @@ -29,7 +29,9 @@ async def _async_play_chime(service_call: ServiceCall) -> None: service_call.hass, DOMAIN, device_id ) host: ReolinkHost = config_entry.runtime_data.host - (_device_uid, chime_id, is_chime) = get_device_uid_and_ch(device, host) + (_device_uid, chime_id, is_chime) = get_device_uid_and_ch( + device.identifiers, host + ) chime: Chime | None = host.api.chime(chime_id) if not is_chime or chime is None: raise ServiceValidationError( diff --git a/homeassistant/components/reolink/util.py b/homeassistant/components/reolink/util.py index ecef195ebc2a..a71bbff148bc 100644 --- a/homeassistant/components/reolink/util.py +++ b/homeassistant/components/reolink/util.py @@ -23,7 +23,6 @@ from homeassistant import config_entries from homeassistant.components.media_source import Unresolvable from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError -from homeassistant.helpers import device_registry as dr from homeassistant.helpers.storage import Store from homeassistant.helpers.translation import async_get_exception_message @@ -75,18 +74,13 @@ def get_store(hass: HomeAssistant, config_entry_id: str) -> Store[str]: def get_device_uid_and_ch( - device: dr.DeviceEntry | tuple[str, str], host: ReolinkHost + identifiers: set[tuple[str, str]], host: ReolinkHost ) -> tuple[list[str], int | None, bool]: - """Get the channel and the split device_uid from a reolink DeviceEntry.""" + """Get the channel and the split device_uid from reolink device identifiers.""" device_uid = [] is_chime = False - if isinstance(device, dr.DeviceEntry): - dev_ids = device.identifiers - else: - dev_ids = {device} - - for dev_id in dev_ids: + for dev_id in identifiers: if dev_id[0] == DOMAIN: device_uid = dev_id[1].split("_") if device_uid[0] == host.unique_id: diff --git a/tests/components/reolink/test_util.py b/tests/components/reolink/test_util.py index 1ebeaf902c84..76d6af2948fa 100644 --- a/tests/components/reolink/test_util.py +++ b/tests/components/reolink/test_util.py @@ -157,6 +157,8 @@ async def test_get_device_uid_and_ch( assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - result = get_device_uid_and_ch(dev_entry, config_entry.runtime_data.host) + result = get_device_uid_and_ch( + dev_entry.identifiers, config_entry.runtime_data.host + ) # always get the uid and channel form the DEV_ID_NVR since is_nvr = True assert result == ([TEST_UID, TEST_UID_CAM], 0, False)