From 792060aa1c711aba42aefd6af2bac3ea31c4c950 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 23 Jun 2025 17:15:15 +0200 Subject: [PATCH] cleanup --- homeassistant/components/esphome/manager.py | 6 +- tests/components/esphome/test_manager.py | 67 +++++++++++++++++++++ 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/esphome/manager.py b/homeassistant/components/esphome/manager.py index c18ba62ac54..89e552bd596 100644 --- a/homeassistant/components/esphome/manager.py +++ b/homeassistant/components/esphome/manager.py @@ -802,8 +802,8 @@ def _async_setup_device_registry( ) suggested_area: str | None = None - if device_info.area: - # Prefer device_info.area over suggested_area + if device_info.area and device_info.area.name: + # Prefer device_info.area over suggested_area when area name is not empty suggested_area = device_info.area.name elif device_info.suggested_area: suggested_area = device_info.suggested_area @@ -830,7 +830,7 @@ def _async_setup_device_registry( for sub_device in device_info.devices: # Determine the area for this sub device sub_device_suggested_area: str | None = None - if sub_device.area_id and sub_device.area_id in areas_by_id: + if sub_device.area_id is not None and sub_device.area_id in areas_by_id: sub_device_suggested_area = areas_by_id[sub_device.area_id].name sub_device_entry = device_registry.async_get_or_create( diff --git a/tests/components/esphome/test_manager.py b/tests/components/esphome/test_manager.py index 492a576cf3d..a95af3f1046 100644 --- a/tests/components/esphome/test_manager.py +++ b/tests/components/esphome/test_manager.py @@ -1678,3 +1678,70 @@ async def test_sub_device_cleanup( ) is not None ) + + +async def test_sub_device_references_main_device_area( + hass: HomeAssistant, + mock_client: APIClient, + mock_esphome_device: MockESPHomeDeviceType, +) -> None: + """Test sub devices can reference the main device's area.""" + device_registry = dr.async_get(hass) + + # Define areas - note we don't include area_id=0 in the areas list + areas = [ + AreaInfo(area_id=1, name="Living Room"), + AreaInfo(area_id=2, name="Bedroom"), + ] + + # Define sub devices - one references the main device's area (area_id=0) + sub_devices = [ + SubDeviceInfo( + device_id=11111111, name="Motion Sensor", area_id=0 + ), # Main device area + SubDeviceInfo( + device_id=22222222, name="Light Switch", area_id=1 + ), # Living Room + SubDeviceInfo( + device_id=33333333, name="Temperature Sensor", area_id=2 + ), # Bedroom + ] + + device_info = { + "areas": areas, + "devices": sub_devices, + "area": AreaInfo(area_id=0, name="Main Hub Area"), + } + + device = await mock_esphome_device( + mock_client=mock_client, + device_info=device_info, + ) + + # Check main device has correct area + main_device = device_registry.async_get_device( + connections={(dr.CONNECTION_NETWORK_MAC, device.device_info.mac_address)} + ) + assert main_device is not None + assert main_device.suggested_area == "Main Hub Area" + + # Check sub device 1 uses main device's area + sub_device_1 = device_registry.async_get_device( + identifiers={(DOMAIN, f"{device.device_info.mac_address}_11111111")} + ) + assert sub_device_1 is not None + assert sub_device_1.suggested_area == "Main Hub Area" + + # Check sub device 2 uses Living Room + sub_device_2 = device_registry.async_get_device( + identifiers={(DOMAIN, f"{device.device_info.mac_address}_22222222")} + ) + assert sub_device_2 is not None + assert sub_device_2.suggested_area == "Living Room" + + # Check sub device 3 uses Bedroom + sub_device_3 = device_registry.async_get_device( + identifiers={(DOMAIN, f"{device.device_info.mac_address}_33333333")} + ) + assert sub_device_3 is not None + assert sub_device_3.suggested_area == "Bedroom"