From 7084d6c650d27babfabf0cf34a9e1aa492927820 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 10 Dec 2020 22:17:58 +0100 Subject: [PATCH] Address old review comments of Tasmota fan (#44112) * Address review comments of Tasmota fan * Address review comment --- homeassistant/components/tasmota/fan.py | 4 +++- tests/components/tasmota/test_fan.py | 28 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/tasmota/fan.py b/homeassistant/components/tasmota/fan.py index 362149e9fca..bdcc00dc764 100644 --- a/homeassistant/components/tasmota/fan.py +++ b/homeassistant/components/tasmota/fan.py @@ -63,7 +63,7 @@ class TasmotaFan( @property def speed_list(self): """Get the list of available speeds.""" - return list(HA_TO_TASMOTA_SPEED_MAP.keys()) + return list(HA_TO_TASMOTA_SPEED_MAP) @property def supported_features(self): @@ -72,6 +72,8 @@ class TasmotaFan( async def async_set_speed(self, speed): """Set the speed of the fan.""" + if speed not in HA_TO_TASMOTA_SPEED_MAP: + raise ValueError(f"Unsupported speed {speed}") if speed == fan.SPEED_OFF: await self.async_turn_off() else: diff --git a/tests/components/tasmota/test_fan.py b/tests/components/tasmota/test_fan.py index 5cadc20218e..1aca8c84e07 100644 --- a/tests/components/tasmota/test_fan.py +++ b/tests/components/tasmota/test_fan.py @@ -7,6 +7,7 @@ from hatasmota.utils import ( get_topic_tele_state, get_topic_tele_will, ) +import pytest from homeassistant.components import fan from homeassistant.components.tasmota.const import DEFAULT_PREFIX @@ -152,6 +153,33 @@ async def test_sending_mqtt_commands(hass, mqtt_mock, setup_tasmota): ) +async def test_invalid_fan_speed(hass, mqtt_mock, setup_tasmota): + """Test the sending MQTT commands.""" + config = copy.deepcopy(DEFAULT_CONFIG) + config["if"] = 1 + mac = config["mac"] + + async_fire_mqtt_message( + hass, + f"{DEFAULT_PREFIX}/{mac}/config", + json.dumps(config), + ) + await hass.async_block_till_done() + + async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/LWT", "Online") + state = hass.states.get("fan.tasmota") + assert state.state == STATE_OFF + await hass.async_block_till_done() + await hass.async_block_till_done() + mqtt_mock.async_publish.reset_mock() + + # Set an unsupported speed and verify MQTT message is not sent + with pytest.raises(ValueError) as excinfo: + await common.async_set_speed(hass, "fan.tasmota", "no_such_speed") + assert "Unsupported speed no_such_speed" in str(excinfo.value) + mqtt_mock.async_publish.assert_not_called() + + async def test_availability_when_connection_lost( hass, mqtt_client_mock, mqtt_mock, setup_tasmota ):