Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions homeassistant/components/roborock/vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,16 @@ async def async_set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None:
translation_domain=DOMAIN,
translation_key="update_options_failed",
)
await self.send(
RoborockCommand.SET_CUSTOM_MODE,
[
{v: k for k, v in self._status_trait.fan_speed_mapping.items()}[
fan_speed
]
],
)
code_mapping = {v: k for k, v in self._status_trait.fan_speed_mapping.items()}
if (fan_speed_code := code_mapping.get(fan_speed)) is None:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="invalid_fan_speed",
translation_placeholders={
"fan_speed": fan_speed,
},
)
await self.send(RoborockCommand.SET_CUSTOM_MODE, [fan_speed_code])

async def async_set_vacuum_goto_position(self, x: int, y: int) -> None:
"""Send vacuum to a specific target point."""
Expand Down Expand Up @@ -458,9 +460,17 @@ async def async_locate(self, **kwargs: Any) -> None:
async def async_set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None:
"""Set vacuum fan speed."""
try:
await self.coordinator.api.set_fan_speed(
SCWindMapping.from_value(fan_speed)
)
fan_speed_code = SCWindMapping.from_value(fan_speed)
except ValueError as err:
raise ServiceValidationError(
translation_domain=DOMAIN,
translation_key="invalid_fan_speed",
translation_placeholders={
"fan_speed": fan_speed,
},
) from err
try:
await self.coordinator.api.set_fan_speed(fan_speed_code)
except RoborockException as err:
raise HomeAssistantError(
translation_domain=DOMAIN,
Expand Down
42 changes: 23 additions & 19 deletions tests/components/roborock/test_vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,29 @@ async def test_commands(
assert vacuum_command.send.call_args == call(command, params=called_params)


@pytest.mark.parametrize(
"entity_id",
[
ENTITY_ID,
Q7_ENTITY_ID,
Q10_ENTITY_ID,
],
)
async def test_set_fan_speed_invalid(
hass: HomeAssistant,
setup_entry: MockConfigEntry,
entity_id: str,
) -> None:
"""Test calling set_fan_speed with an invalid mode."""
with pytest.raises(ServiceValidationError, match="Invalid fan speed: some-mode"):
await hass.services.async_call(
VACUUM_DOMAIN,
SERVICE_SET_FAN_SPEED,
{ATTR_ENTITY_ID: entity_id, "fan_speed": "some-mode"},
blocking=True,
)


@pytest.mark.parametrize(
("in_cleaning_int", "in_returning_int", "expected_command"),
[
Expand Down Expand Up @@ -880,25 +903,6 @@ async def test_q10_set_fan_speed_command(
assert q10_vacuum_api.vacuum.set_fan_level.call_args[0] == (YXFanLevel.QUIET,)


async def test_q10_set_invalid_fan_speed(
hass: HomeAssistant,
setup_entry: MockConfigEntry,
q10_vacuum_api: Mock,
) -> None:
"""Test that setting an invalid fan speed raises an error."""
vacuum = hass.states.get(Q10_ENTITY_ID)
assert vacuum

with pytest.raises(ServiceValidationError):
await hass.services.async_call(
VACUUM_DOMAIN,
SERVICE_SET_FAN_SPEED,
{ATTR_ENTITY_ID: Q10_ENTITY_ID, "fan_speed": "invalid_speed"},
blocking=True,
)
assert q10_vacuum_api.vacuum.set_fan_level.call_count == 0


@pytest.mark.parametrize(
"command",
[
Expand Down
Loading