Refactor reolink util get_device_uid_and_ch (#181922)

This commit is contained in:
Erik Montnemery
2026-09-11 11:30:45 +02:00
committed by GitHub
parent 5f3caf3e24
commit 4381f31d6f
4 changed files with 12 additions and 14 deletions
+3 -3
View File
@@ -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
+3 -1
View File
@@ -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(
+3 -9
View File
@@ -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:
+3 -1
View File
@@ -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)