This commit is contained in:
J. Nick Koston
2025-06-23 23:48:43 +02:00
parent fe8f0a781a
commit 2ecd334f80

View File

@@ -1039,7 +1039,7 @@ async def test_entity_switches_devices_entity_not_in_registry(
mock_client: APIClient,
mock_esphome_device: MockESPHomeDeviceType,
) -> None:
"""Test entity device switch when entity is not in registry."""
"""Test entity device switch when one entity is not in registry."""
# Define sub devices
sub_devices = [
SubDeviceInfo(device_id=11111111, name="Sub Device 1", area_id=0),
@@ -1050,19 +1050,27 @@ async def test_entity_switches_devices_entity_not_in_registry(
"devices": sub_devices,
}
# Create initial entity on sub device 1
# Create initial entities on sub device 1
entity_info = [
BinarySensorInfo(
object_id="sensor",
object_id="sensor1",
key=1,
name="Test Sensor",
unique_id="sensor",
name="Test Sensor 1",
unique_id="sensor1",
device_id=11111111,
),
BinarySensorInfo(
object_id="sensor2",
key=2,
name="Test Sensor 2",
unique_id="sensor2",
device_id=11111111,
),
]
states = [
BinarySensorState(key=1, state=True, missing_state=False),
BinarySensorState(key=2, state=False, missing_state=False),
]
device = await mock_esphome_device(
@@ -1072,39 +1080,60 @@ async def test_entity_switches_devices_entity_not_in_registry(
states=states,
)
# Verify entity is created on sub device 1
# Verify both entities are created on sub device 1
sub_device_1 = device_registry.async_get_device(
identifiers={(DOMAIN, f"{device.device_info.mac_address}_11111111")}
)
assert sub_device_1 is not None
sensor_entity = entity_registry.async_get("binary_sensor.test_sensor")
assert sensor_entity is not None
assert sensor_entity.device_id == sub_device_1.id
sensor1_entity = entity_registry.async_get("binary_sensor.test_sensor1")
assert sensor1_entity is not None
assert sensor1_entity.device_id == sub_device_1.id
# Remove the entity from registry
entity_registry.async_remove(sensor_entity.entity_id)
sensor2_entity = entity_registry.async_get("binary_sensor.test_sensor2")
assert sensor2_entity is not None
assert sensor2_entity.device_id == sub_device_1.id
# Remove sensor1 from registry
entity_registry.async_remove(sensor1_entity.entity_id)
# Verify it's removed
assert entity_registry.async_get("binary_sensor.test_sensor") is None
assert entity_registry.async_get("binary_sensor.test_sensor1") is None
# Try to update entity to move to sub device 2
# Try to update both entities to move to sub device 2
updated_entity_info = [
BinarySensorInfo(
object_id="sensor",
object_id="sensor1",
key=1,
name="Test Sensor",
unique_id="sensor",
name="Test Sensor 1",
unique_id="sensor1",
device_id=22222222, # Try to move to sub device 2
),
BinarySensorInfo(
object_id="sensor2",
key=2,
name="Test Sensor 2",
unique_id="sensor2",
device_id=22222222, # Move to sub device 2
),
]
# Update should not fail even though entity is not in registry
# Update should not fail even though sensor1 is not in registry
mock_client.list_entities_services = AsyncMock(
return_value=(updated_entity_info, [])
)
await device.mock_disconnect(expected_disconnect=False)
await device.mock_connect()
# Entity should remain not in registry
assert entity_registry.async_get("binary_sensor.test_sensor") is None
# Sensor1 should remain not in registry
assert entity_registry.async_get("binary_sensor.test_sensor1") is None
# But sensor2 should have moved to sub device 2
sub_device_2 = device_registry.async_get_device(
identifiers={(DOMAIN, f"{device.device_info.mac_address}_22222222")}
)
assert sub_device_2 is not None
sensor2_entity = entity_registry.async_get("binary_sensor.test_sensor2")
assert sensor2_entity is not None
assert sensor2_entity.device_id == sub_device_2.id