mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Fix via_device race in yardian (#177859)
This commit is contained in:
@@ -4,6 +4,7 @@ from pyyardian import AsyncYardianClient
|
||||
|
||||
from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .coordinator import YardianConfigEntry, YardianUpdateCoordinator
|
||||
@@ -33,6 +34,13 @@ async def async_setup_entry(hass: HomeAssistant, entry: YardianConfigEntry) -> b
|
||||
|
||||
entry.runtime_data = coordinator
|
||||
|
||||
# Register the main device so zone child devices can resolve it as their
|
||||
# via_device regardless of platform setup order.
|
||||
dr.async_get(hass).async_get_or_create(
|
||||
config_entry_id=entry.entry_id,
|
||||
**coordinator.device_info,
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
return True
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
"""Base entities for Yardian integration."""
|
||||
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
@@ -31,5 +32,9 @@ class YardianZoneEntity(CoordinatorEntity[YardianUpdateCoordinator]):
|
||||
identifiers={(DOMAIN, f"{coordinator.yid}_{zone_id}")},
|
||||
name=coordinator.data.zones[zone_id].name,
|
||||
manufacturer=MANUFACTURER,
|
||||
via_device=(DOMAIN, coordinator.yid),
|
||||
via_device_id=dr.async_get_device_id_by_identifier(
|
||||
coordinator.hass,
|
||||
(DOMAIN, coordinator.yid),
|
||||
config_entry_id=coordinator.config_entry.entry_id,
|
||||
),
|
||||
)
|
||||
|
||||
@@ -81,6 +81,13 @@ def sensor_platform_only() -> Generator[None]:
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def switch_platform_only() -> Generator[None]:
|
||||
"""Limit the integration setup to the switch (child-only) platform."""
|
||||
with patch("homeassistant.components.yardian.PLATFORMS", [Platform.SWITCH]):
|
||||
yield
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def mock_ui_refresh_delay() -> Generator[None]:
|
||||
"""Patch the Switch refresh delay to 0 seconds to speed up tests."""
|
||||
|
||||
@@ -5,8 +5,10 @@ from unittest.mock import AsyncMock
|
||||
import pytest
|
||||
from pyyardian import NetworkException, NotAuthorizedException
|
||||
|
||||
from homeassistant.components.yardian.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from . import setup_integration
|
||||
|
||||
@@ -35,3 +37,24 @@ async def test_setup_unauthorized(
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
assert mock_config_entry.state is entry_state
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_yardian_client", "switch_platform_only")
|
||||
async def test_zone_device_via_device(
|
||||
hass: HomeAssistant,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
) -> None:
|
||||
"""Test that zone child devices link to the main device via via_device_id."""
|
||||
await setup_integration(hass, mock_config_entry)
|
||||
|
||||
main_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, "yid123"), mock_config_entry.entry_id
|
||||
)
|
||||
zone_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, "yid123_0"), mock_config_entry.entry_id
|
||||
)
|
||||
|
||||
assert main_device is not None
|
||||
assert zone_device is not None
|
||||
assert zone_device.via_device_id == main_device.id
|
||||
|
||||
Reference in New Issue
Block a user