diff --git a/homeassistant/components/withings/entity.py b/homeassistant/components/withings/entity.py index bc7ce5c21f35..79b2e2b0e359 100644 --- a/homeassistant/components/withings/entity.py +++ b/homeassistant/components/withings/entity.py @@ -4,6 +4,7 @@ from typing import Any, override from aiowithings import Device +from homeassistant.helpers import device_registry as dr from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import CoordinatorEntity @@ -50,7 +51,11 @@ class WithingsDeviceEntity(WithingsEntity[WithingsDeviceDataUpdateCoordinator]): manufacturer="Withings", name=self.device.raw_model, model=self.device.raw_model, - via_device=(DOMAIN, str(coordinator.config_entry.unique_id)), + via_device_id=dr.async_get_device_id_by_identifier( + coordinator.hass, + (DOMAIN, str(coordinator.config_entry.unique_id)), + config_entry_id=coordinator.config_entry.entry_id, + ), ) @property diff --git a/tests/components/withings/test_init.py b/tests/components/withings/test_init.py index f287d2ae0af0..63c50983ae9b 100644 --- a/tests/components/withings/test_init.py +++ b/tests/components/withings/test_init.py @@ -715,6 +715,29 @@ async def test_devices( assert device == snapshot(name=device_id) +@pytest.mark.usefixtures("withings") +async def test_device_via_device_id( + hass: HomeAssistant, + webhook_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test the physical device is linked to the account device via via_device_id.""" + await setup_integration(hass, webhook_config_entry) + await hass.async_block_till_done() + + account_device = device_registry.async_get_device_by_identifier( + (DOMAIN, str(webhook_config_entry.unique_id)), webhook_config_entry.entry_id + ) + assert account_device is not None + + device = device_registry.async_get_device_by_identifier( + (DOMAIN, "f998be4b9ccc9e136fd8cd8e8e344c31ec3b271d"), + webhook_config_entry.entry_id, + ) + assert device is not None + assert device.via_device_id == account_device.id + + async def test_oauth_implementation_not_available( hass: HomeAssistant, webhook_config_entry: MockConfigEntry,