diff --git a/homeassistant/components/notion/entity.py b/homeassistant/components/notion/entity.py index d8179aca51b8..0d1a9fdeee16 100644 --- a/homeassistant/components/notion/entity.py +++ b/homeassistant/components/notion/entity.py @@ -50,7 +50,13 @@ class NotionEntity(CoordinatorEntity[NotionDataUpdateCoordinator]): ) if bridge := self._async_get_bridge(bridge_id): - self._attr_device_info["via_device"] = (DOMAIN, bridge.hardware_id) + self._attr_device_info["via_device_id"] = ( + dr.async_get_device_id_by_identifier( + self.coordinator.hass, + (DOMAIN, bridge.hardware_id), + config_entry_id=self.coordinator.config_entry.entry_id, + ) + ) self._attr_extra_state_attributes = {} self._attr_unique_id = listener_id diff --git a/tests/components/notion/test_init.py b/tests/components/notion/test_init.py new file mode 100644 index 000000000000..9706e877d21f --- /dev/null +++ b/tests/components/notion/test_init.py @@ -0,0 +1,63 @@ +"""Test Notion setup.""" + +from copy import deepcopy +from typing import Any + +from aionotion.listener.models import ListenerKind +import pytest + +from homeassistant.components.notion.const import DOMAIN +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from tests.common import MockConfigEntry + +# The id of the bridge in the bridge fixture; the sensor must reference it to link. +BRIDGE_ID = 12345 +BRIDGE_HARDWARE_ID = "0x0000000000000012" +SENSOR_HARDWARE_ID = "0x0000000000000034" + + +@pytest.fixture(name="data_bridge") +def data_bridge_fixture(data_bridge: dict[str, Any]) -> dict[str, Any]: + """Give the bridge a hardware id distinct from the sensor.""" + data = deepcopy(data_bridge) + data["base_stations"][0]["hardware_id"] = BRIDGE_HARDWARE_ID + return data + + +@pytest.fixture(name="data_sensor") +def data_sensor_fixture(data_sensor: dict[str, Any]) -> dict[str, Any]: + """Link the sensor to the bridge and give it a distinct hardware id.""" + data = deepcopy(data_sensor) + data["sensors"][0]["hardware_id"] = SENSOR_HARDWARE_ID + data["sensors"][0]["bridge"]["id"] = BRIDGE_ID + data["sensors"][0]["bridge"]["hardware_id"] = BRIDGE_HARDWARE_ID + return data + + +@pytest.fixture(name="data_listener") +def data_listener_fixture(data_listener: dict[str, Any]) -> dict[str, Any]: + """Use a listener kind that maps to an entity so a sensor device is created.""" + data = deepcopy(data_listener) + data["listeners"][0]["definition_id"] = ListenerKind.HINGED_WINDOW.value + return data + + +@pytest.mark.usefixtures("setup_config_entry") +async def test_device_via_device_links( + hass: HomeAssistant, + config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test that a sensor device links to its bridge via via_device_id.""" + bridge_device = device_registry.async_get_device_by_identifier( + (DOMAIN, BRIDGE_HARDWARE_ID), config_entry.entry_id + ) + assert bridge_device is not None + + sensor_device = device_registry.async_get_device_by_identifier( + (DOMAIN, SENSOR_HARDWARE_ID), config_entry.entry_id + ) + assert sensor_device is not None + assert sensor_device.via_device_id == bridge_device.id