Adapt hikvision to set via_device_id in DeviceInfo (#177719)

This commit is contained in:
Erik Montnemery
2026-08-03 15:59:28 +02:00
committed by GitHub
parent 5f1550f561
commit 5f0e4a66c1
4 changed files with 22 additions and 9 deletions
@@ -267,6 +267,7 @@ async def async_setup_entry(
seen_channels.add(channel)
entities.append(
HikvisionBinarySensor(
hass=hass,
entry=entry,
description=BINARY_SENSOR_DESCRIPTIONS[sensor_type],
sensor_type=sensor_type,
@@ -284,13 +285,14 @@ class HikvisionBinarySensor(HikvisionEntity, BinarySensorEntity):
def __init__(
self,
hass: HomeAssistant,
entry: HikvisionConfigEntry,
description: BinarySensorEntityDescription,
sensor_type: str,
channel: int,
) -> None:
"""Initialize the binary sensor."""
super().__init__(entry, channel)
super().__init__(hass, entry, channel)
self.entity_description = description
self._sensor_type = sensor_type
+4 -2
View File
@@ -27,7 +27,7 @@ async def async_setup_entry(
if data.channels:
# NVR with video channels from get_video_channels()
async_add_entities(
HikvisionCamera(entry, channel)
HikvisionCamera(hass, entry, channel)
for channel in data.channels.values()
if channel.enabled
)
@@ -36,6 +36,7 @@ async def async_setup_entry(
async_add_entities(
[
HikvisionCamera(
hass,
entry,
VideoChannel(id=1, name=data.device_name, enabled=True),
)
@@ -51,11 +52,12 @@ class HikvisionCamera(HikvisionEntity, Camera):
def __init__(
self,
hass: HomeAssistant,
entry: HikvisionConfigEntry,
channel: VideoChannel,
) -> None:
"""Initialize the camera."""
super().__init__(entry, channel.id)
super().__init__(hass, entry, channel.id)
self._video_channel = channel
# Build unique ID (unique per platform per integration)
+7 -3
View File
@@ -1,5 +1,7 @@
"""Base entity for Hikvision integration."""
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
@@ -14,6 +16,7 @@ class HikvisionEntity(Entity):
def __init__(
self,
hass: HomeAssistant,
entry: HikvisionConfigEntry,
channel: int,
) -> None:
@@ -23,12 +26,13 @@ class HikvisionEntity(Entity):
self._camera = self._data.camera
self._channel = channel
# Device info for device registry
if self._data.device_type == "NVR":
# NVR channels get their own device linked to the NVR via via_device
# NVR channels get their own device linked to the NVR via via_device_id
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, f"{self._data.device_id}_{channel}")},
via_device=(DOMAIN, self._data.device_id),
via_device_id=dr.async_get_device_id_by_identifier(
hass, (DOMAIN, self._data.device_id), config_entry_id=entry.entry_id
),
translation_key="nvr_channel",
translation_placeholders={
"device_name": self._data.device_name,
@@ -147,18 +147,23 @@ async def test_binary_sensor_nvr_device(
await setup_integration(hass, mock_config_entry)
# Verify NVR channel devices are created with via_device linking
# Verify NVR channel devices are linked to the NVR device via via_device_id
nvr_device = device_registry.async_get_device_by_identifier(
(DOMAIN, TEST_DEVICE_ID), mock_config_entry.entry_id
)
assert nvr_device is not None
channel_1_device = device_registry.async_get_device(
identifiers={(DOMAIN, f"{TEST_DEVICE_ID}_1")}
)
assert channel_1_device is not None
assert channel_1_device.via_device_id is not None
assert channel_1_device.via_device_id == nvr_device.id
channel_2_device = device_registry.async_get_device(
identifiers={(DOMAIN, f"{TEST_DEVICE_ID}_2")}
)
assert channel_2_device is not None
assert channel_2_device.via_device_id is not None
assert channel_2_device.via_device_id == nvr_device.id
# Verify sensors are created (entity IDs depend on translation loading)
states = hass.states.async_entity_ids("binary_sensor")