diff --git a/homeassistant/components/overkiz/__init__.py b/homeassistant/components/overkiz/__init__.py index 3cb4d01ce1b3..87bd4a333caf 100644 --- a/homeassistant/components/overkiz/__init__.py +++ b/homeassistant/components/overkiz/__init__.py @@ -193,6 +193,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: OverkizDataConfigEntry) device_registry = dr.async_get(hass) + registered_gateway_ids = {gateway.id for gateway in setup.gateways} + for gateway in setup.gateways: device_registry.async_get_or_create( config_entry_id=entry.entry_id, @@ -208,6 +210,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: OverkizDataConfigEntry) configuration_url=client.server_config.configuration_url, ) + # Some devices reference a gateway that is not part of setup.gateways + # (e.g. a secondary box hosting a device). Register a device for each such + # gateway so the via_device link of its child devices resolves. + for gateway_id in { + device.identifier.gateway_id for device in coordinator.data.values() + } - registered_gateway_ids: + device_registry.async_get_or_create( + config_entry_id=entry.entry_id, + identifiers={(DOMAIN, gateway_id)}, + manufacturer=client.server_config.manufacturer, + name=gateway_id, + configuration_url=client.server_config.configuration_url, + ) + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) return True diff --git a/homeassistant/components/overkiz/entity.py b/homeassistant/components/overkiz/entity.py index 97560335b283..61d1d84a3b4b 100644 --- a/homeassistant/components/overkiz/entity.py +++ b/homeassistant/components/overkiz/entity.py @@ -5,6 +5,7 @@ from typing import cast, override from pyoverkiz.enums import APIType, OverkizAttribute, OverkizCommandParam, OverkizState from pyoverkiz.models import Device +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.entity import EntityDescription from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -111,7 +112,11 @@ class OverkizEntity(CoordinatorEntity[OverkizDataUpdateCoordinator]): model_id=self.device.widget, hw_version=self.device.controllable_name, suggested_area=suggested_area, - via_device=(DOMAIN, self.device.identifier.gateway_id), + via_device_id=dr.async_get_device_id_by_identifier( + self.coordinator.hass, + (DOMAIN, self.device.identifier.gateway_id), + config_entry_id=self.coordinator.config_entry.entry_id, + ), configuration_url=self.coordinator.client.server_config.configuration_url, ) diff --git a/tests/components/overkiz/helpers.py b/tests/components/overkiz/helpers.py index 02c9f2844bf3..87214a79dd7a 100644 --- a/tests/components/overkiz/helpers.py +++ b/tests/components/overkiz/helpers.py @@ -7,6 +7,7 @@ from freezegun.api import FrozenDateTimeFactory from pyoverkiz.enums import DataType, EventName, ExecutionState from pyoverkiz.models import ( DeviceAvailableEvent, + DeviceCreatedEvent, DeviceRemovedEvent, DeviceStateChangedEvent, DeviceUnavailableEvent, @@ -97,6 +98,11 @@ def device_removed_event(device_url: str) -> DeviceRemovedEvent: return DeviceRemovedEvent(name=EventName.DEVICE_REMOVED, device_url=device_url) +def device_created_event(device_url: str) -> DeviceCreatedEvent: + """Build a DEVICE_CREATED event for the given device.""" + return DeviceCreatedEvent(name=EventName.DEVICE_CREATED, device_url=device_url) + + def execution_state_changed_event( exec_id: str, new_state: ExecutionState, old_state: ExecutionState ) -> ExecutionStateChangedEvent: diff --git a/tests/components/overkiz/test_coordinator.py b/tests/components/overkiz/test_coordinator.py index 273a0ca5f531..7ec38ac0ecc9 100644 --- a/tests/components/overkiz/test_coordinator.py +++ b/tests/components/overkiz/test_coordinator.py @@ -1,6 +1,6 @@ """Tests for the Overkiz data update coordinator.""" -from unittest.mock import Mock +from unittest.mock import Mock, patch from aiohttp import ClientConnectorError from freezegun.api import FrozenDateTimeFactory @@ -13,13 +13,14 @@ from pyoverkiz.exceptions import ( ) import pytest -from homeassistant.components.overkiz.const import UPDATE_INTERVAL +from homeassistant.components.overkiz.const import DOMAIN, UPDATE_INTERVAL +from homeassistant.config_entries import ConfigEntryState from homeassistant.const import STATE_UNAVAILABLE from homeassistant.core import HomeAssistant from homeassistant.helpers import device_registry as dr, entity_registry as er from .conftest import FixtureDevice, MockOverkizClient, SetupOverkizIntegration -from .helpers import async_deliver_events, device_removed_event +from .helpers import async_deliver_events, device_created_event, device_removed_event from tests.common import MockConfigEntry, async_fire_time_changed @@ -29,6 +30,14 @@ TEMPERATURE_SENSOR = FixtureDevice( "sensor.maple_residence_garden_radiator_bathroom_temperature_sensor_temperature", ) +# A TaHoma v2 setup whose gateways list only holds the main box, while a +# swinging gate device is hosted by a secondary box absent from setup.gateways. +TAHOMA_V2_FIXTURE = "setup/cloud_somfy_tahoma_v2_europe.json" +MAIN_GATEWAY_ID = "1234-1234-6233" +SECONDARY_GATEWAY_ID = "1234-1234-8983" +MAIN_GATEWAY_CHILD_URL = "io://1234-1234-6233/12184029" +SECONDARY_GATEWAY_CHILD_URL = "io://1234-1234-8983/1959462" + @pytest.mark.parametrize( "exception", @@ -134,3 +143,67 @@ async def test_device_removed_keeps_device_owned_by_other_entry( device = device_registry.async_get(device_id) assert device is not None assert device.config_entry_id == other_entry.entry_id + + +async def test_child_devices_link_to_their_gateway( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_client: MockOverkizClient, + freezer: FrozenDateTimeFactory, + device_registry: dr.DeviceRegistry, +) -> None: + """Every child device links to its gateway, even one absent from setup.gateways. + + The secondary box that hosts the swinging gate is not returned in + setup.gateways, but the via_device link of its child must still resolve. + A DEVICE_CREATED event reloads the entry, which must keep the link intact. + """ + mock_config_entry.add_to_hass(hass) + mock_client.set_setup_fixture(TAHOMA_V2_FIXTURE) + + with patch( + "homeassistant.components.overkiz.create_cloud_client", + return_value=mock_client, + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + main_gateway = device_registry.async_get_device_by_identifier( + (DOMAIN, MAIN_GATEWAY_ID), mock_config_entry.entry_id + ) + secondary_gateway = device_registry.async_get_device_by_identifier( + (DOMAIN, SECONDARY_GATEWAY_ID), mock_config_entry.entry_id + ) + assert main_gateway is not None + assert secondary_gateway is not None + + main_child = device_registry.async_get_device_by_identifier( + (DOMAIN, MAIN_GATEWAY_CHILD_URL), mock_config_entry.entry_id + ) + secondary_child = device_registry.async_get_device_by_identifier( + (DOMAIN, SECONDARY_GATEWAY_CHILD_URL), mock_config_entry.entry_id + ) + assert main_child is not None + assert secondary_child is not None + assert main_child.via_device_id == main_gateway.id + assert secondary_child.via_device_id == secondary_gateway.id + + # A DEVICE_CREATED event reloads the entry; the links must survive. + await async_deliver_events( + hass, + freezer, + mock_client, + [device_created_event(SECONDARY_GATEWAY_CHILD_URL)], + ) + + assert mock_config_entry.state is ConfigEntryState.LOADED + + secondary_gateway = device_registry.async_get_device_by_identifier( + (DOMAIN, SECONDARY_GATEWAY_ID), mock_config_entry.entry_id + ) + secondary_child = device_registry.async_get_device_by_identifier( + (DOMAIN, SECONDARY_GATEWAY_CHILD_URL), mock_config_entry.entry_id + ) + assert secondary_gateway is not None + assert secondary_child is not None + assert secondary_child.via_device_id == secondary_gateway.id