diff --git a/homeassistant/components/hikvision/binary_sensor.py b/homeassistant/components/hikvision/binary_sensor.py index 2d0404f0def3..3342cc91744c 100644 --- a/homeassistant/components/hikvision/binary_sensor.py +++ b/homeassistant/components/hikvision/binary_sensor.py @@ -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 diff --git a/homeassistant/components/hikvision/camera.py b/homeassistant/components/hikvision/camera.py index fc979f4f93ad..b44a128f8771 100644 --- a/homeassistant/components/hikvision/camera.py +++ b/homeassistant/components/hikvision/camera.py @@ -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) diff --git a/homeassistant/components/hikvision/entity.py b/homeassistant/components/hikvision/entity.py index 4cd1ed6315f7..0723f685439c 100644 --- a/homeassistant/components/hikvision/entity.py +++ b/homeassistant/components/hikvision/entity.py @@ -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, diff --git a/tests/components/hikvision/test_binary_sensor.py b/tests/components/hikvision/test_binary_sensor.py index 45eb0e76e67c..d0ba96fe0354 100644 --- a/tests/components/hikvision/test_binary_sensor.py +++ b/tests/components/hikvision/test_binary_sensor.py @@ -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")