Push real binary sensor states to state machine in tests (#128894)

This commit is contained in:
epenet 2024-10-21 16:19:25 +02:00 committed by GitHub
parent e861cab727
commit be4641b8f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 27 additions and 26 deletions

View File

@ -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

View File

@ -30,10 +30,9 @@ from homeassistant.const import (
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

View File

@ -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 @@ async def test_live_stream_with_changed_state_change(
# 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},
]

View File

@ -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

View File

@ -1892,10 +1892,10 @@ async def test_track_template_result_complex(hass: HomeAssistant) -> None:
"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(),

View File

@ -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