Skip to content

Commit e5f3ccb

Browse files
authored
Improve roborock test accuracy/robustness (#160021)
1 parent 560b91b commit e5f3ccb

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

tests/components/roborock/conftest.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ async def close(self) -> None:
175175
"""Close the device."""
176176

177177

178+
def set_trait_attributes(
179+
trait: AsyncMock,
180+
dataclass_template: RoborockBase,
181+
init_none: bool = False,
182+
) -> None:
183+
"""Set attributes on a mock roborock trait."""
184+
template_copy = deepcopy(dataclass_template)
185+
for attr_name in dir(template_copy):
186+
if attr_name.startswith("_"):
187+
continue
188+
attr_value = getattr(template_copy, attr_name) if not init_none else None
189+
setattr(trait, attr_name, attr_value)
190+
191+
178192
def make_mock_trait(
179193
trait_spec: type[V1TraitMixin] | None = None,
180194
dataclass_template: RoborockBase | None = None,
@@ -183,12 +197,14 @@ def make_mock_trait(
183197
trait = AsyncMock(spec=trait_spec or V1TraitMixin)
184198
if dataclass_template is not None:
185199
# Copy all attributes and property methods (e.g. computed properties)
186-
template_copy = deepcopy(dataclass_template)
187-
for attr_name in dir(template_copy):
188-
if attr_name.startswith("_"):
189-
continue
190-
setattr(trait, attr_name, getattr(template_copy, attr_name))
191-
trait.refresh = AsyncMock()
200+
# on the first call to refresh(). The object starts uninitialized.
201+
set_trait_attributes(trait, dataclass_template, init_none=True)
202+
203+
async def refresh() -> None:
204+
if dataclass_template is not None:
205+
set_trait_attributes(trait, dataclass_template)
206+
207+
trait.refresh = AsyncMock(side_effect=refresh)
192208
return trait
193209

194210

tests/components/roborock/test_vacuum.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from homeassistant.helpers import device_registry as dr, entity_registry as er
3232
from homeassistant.setup import async_setup_component
3333

34-
from .conftest import FakeDevice
34+
from .conftest import FakeDevice, set_trait_attributes
35+
from .mock_data import STATUS
3536

3637
from tests.common import MockConfigEntry
3738

@@ -134,8 +135,14 @@ async def test_resume_cleaning(
134135
vacuum_command: Mock,
135136
) -> None:
136137
"""Test resuming clean on start button when a clean is paused."""
137-
fake_vacuum.v1_properties.status.in_cleaning = in_cleaning_int
138-
fake_vacuum.v1_properties.status.in_returning = in_returning_int
138+
139+
async def refresh_properties() -> None:
140+
set_trait_attributes(fake_vacuum.v1_properties.status, STATUS)
141+
fake_vacuum.v1_properties.status.in_cleaning = in_cleaning_int
142+
fake_vacuum.v1_properties.status.in_returning = in_returning_int
143+
144+
fake_vacuum.v1_properties.status.refresh.side_effect = refresh_properties
145+
139146
await async_setup_component(hass, DOMAIN, {})
140147
vacuum = hass.states.get(ENTITY_ID)
141148
assert vacuum

0 commit comments

Comments
 (0)