From 88231aa541a2439a69c59191ec3d8a4591d68200 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 23 Oct 2020 17:40:43 +0200 Subject: [PATCH] Fix Tasmota relay acting as on/off light (#42259) --- .../components/tasmota/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/tasmota/test_light.py | 76 ++++++++++++++++++- tests/components/tasmota/test_switch.py | 4 +- 5 files changed, 79 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/tasmota/manifest.json b/homeassistant/components/tasmota/manifest.json index b64d10c1070..41fffd9ea80 100644 --- a/homeassistant/components/tasmota/manifest.json +++ b/homeassistant/components/tasmota/manifest.json @@ -3,7 +3,7 @@ "name": "Tasmota (beta)", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/tasmota", - "requirements": ["hatasmota==0.0.20"], + "requirements": ["hatasmota==0.0.21"], "dependencies": ["mqtt"], "mqtt": ["tasmota/discovery/#"], "codeowners": ["@emontnemery"] diff --git a/requirements_all.txt b/requirements_all.txt index b6512331c71..c80ec2dd9ca 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -732,7 +732,7 @@ hass-nabucasa==0.37.1 hass_splunk==0.1.1 # homeassistant.components.tasmota -hatasmota==0.0.20 +hatasmota==0.0.21 # homeassistant.components.jewish_calendar hdate==0.9.5 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index cf0f1a7beaa..b15a35929ac 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -367,7 +367,7 @@ hangups==0.4.11 hass-nabucasa==0.37.1 # homeassistant.components.tasmota -hatasmota==0.0.20 +hatasmota==0.0.21 # homeassistant.components.jewish_calendar hdate==0.9.5 diff --git a/tests/components/tasmota/test_light.py b/tests/components/tasmota/test_light.py index d2f14a151d7..34164aa44f1 100644 --- a/tests/components/tasmota/test_light.py +++ b/tests/components/tasmota/test_light.py @@ -277,7 +277,39 @@ async def test_attributes_rgbww_reduced(hass, mqtt_mock, setup_tasmota): ) -async def test_controlling_state_via_mqtt(hass, mqtt_mock, setup_tasmota): +async def test_controlling_state_via_mqtt_on_off(hass, mqtt_mock, setup_tasmota): + """Test state update via MQTT.""" + config = copy.deepcopy(DEFAULT_CONFIG) + config["rl"][0] = 1 + config["so"]["30"] = 1 # Enforce Home Assistant auto-discovery as light + mac = config["mac"] + + async_fire_mqtt_message( + hass, + f"{DEFAULT_PREFIX}/{mac}/config", + json.dumps(config), + ) + await hass.async_block_till_done() + + state = hass.states.get("light.test") + assert state.state == "unavailable" + assert not state.attributes.get(ATTR_ASSUMED_STATE) + + async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/LWT", "Online") + state = hass.states.get("light.test") + assert state.state == STATE_OFF + assert not state.attributes.get(ATTR_ASSUMED_STATE) + + async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"ON"}') + state = hass.states.get("light.test") + assert state.state == STATE_ON + + async_fire_mqtt_message(hass, "tasmota_49A3BC/tele/STATE", '{"POWER":"OFF"}') + state = hass.states.get("light.test") + assert state.state == STATE_OFF + + +async def test_controlling_state_via_mqtt_rgbww(hass, mqtt_mock, setup_tasmota): """Test state update via MQTT.""" config = copy.deepcopy(DEFAULT_CONFIG) config["rl"][0] = 2 @@ -344,7 +376,47 @@ async def test_controlling_state_via_mqtt(hass, mqtt_mock, setup_tasmota): assert state.attributes.get("effect") == "Cycle down" -async def test_sending_mqtt_commands(hass, mqtt_mock, setup_tasmota): +async def test_sending_mqtt_commands_on_off(hass, mqtt_mock, setup_tasmota): + """Test the sending MQTT commands.""" + config = copy.deepcopy(DEFAULT_CONFIG) + config["rl"][0] = 1 + config["so"]["30"] = 1 # Enforce Home Assistant auto-discovery as light + 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("light.test") + assert state.state == STATE_OFF + await hass.async_block_till_done() + await hass.async_block_till_done() + mqtt_mock.async_publish.reset_mock() + + # Turn the light on and verify MQTT message is sent + await common.async_turn_on(hass, "light.test") + mqtt_mock.async_publish.assert_called_once_with( + "tasmota_49A3BC/cmnd/Backlog", "NoDelay;Fade 0;NoDelay;Power1 ON", 0, False + ) + mqtt_mock.async_publish.reset_mock() + + # Tasmota is not optimistic, the state should still be off + state = hass.states.get("light.test") + assert state.state == STATE_OFF + + # Turn the light off and verify MQTT message is sent + await common.async_turn_off(hass, "light.test") + mqtt_mock.async_publish.assert_called_once_with( + "tasmota_49A3BC/cmnd/Backlog", "NoDelay;Fade 0;NoDelay;Power1 OFF", 0, False + ) + mqtt_mock.async_publish.reset_mock() + + +async def test_sending_mqtt_commands_rgbww(hass, mqtt_mock, setup_tasmota): """Test the sending MQTT commands.""" config = copy.deepcopy(DEFAULT_CONFIG) config["rl"][0] = 2 diff --git a/tests/components/tasmota/test_switch.py b/tests/components/tasmota/test_switch.py index 5757d161129..564dc2f2868 100644 --- a/tests/components/tasmota/test_switch.py +++ b/tests/components/tasmota/test_switch.py @@ -80,7 +80,7 @@ async def test_sending_mqtt_commands(hass, mqtt_mock, setup_tasmota): # Turn the switch on and verify MQTT message is sent await common.async_turn_on(hass, "switch.test") mqtt_mock.async_publish.assert_called_once_with( - "tasmota_49A3BC/cmnd/POWER1", "ON", 0, False + "tasmota_49A3BC/cmnd/Power1", "ON", 0, False ) mqtt_mock.async_publish.reset_mock() @@ -91,7 +91,7 @@ async def test_sending_mqtt_commands(hass, mqtt_mock, setup_tasmota): # Turn the switch off and verify MQTT message is sent await common.async_turn_off(hass, "switch.test") mqtt_mock.async_publish.assert_called_once_with( - "tasmota_49A3BC/cmnd/POWER1", "OFF", 0, False + "tasmota_49A3BC/cmnd/Power1", "OFF", 0, False ) state = hass.states.get("switch.test")