From f89de613d9449f1edd717f00f333dd0546985fb4 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Thu, 17 Feb 2022 00:06:42 +0100 Subject: [PATCH] Improve MQTT binary_sensor test (#66688) --- tests/components/mqtt/test_binary_sensor.py | 24 ++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index 5fa71d73632..5055550be7c 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -887,8 +887,12 @@ async def test_reloadable_late(hass, mqtt_client_mock, caplog, tmp_path): await help_test_reloadable_late(hass, caplog, tmp_path, domain, config) +@pytest.mark.parametrize( + "payload1, state1, payload2, state2", + [("ON", "on", "OFF", "off"), ("OFF", "off", "ON", "on")], +) async def test_cleanup_triggers_and_restoring_state( - hass, mqtt_mock, caplog, tmp_path, freezer + hass, mqtt_mock, caplog, tmp_path, freezer, payload1, state1, payload2, state2 ): """Test cleanup old triggers at reloading and restoring the state.""" domain = binary_sensor.DOMAIN @@ -909,13 +913,13 @@ async def test_cleanup_triggers_and_restoring_state( {binary_sensor.DOMAIN: [config1, config2]}, ) await hass.async_block_till_done() - async_fire_mqtt_message(hass, "test-topic1", "ON") + async_fire_mqtt_message(hass, "test-topic1", payload1) state = hass.states.get("binary_sensor.test1") - assert state.state == "on" + assert state.state == state1 - async_fire_mqtt_message(hass, "test-topic2", "ON") + async_fire_mqtt_message(hass, "test-topic2", payload1) state = hass.states.get("binary_sensor.test2") - assert state.state == "on" + assert state.state == state1 freezer.move_to("2022-02-02 12:01:10+01:00") @@ -931,18 +935,18 @@ async def test_cleanup_triggers_and_restoring_state( assert "State recovered after reload for binary_sensor.test2" not in caplog.text state = hass.states.get("binary_sensor.test1") - assert state.state == "on" + assert state.state == state1 state = hass.states.get("binary_sensor.test2") assert state.state == STATE_UNAVAILABLE - async_fire_mqtt_message(hass, "test-topic1", "OFF") + async_fire_mqtt_message(hass, "test-topic1", payload2) state = hass.states.get("binary_sensor.test1") - assert state.state == "off" + assert state.state == state2 - async_fire_mqtt_message(hass, "test-topic2", "OFF") + async_fire_mqtt_message(hass, "test-topic2", payload2) state = hass.states.get("binary_sensor.test2") - assert state.state == "off" + assert state.state == state2 async def test_skip_restoring_state_with_over_due_expire_trigger(