From 954ad854fb87cb035191d378fe9ca5bad3a61941 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 22 Jan 2021 23:24:06 -0600 Subject: [PATCH] Remove the ability for mqtt to set speeds that are not in the speed_list (#45445) --- homeassistant/components/mqtt/fan.py | 2 +- tests/components/mqtt/test_fan.py | 18 ++++-------------- 2 files changed, 5 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/mqtt/fan.py b/homeassistant/components/mqtt/fan.py index 5d69fb87b91..e5cebd43714 100644 --- a/homeassistant/components/mqtt/fan.py +++ b/homeassistant/components/mqtt/fan.py @@ -365,7 +365,7 @@ class MqttFan(MqttEntity, FanEntity): elif speed == SPEED_OFF: mqtt_payload = self._payload["SPEED_OFF"] else: - mqtt_payload = speed + raise ValueError(f"{speed} is not a valid fan speed") mqtt.async_publish( self.hass, diff --git a/tests/components/mqtt/test_fan.py b/tests/components/mqtt/test_fan.py index d52b67fd3ed..045b8fdaf0e 100644 --- a/tests/components/mqtt/test_fan.py +++ b/tests/components/mqtt/test_fan.py @@ -435,14 +435,8 @@ async def test_sending_mqtt_commands_and_explicit_optimistic(hass, mqtt_mock): assert state.state is STATE_OFF assert state.attributes.get(ATTR_ASSUMED_STATE) - await common.async_set_speed(hass, "fan.test", "cUsToM") - mqtt_mock.async_publish.assert_called_once_with( - "speed-command-topic", "cUsToM", 0, False - ) - mqtt_mock.async_publish.reset_mock() - state = hass.states.get("fan.test") - assert state.state is STATE_OFF - assert state.attributes.get(ATTR_ASSUMED_STATE) + with pytest.raises(ValueError): + await common.async_set_speed(hass, "fan.test", "cUsToM") async def test_attributes(hass, mqtt_mock): @@ -522,12 +516,8 @@ async def test_attributes(hass, mqtt_mock): assert state.attributes.get(fan.ATTR_SPEED) == "off" assert state.attributes.get(fan.ATTR_OSCILLATING) is False - await common.async_set_speed(hass, "fan.test", "cUsToM") - state = hass.states.get("fan.test") - assert state.state is STATE_OFF - assert state.attributes.get(ATTR_ASSUMED_STATE) - assert state.attributes.get(fan.ATTR_SPEED) == "cUsToM" - assert state.attributes.get(fan.ATTR_OSCILLATING) is False + with pytest.raises(ValueError): + await common.async_set_speed(hass, "fan.test", "cUsToM") async def test_custom_speed_list(hass, mqtt_mock):