From f837c5f1ae20bb4519e1022d187ff42c091de276 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 21 Oct 2024 11:55:40 +0000 Subject: [PATCH] Push real binary sensor states to state machine in tests --- tests/components/google_pubsub/test_init.py | 4 ++-- tests/components/homekit/test_type_sensors.py | 17 +++++++++-------- tests/components/logbook/test_websocket_api.py | 6 +++--- .../components/template/test_binary_sensor.py | 18 +++++++++--------- tests/helpers/test_event.py | 4 ++-- tests/helpers/test_template.py | 4 ++-- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/tests/components/google_pubsub/test_init.py b/tests/components/google_pubsub/test_init.py index 97e499d5d6d68a..5f160054da7738 100644 --- a/tests/components/google_pubsub/test_init.py +++ b/tests/components/google_pubsub/test_init.py @@ -148,7 +148,7 @@ async def test_allowlist(hass: HomeAssistant, mock_client) -> None: ] for test in tests: - hass.states.async_set(test.id, "not blank") + hass.states.async_set(test.id, "on") await hass.async_block_till_done() was_called = publish_client.publish.call_count == 1 @@ -178,7 +178,7 @@ async def test_denylist(hass: HomeAssistant, mock_client) -> None: ] for test in tests: - hass.states.async_set(test.id, "not blank") + hass.states.async_set(test.id, "on") await hass.async_block_till_done() was_called = publish_client.publish.call_count == 1 diff --git a/tests/components/homekit/test_type_sensors.py b/tests/components/homekit/test_type_sensors.py index ef1c124781a604..2bfddf4d4c6f3f 100644 --- a/tests/components/homekit/test_type_sensors.py +++ b/tests/components/homekit/test_type_sensors.py @@ -30,10 +30,9 @@ ATTR_UNIT_OF_MEASUREMENT, EVENT_HOMEASSISTANT_START, PERCENTAGE, - STATE_HOME, - STATE_NOT_HOME, STATE_OFF, STATE_ON, + STATE_UNAVAILABLE, STATE_UNKNOWN, UnitOfTemperature, ) @@ -535,11 +534,11 @@ async def test_binary(hass: HomeAssistant, hk_driver) -> None: await hass.async_block_till_done() assert acc.char_detected.value == 0 - hass.states.async_set(entity_id, STATE_HOME, {ATTR_DEVICE_CLASS: "opening"}) + hass.states.async_set(entity_id, STATE_UNKNOWN, {ATTR_DEVICE_CLASS: "opening"}) await hass.async_block_till_done() - assert acc.char_detected.value == 1 + assert acc.char_detected.value == 0 - hass.states.async_set(entity_id, STATE_NOT_HOME, {ATTR_DEVICE_CLASS: "opening"}) + hass.states.async_set(entity_id, STATE_UNAVAILABLE, {ATTR_DEVICE_CLASS: "opening"}) await hass.async_block_till_done() assert acc.char_detected.value == 0 @@ -579,13 +578,15 @@ async def test_motion_uses_bool(hass: HomeAssistant, hk_driver) -> None: assert acc.char_detected.value is False hass.states.async_set( - entity_id, STATE_HOME, {ATTR_DEVICE_CLASS: BinarySensorDeviceClass.MOTION} + entity_id, STATE_UNKNOWN, {ATTR_DEVICE_CLASS: BinarySensorDeviceClass.MOTION} ) await hass.async_block_till_done() - assert acc.char_detected.value is True + assert acc.char_detected.value is False hass.states.async_set( - entity_id, STATE_NOT_HOME, {ATTR_DEVICE_CLASS: BinarySensorDeviceClass.MOTION} + entity_id, + STATE_UNAVAILABLE, + {ATTR_DEVICE_CLASS: BinarySensorDeviceClass.MOTION}, ) await hass.async_block_till_done() assert acc.char_detected.value is False diff --git a/tests/components/logbook/test_websocket_api.py b/tests/components/logbook/test_websocket_api.py index 2a97556f5ad011..50139d0f4f711e 100644 --- a/tests/components/logbook/test_websocket_api.py +++ b/tests/components/logbook/test_websocket_api.py @@ -2985,8 +2985,8 @@ async def test_live_stream_with_changed_state_change( ] ) - hass.states.async_set("binary_sensor.is_light", "ignored") - hass.states.async_set("binary_sensor.is_light", "init") + hass.states.async_set("binary_sensor.is_light", "unavailable") + hass.states.async_set("binary_sensor.is_light", "unknown") await async_wait_recording_done(hass) @callback @@ -3023,7 +3023,7 @@ def auto_off_listener(event): # Make sure we get rows back in order assert recieved_rows == [ - {"entity_id": "binary_sensor.is_light", "state": "init", "when": ANY}, + {"entity_id": "binary_sensor.is_light", "state": "unknown", "when": ANY}, {"entity_id": "binary_sensor.is_light", "state": "on", "when": ANY}, {"entity_id": "binary_sensor.is_light", "state": "off", "when": ANY}, ] diff --git a/tests/components/template/test_binary_sensor.py b/tests/components/template/test_binary_sensor.py index 74662d2ab09476..3ff19190991b3e 100644 --- a/tests/components/template/test_binary_sensor.py +++ b/tests/components/template/test_binary_sensor.py @@ -253,7 +253,7 @@ async def test_setup_invalid_sensors(hass: HomeAssistant, count) -> None: "value_template": "{{ states.sensor.xyz.state }}", "icon_template": "{% if " "states.binary_sensor.test_state.state == " - "'Works' %}" + "'on' %}" "mdi:check" "{% endif %}", }, @@ -270,7 +270,7 @@ async def test_setup_invalid_sensors(hass: HomeAssistant, count) -> None: "state": "{{ states.sensor.xyz.state }}", "icon": "{% if " "states.binary_sensor.test_state.state == " - "'Works' %}" + "'on' %}" "mdi:check" "{% endif %}", }, @@ -287,7 +287,7 @@ async def test_icon_template(hass: HomeAssistant, entity_id) -> None: state = hass.states.get(entity_id) assert state.attributes.get("icon") == "" - hass.states.async_set("binary_sensor.test_state", "Works") + hass.states.async_set("binary_sensor.test_state", STATE_ON) await hass.async_block_till_done() state = hass.states.get(entity_id) assert state.attributes["icon"] == "mdi:check" @@ -306,7 +306,7 @@ async def test_icon_template(hass: HomeAssistant, entity_id) -> None: "value_template": "{{ states.sensor.xyz.state }}", "entity_picture_template": "{% if " "states.binary_sensor.test_state.state == " - "'Works' %}" + "'on' %}" "/local/sensor.png" "{% endif %}", }, @@ -323,7 +323,7 @@ async def test_icon_template(hass: HomeAssistant, entity_id) -> None: "state": "{{ states.sensor.xyz.state }}", "picture": "{% if " "states.binary_sensor.test_state.state == " - "'Works' %}" + "'on' %}" "/local/sensor.png" "{% endif %}", }, @@ -340,7 +340,7 @@ async def test_entity_picture_template(hass: HomeAssistant, entity_id) -> None: state = hass.states.get(entity_id) assert state.attributes.get("entity_picture") == "" - hass.states.async_set("binary_sensor.test_state", "Works") + hass.states.async_set("binary_sensor.test_state", STATE_ON) await hass.async_block_till_done() state = hass.states.get(entity_id) assert state.attributes["entity_picture"] == "/local/sensor.png" @@ -737,7 +737,7 @@ async def test_invalid_attribute_template( hass: HomeAssistant, caplog_setup_text ) -> None: """Test that errors are logged if rendering template fails.""" - hass.states.async_set("binary_sensor.test_sensor", "true") + hass.states.async_set("binary_sensor.test_sensor", STATE_ON) assert len(hass.states.async_all()) == 2 assert ("test_attribute") in caplog_setup_text assert ("TemplateError") in caplog_setup_text @@ -802,7 +802,7 @@ async def test_no_update_template_match_all( }, ) await hass.async_block_till_done() - hass.states.async_set("binary_sensor.test_sensor", "true") + hass.states.async_set("binary_sensor.test_sensor", STATE_ON) assert len(hass.states.async_all()) == 5 assert hass.states.get("binary_sensor.all_state").state == STATE_UNKNOWN @@ -818,7 +818,7 @@ async def test_no_update_template_match_all( assert hass.states.get("binary_sensor.all_entity_picture").state == ON assert hass.states.get("binary_sensor.all_attribute").state == ON - hass.states.async_set("binary_sensor.test_sensor", "false") + hass.states.async_set("binary_sensor.test_sensor", STATE_OFF) await hass.async_block_till_done() assert hass.states.get("binary_sensor.all_state").state == ON diff --git a/tests/helpers/test_event.py b/tests/helpers/test_event.py index 19f1ef5bb761d0..a45b418c526938 100644 --- a/tests/helpers/test_event.py +++ b/tests/helpers/test_event.py @@ -1892,10 +1892,10 @@ def specific_run_callback( "time": False, } - hass.states.async_set("binary_sensor.single", "binary_sensor_on") + hass.states.async_set("binary_sensor.single", "on") await hass.async_block_till_done() assert len(specific_runs) == 9 - assert specific_runs[8] == "binary_sensor_on" + assert specific_runs[8] == "on" assert info.listeners == { "all": False, "domains": set(), diff --git a/tests/helpers/test_template.py b/tests/helpers/test_template.py index 9a594408465c4c..b8c6b5a25af373 100644 --- a/tests/helpers/test_template.py +++ b/tests/helpers/test_template.py @@ -4549,7 +4549,7 @@ async def test_async_render_to_info_with_wildcard_matching_state( hass.states.async_set("cover.office_window", "closed") hass.states.async_set("cover.office_skylight", "open") hass.states.async_set("cover.x_skylight", "open") - hass.states.async_set("binary_sensor.door", "open") + hass.states.async_set("binary_sensor.door", "on") await hass.async_block_till_done() info = render_to_info(hass, template_complex_str) @@ -4559,7 +4559,7 @@ async def test_async_render_to_info_with_wildcard_matching_state( assert info.all_states is True assert info.rate_limit == template.ALL_STATES_RATE_LIMIT - hass.states.async_set("binary_sensor.door", "closed") + hass.states.async_set("binary_sensor.door", "off") info = render_to_info(hass, template_complex_str) assert not info.domains