From 13dad34019e69d0c2e1f8122795699d880dfd3a6 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 5 Oct 2020 19:42:28 +0200 Subject: [PATCH] Reorder MQTT binary sensor tests (#41277) --- tests/components/mqtt/test_binary_sensor.py | 164 ++++++++++---------- 1 file changed, 82 insertions(+), 82 deletions(-) diff --git a/tests/components/mqtt/test_binary_sensor.py b/tests/components/mqtt/test_binary_sensor.py index d2375278c4d..daa18dcd486 100644 --- a/tests/components/mqtt/test_binary_sensor.py +++ b/tests/components/mqtt/test_binary_sensor.py @@ -161,6 +161,88 @@ async def expires_helper(hass, mqtt_mock, caplog): assert state.state == STATE_UNAVAILABLE +async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor( + hass, mqtt_mock, legacy_patchable_time, caplog +): + """Test that binary_sensor with expire_after set behaves correctly on discovery and discovery update.""" + entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] + await async_start(hass, "homeassistant", entry) + + config = { + "name": "Test", + "state_topic": "test-topic", + "expire_after": 4, + "force_update": True, + } + + config_msg = json.dumps(config) + + # Set time and publish config message to create binary_sensor via discovery with 4 s expiry + now = datetime(2017, 1, 1, 1, tzinfo=dt_util.UTC) + with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): + async_fire_time_changed(hass, now) + async_fire_mqtt_message( + hass, "homeassistant/binary_sensor/bla/config", config_msg + ) + await hass.async_block_till_done() + + # Test that binary_sensor is not available + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_UNAVAILABLE + + # Publish state message + with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): + async_fire_mqtt_message(hass, "test-topic", "ON") + await hass.async_block_till_done() + + # Test that binary_sensor has correct state + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_ON + + # Advance +3 seconds + now = now + timedelta(seconds=3) + with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): + async_fire_time_changed(hass, now) + await hass.async_block_till_done() + + # binary_sensor is not yet expired + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_ON + + # Resend config message to update discovery + with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): + async_fire_time_changed(hass, now) + async_fire_mqtt_message( + hass, "homeassistant/binary_sensor/bla/config", config_msg + ) + await hass.async_block_till_done() + + # Test that binary_sensor has not expired + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_ON + + # Add +2 seconds + now = now + timedelta(seconds=2) + with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): + async_fire_time_changed(hass, now) + await hass.async_block_till_done() + + # Test that binary_sensor has expired + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_UNAVAILABLE + + # Resend config message to update discovery + with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): + async_fire_mqtt_message( + hass, "homeassistant/binary_sensor/bla/config", config_msg + ) + await hass.async_block_till_done() + + # Test that binary_sensor is still expired + state = hass.states.get("binary_sensor.test") + assert state.state == STATE_UNAVAILABLE + + async def test_setting_sensor_value_via_mqtt_message(hass, mqtt_mock): """Test the setting of the value via MQTT.""" assert await async_setup_component( @@ -666,88 +748,6 @@ async def test_discovery_update_unchanged_binary_sensor(hass, mqtt_mock, caplog) ) -async def test_expiration_on_discovery_and_discovery_update_of_binary_sensor( - hass, mqtt_mock, legacy_patchable_time, caplog -): - """Test that binary_sensor with expire_after set behaves correctly on discovery and discovery update.""" - entry = hass.config_entries.async_entries(mqtt.DOMAIN)[0] - await async_start(hass, "homeassistant", entry) - - config = { - "name": "Test", - "state_topic": "test-topic", - "expire_after": 4, - "force_update": True, - } - - config_msg = json.dumps(config) - - # Set time and publish config message to create binary_sensor via discovery with 4 s expiry - now = datetime(2017, 1, 1, 1, tzinfo=dt_util.UTC) - with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): - async_fire_time_changed(hass, now) - async_fire_mqtt_message( - hass, "homeassistant/binary_sensor/bla/config", config_msg - ) - await hass.async_block_till_done() - - # Test that binary_sensor is not available - state = hass.states.get("binary_sensor.test") - assert state.state == STATE_UNAVAILABLE - - # Publish state message - with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): - async_fire_mqtt_message(hass, "test-topic", "ON") - await hass.async_block_till_done() - - # Test that binary_sensor has correct state - state = hass.states.get("binary_sensor.test") - assert state.state == STATE_ON - - # Advance +3 seconds - now = now + timedelta(seconds=3) - with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): - async_fire_time_changed(hass, now) - await hass.async_block_till_done() - - # binary_sensor is not yet expired - state = hass.states.get("binary_sensor.test") - assert state.state == STATE_ON - - # Resend config message to update discovery - with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): - async_fire_time_changed(hass, now) - async_fire_mqtt_message( - hass, "homeassistant/binary_sensor/bla/config", config_msg - ) - await hass.async_block_till_done() - - # Test that binary_sensor has not expired - state = hass.states.get("binary_sensor.test") - assert state.state == STATE_ON - - # Add +2 seconds - now = now + timedelta(seconds=2) - with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): - async_fire_time_changed(hass, now) - await hass.async_block_till_done() - - # Test that binary_sensor has expired - state = hass.states.get("binary_sensor.test") - assert state.state == STATE_UNAVAILABLE - - # Resend config message to update discovery - with patch(("homeassistant.helpers.event.dt_util.utcnow"), return_value=now): - async_fire_mqtt_message( - hass, "homeassistant/binary_sensor/bla/config", config_msg - ) - await hass.async_block_till_done() - - # Test that binary_sensor is still expired - state = hass.states.get("binary_sensor.test") - assert state.state == STATE_UNAVAILABLE - - @pytest.mark.no_fail_on_log_exception async def test_discovery_broken(hass, mqtt_mock, caplog): """Test handling of bad discovery message."""