Improve deCONZ fan platform handling unsupported commands (#41779)

* Improve handling unsupported commands

* Raise valueerror on unsupported speed

* Fix linting
pull/41784/head
Robert Svensson 2020-10-13 19:34:33 +02:00 committed by GitHub
parent 7f9db59209
commit 267d97e80e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -105,7 +105,11 @@ class DeconzFan(DeconzDevice, FanEntity):
async def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan."""
if speed not in SPEEDS:
raise ValueError(f"Unsupported speed {speed}")
data = {"speed": SPEEDS[speed]}
await self._device.async_set_state(data)
async def async_turn_on(self, speed: str = None, **kwargs) -> None:

View File

@ -1,6 +1,8 @@
"""deCONZ fan platform tests."""
from copy import deepcopy
import pytest
from homeassistant.components import deconz
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
import homeassistant.components.fan as fan
@ -165,7 +167,20 @@ async def test_fans(hass):
await hass.async_block_till_done()
set_callback.assert_called_with("put", "/lights/1/state", json={"speed": 0})
# Verify that an unsupported speed gets converted to default speed "medium"
# Service set fan speed to unsupported value
with patch.object(
ceiling_fan_device, "_request", return_value=True
) as set_callback, pytest.raises(ValueError):
await hass.services.async_call(
fan.DOMAIN,
fan.SERVICE_SET_SPEED,
{"entity_id": "fan.ceiling_fan", fan.ATTR_SPEED: "bad value"},
blocking=True,
)
await hass.async_block_till_done()
# Events with an unsupported speed gets converted to default speed "medium"
state_changed_event = {
"t": "event",