Improve roborock test accuracy/robustness (#160021)

This commit is contained in:
Allen Porter
2025-12-31 16:32:53 +01:00
committed by GitHub
parent 560b91b93b
commit e5f3ccb38d
2 changed files with 32 additions and 9 deletions
+22 -6
View File
@@ -175,6 +175,20 @@ class FakeDevice(RoborockDevice):
"""Close the device."""
def set_trait_attributes(
trait: AsyncMock,
dataclass_template: RoborockBase,
init_none: bool = False,
) -> None:
"""Set attributes on a mock roborock trait."""
template_copy = deepcopy(dataclass_template)
for attr_name in dir(template_copy):
if attr_name.startswith("_"):
continue
attr_value = getattr(template_copy, attr_name) if not init_none else None
setattr(trait, attr_name, attr_value)
def make_mock_trait(
trait_spec: type[V1TraitMixin] | None = None,
dataclass_template: RoborockBase | None = None,
@@ -183,12 +197,14 @@ def make_mock_trait(
trait = AsyncMock(spec=trait_spec or V1TraitMixin)
if dataclass_template is not None:
# Copy all attributes and property methods (e.g. computed properties)
template_copy = deepcopy(dataclass_template)
for attr_name in dir(template_copy):
if attr_name.startswith("_"):
continue
setattr(trait, attr_name, getattr(template_copy, attr_name))
trait.refresh = AsyncMock()
# on the first call to refresh(). The object starts uninitialized.
set_trait_attributes(trait, dataclass_template, init_none=True)
async def refresh() -> None:
if dataclass_template is not None:
set_trait_attributes(trait, dataclass_template)
trait.refresh = AsyncMock(side_effect=refresh)
return trait
+10 -3
View File
@@ -31,7 +31,8 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component
from .conftest import FakeDevice
from .conftest import FakeDevice, set_trait_attributes
from .mock_data import STATUS
from tests.common import MockConfigEntry
@@ -134,8 +135,14 @@ async def test_resume_cleaning(
vacuum_command: Mock,
) -> None:
"""Test resuming clean on start button when a clean is paused."""
fake_vacuum.v1_properties.status.in_cleaning = in_cleaning_int
fake_vacuum.v1_properties.status.in_returning = in_returning_int
async def refresh_properties() -> None:
set_trait_attributes(fake_vacuum.v1_properties.status, STATUS)
fake_vacuum.v1_properties.status.in_cleaning = in_cleaning_int
fake_vacuum.v1_properties.status.in_returning = in_returning_int
fake_vacuum.v1_properties.status.refresh.side_effect = refresh_properties
await async_setup_component(hass, DOMAIN, {})
vacuum = hass.states.get(ENTITY_ID)
assert vacuum