Use STATE_ON/STATE_OFF constants in template test (#128883)

This commit is contained in:
epenet 2024-10-21 16:19:44 +02:00 committed by GitHub
parent be4641b8f3
commit 838519e89f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -33,9 +33,6 @@ from tests.common import (
mock_restore_cache_with_extra_data, mock_restore_cache_with_extra_data,
) )
ON = "on"
OFF = "off"
@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize("count", [1])
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -78,7 +75,7 @@ async def test_setup_minimal(hass: HomeAssistant, entity_id, name, attributes) -
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.name == name assert state.name == name
assert state.state == ON assert state.state == STATE_ON
assert state.attributes == attributes assert state.attributes == attributes
@ -123,7 +120,7 @@ async def test_setup(hass: HomeAssistant, entity_id) -> None:
state = hass.states.get(entity_id) state = hass.states.get(entity_id)
assert state is not None assert state is not None
assert state.name == "virtual thingy" assert state.name == "virtual thingy"
assert state.state == ON assert state.state == STATE_ON
assert state.attributes["device_class"] == "motion" assert state.attributes["device_class"] == "motion"
@ -460,13 +457,13 @@ async def test_match_all(hass: HomeAssistant, setup_mock) -> None:
async def test_event(hass: HomeAssistant) -> None: async def test_event(hass: HomeAssistant) -> None:
"""Test the event.""" """Test the event."""
state = hass.states.get("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == OFF assert state.state == STATE_OFF
hass.states.async_set("sensor.test_state", ON) hass.states.async_set("sensor.test_state", STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == ON assert state.state == STATE_ON
@pytest.mark.parametrize( @pytest.mark.parametrize(
@ -571,42 +568,42 @@ async def test_event(hass: HomeAssistant) -> None:
async def test_template_delay_on_off(hass: HomeAssistant) -> None: async def test_template_delay_on_off(hass: HomeAssistant) -> None:
"""Test binary sensor template delay on.""" """Test binary sensor template delay on."""
# Ensure the initial state is not on # Ensure the initial state is not on
assert hass.states.get("binary_sensor.test_on").state != ON assert hass.states.get("binary_sensor.test_on").state != STATE_ON
assert hass.states.get("binary_sensor.test_off").state != ON assert hass.states.get("binary_sensor.test_off").state != STATE_ON
hass.states.async_set("input_number.delay", 5) hass.states.async_set("input_number.delay", 5)
hass.states.async_set("sensor.test_state", ON) hass.states.async_set("sensor.test_state", STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.test_on").state == OFF assert hass.states.get("binary_sensor.test_on").state == STATE_OFF
assert hass.states.get("binary_sensor.test_off").state == ON assert hass.states.get("binary_sensor.test_off").state == STATE_ON
future = dt_util.utcnow() + timedelta(seconds=5) future = dt_util.utcnow() + timedelta(seconds=5)
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.test_on").state == ON assert hass.states.get("binary_sensor.test_on").state == STATE_ON
assert hass.states.get("binary_sensor.test_off").state == ON assert hass.states.get("binary_sensor.test_off").state == STATE_ON
# check with time changes # check with time changes
hass.states.async_set("sensor.test_state", OFF) hass.states.async_set("sensor.test_state", STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.test_on").state == OFF assert hass.states.get("binary_sensor.test_on").state == STATE_OFF
assert hass.states.get("binary_sensor.test_off").state == ON assert hass.states.get("binary_sensor.test_off").state == STATE_ON
hass.states.async_set("sensor.test_state", ON) hass.states.async_set("sensor.test_state", STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.test_on").state == OFF assert hass.states.get("binary_sensor.test_on").state == STATE_OFF
assert hass.states.get("binary_sensor.test_off").state == ON assert hass.states.get("binary_sensor.test_off").state == STATE_ON
hass.states.async_set("sensor.test_state", OFF) hass.states.async_set("sensor.test_state", STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.test_on").state == OFF assert hass.states.get("binary_sensor.test_on").state == STATE_OFF
assert hass.states.get("binary_sensor.test_off").state == ON assert hass.states.get("binary_sensor.test_off").state == STATE_ON
future = dt_util.utcnow() + timedelta(seconds=5) future = dt_util.utcnow() + timedelta(seconds=5)
async_fire_time_changed(hass, future) async_fire_time_changed(hass, future)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.test_on").state == OFF assert hass.states.get("binary_sensor.test_on").state == STATE_OFF
assert hass.states.get("binary_sensor.test_off").state == OFF assert hass.states.get("binary_sensor.test_off").state == STATE_OFF
@pytest.mark.parametrize("count", [1]) @pytest.mark.parametrize("count", [1])
@ -813,29 +810,29 @@ async def test_no_update_template_match_all(
hass.bus.async_fire(EVENT_HOMEASSISTANT_START) hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.all_state").state == ON assert hass.states.get("binary_sensor.all_state").state == STATE_ON
assert hass.states.get("binary_sensor.all_icon").state == ON assert hass.states.get("binary_sensor.all_icon").state == STATE_ON
assert hass.states.get("binary_sensor.all_entity_picture").state == ON assert hass.states.get("binary_sensor.all_entity_picture").state == STATE_ON
assert hass.states.get("binary_sensor.all_attribute").state == ON assert hass.states.get("binary_sensor.all_attribute").state == STATE_ON
hass.states.async_set("binary_sensor.test_sensor", STATE_OFF) hass.states.async_set("binary_sensor.test_sensor", STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get("binary_sensor.all_state").state == ON assert hass.states.get("binary_sensor.all_state").state == STATE_ON
# Will now process because we have one valid template # Will now process because we have one valid template
assert hass.states.get("binary_sensor.all_icon").state == OFF assert hass.states.get("binary_sensor.all_icon").state == STATE_OFF
assert hass.states.get("binary_sensor.all_entity_picture").state == OFF assert hass.states.get("binary_sensor.all_entity_picture").state == STATE_OFF
assert hass.states.get("binary_sensor.all_attribute").state == OFF assert hass.states.get("binary_sensor.all_attribute").state == STATE_OFF
await async_update_entity(hass, "binary_sensor.all_state") await async_update_entity(hass, "binary_sensor.all_state")
await async_update_entity(hass, "binary_sensor.all_icon") await async_update_entity(hass, "binary_sensor.all_icon")
await async_update_entity(hass, "binary_sensor.all_entity_picture") await async_update_entity(hass, "binary_sensor.all_entity_picture")
await async_update_entity(hass, "binary_sensor.all_attribute") await async_update_entity(hass, "binary_sensor.all_attribute")
assert hass.states.get("binary_sensor.all_state").state == ON assert hass.states.get("binary_sensor.all_state").state == STATE_ON
assert hass.states.get("binary_sensor.all_icon").state == OFF assert hass.states.get("binary_sensor.all_icon").state == STATE_OFF
assert hass.states.get("binary_sensor.all_entity_picture").state == OFF assert hass.states.get("binary_sensor.all_entity_picture").state == STATE_OFF
assert hass.states.get("binary_sensor.all_attribute").state == OFF assert hass.states.get("binary_sensor.all_attribute").state == STATE_OFF
@pytest.mark.parametrize(("count", "domain"), [(1, "template")]) @pytest.mark.parametrize(("count", "domain"), [(1, "template")])
@ -848,7 +845,7 @@ async def test_no_update_template_match_all(
"binary_sensor": { "binary_sensor": {
"name": "top-level", "name": "top-level",
"unique_id": "sensor-id", "unique_id": "sensor-id",
"state": ON, "state": STATE_ON,
}, },
}, },
"binary_sensor": { "binary_sensor": {
@ -1008,30 +1005,30 @@ async def test_availability_icon_picture(hass: HomeAssistant, entity_id) -> None
@pytest.mark.parametrize( @pytest.mark.parametrize(
("extra_config", "source_state", "restored_state", "initial_state"), ("extra_config", "source_state", "restored_state", "initial_state"),
[ [
({}, OFF, ON, OFF), ({}, STATE_OFF, STATE_ON, STATE_OFF),
({}, OFF, OFF, OFF), ({}, STATE_OFF, STATE_OFF, STATE_OFF),
({}, OFF, STATE_UNAVAILABLE, OFF), ({}, STATE_OFF, STATE_UNAVAILABLE, STATE_OFF),
({}, OFF, STATE_UNKNOWN, OFF), ({}, STATE_OFF, STATE_UNKNOWN, STATE_OFF),
({"delay_off": 5}, OFF, ON, ON), ({"delay_off": 5}, STATE_OFF, STATE_ON, STATE_ON),
({"delay_off": 5}, OFF, OFF, OFF), ({"delay_off": 5}, STATE_OFF, STATE_OFF, STATE_OFF),
({"delay_off": 5}, OFF, STATE_UNAVAILABLE, STATE_UNKNOWN), ({"delay_off": 5}, STATE_OFF, STATE_UNAVAILABLE, STATE_UNKNOWN),
({"delay_off": 5}, OFF, STATE_UNKNOWN, STATE_UNKNOWN), ({"delay_off": 5}, STATE_OFF, STATE_UNKNOWN, STATE_UNKNOWN),
({"delay_on": 5}, OFF, ON, OFF), ({"delay_on": 5}, STATE_OFF, STATE_ON, STATE_OFF),
({"delay_on": 5}, OFF, OFF, OFF), ({"delay_on": 5}, STATE_OFF, STATE_OFF, STATE_OFF),
({"delay_on": 5}, OFF, STATE_UNAVAILABLE, OFF), ({"delay_on": 5}, STATE_OFF, STATE_UNAVAILABLE, STATE_OFF),
({"delay_on": 5}, OFF, STATE_UNKNOWN, OFF), ({"delay_on": 5}, STATE_OFF, STATE_UNKNOWN, STATE_OFF),
({}, ON, ON, ON), ({}, STATE_ON, STATE_ON, STATE_ON),
({}, ON, OFF, ON), ({}, STATE_ON, STATE_OFF, STATE_ON),
({}, ON, STATE_UNAVAILABLE, ON), ({}, STATE_ON, STATE_UNAVAILABLE, STATE_ON),
({}, ON, STATE_UNKNOWN, ON), ({}, STATE_ON, STATE_UNKNOWN, STATE_ON),
({"delay_off": 5}, ON, ON, ON), ({"delay_off": 5}, STATE_ON, STATE_ON, STATE_ON),
({"delay_off": 5}, ON, OFF, ON), ({"delay_off": 5}, STATE_ON, STATE_OFF, STATE_ON),
({"delay_off": 5}, ON, STATE_UNAVAILABLE, ON), ({"delay_off": 5}, STATE_ON, STATE_UNAVAILABLE, STATE_ON),
({"delay_off": 5}, ON, STATE_UNKNOWN, ON), ({"delay_off": 5}, STATE_ON, STATE_UNKNOWN, STATE_ON),
({"delay_on": 5}, ON, ON, ON), ({"delay_on": 5}, STATE_ON, STATE_ON, STATE_ON),
({"delay_on": 5}, ON, OFF, OFF), ({"delay_on": 5}, STATE_ON, STATE_OFF, STATE_OFF),
({"delay_on": 5}, ON, STATE_UNAVAILABLE, STATE_UNKNOWN), ({"delay_on": 5}, STATE_ON, STATE_UNAVAILABLE, STATE_UNKNOWN),
({"delay_on": 5}, ON, STATE_UNKNOWN, STATE_UNKNOWN), ({"delay_on": 5}, STATE_ON, STATE_UNKNOWN, STATE_UNKNOWN),
], ],
) )
async def test_restore_state( async def test_restore_state(
@ -1145,7 +1142,7 @@ async def test_trigger_entity(
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.hello_name") state = hass.states.get("binary_sensor.hello_name")
assert state.state == ON assert state.state == STATE_ON
assert state.attributes.get("device_class") == "battery" assert state.attributes.get("device_class") == "battery"
assert state.attributes.get("icon") == "mdi:pirate" assert state.attributes.get("icon") == "mdi:pirate"
assert state.attributes.get("entity_picture") == "/local/dogs.png" assert state.attributes.get("entity_picture") == "/local/dogs.png"
@ -1163,7 +1160,7 @@ async def test_trigger_entity(
) )
state = hass.states.get("binary_sensor.via_list") state = hass.states.get("binary_sensor.via_list")
assert state.state == ON assert state.state == STATE_ON
assert state.attributes.get("device_class") == "battery" assert state.attributes.get("device_class") == "battery"
assert state.attributes.get("icon") == "mdi:pirate" assert state.attributes.get("icon") == "mdi:pirate"
assert state.attributes.get("entity_picture") == "/local/dogs.png" assert state.attributes.get("entity_picture") == "/local/dogs.png"
@ -1175,7 +1172,7 @@ async def test_trigger_entity(
hass.bus.async_fire("test_event", {"beer": 2, "uno_mas": "si"}) hass.bus.async_fire("test_event", {"beer": 2, "uno_mas": "si"})
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.via_list") state = hass.states.get("binary_sensor.via_list")
assert state.state == ON assert state.state == STATE_ON
assert state.attributes.get("another") == "si" assert state.attributes.get("another") == "si"
@ -1217,7 +1214,7 @@ async def test_template_with_trigger_templated_delay_on(hass: HomeAssistant) ->
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == ON assert state.state == STATE_ON
# Now wait for the auto-off # Now wait for the auto-off
future = dt_util.utcnow() + timedelta(seconds=2) future = dt_util.utcnow() + timedelta(seconds=2)
@ -1225,7 +1222,7 @@ async def test_template_with_trigger_templated_delay_on(hass: HomeAssistant) ->
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == OFF assert state.state == STATE_OFF
@pytest.mark.parametrize(("count", "domain"), [(1, "template")]) @pytest.mark.parametrize(("count", "domain"), [(1, "template")])
@ -1253,8 +1250,8 @@ async def test_template_with_trigger_templated_delay_on(hass: HomeAssistant) ->
@pytest.mark.parametrize( @pytest.mark.parametrize(
("restored_state", "initial_state", "initial_attributes"), ("restored_state", "initial_state", "initial_attributes"),
[ [
(ON, ON, ["entity_picture", "icon", "plus_one"]), (STATE_ON, STATE_ON, ["entity_picture", "icon", "plus_one"]),
(OFF, OFF, ["entity_picture", "icon", "plus_one"]), (STATE_OFF, STATE_OFF, ["entity_picture", "icon", "plus_one"]),
(STATE_UNAVAILABLE, STATE_UNKNOWN, []), (STATE_UNAVAILABLE, STATE_UNKNOWN, []),
(STATE_UNKNOWN, STATE_UNKNOWN, []), (STATE_UNKNOWN, STATE_UNKNOWN, []),
], ],
@ -1309,7 +1306,7 @@ async def test_trigger_entity_restore_state(
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == ON assert state.state == STATE_ON
assert state.attributes["icon"] == "mdi:pirate" assert state.attributes["icon"] == "mdi:pirate"
assert state.attributes["entity_picture"] == "/local/dogs.png" assert state.attributes["entity_picture"] == "/local/dogs.png"
assert state.attributes["plus_one"] == 3 assert state.attributes["plus_one"] == 3
@ -1333,7 +1330,7 @@ async def test_trigger_entity_restore_state(
}, },
], ],
) )
@pytest.mark.parametrize("restored_state", [ON, OFF]) @pytest.mark.parametrize("restored_state", [STATE_ON, STATE_OFF])
async def test_trigger_entity_restore_state_auto_off( async def test_trigger_entity_restore_state_auto_off(
hass: HomeAssistant, hass: HomeAssistant,
count, count,
@ -1377,7 +1374,7 @@ async def test_trigger_entity_restore_state_auto_off(
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == OFF assert state.state == STATE_OFF
@pytest.mark.parametrize(("count", "domain"), [(1, "template")]) @pytest.mark.parametrize(("count", "domain"), [(1, "template")])
@ -1405,7 +1402,7 @@ async def test_trigger_entity_restore_state_auto_off_expired(
freezer.move_to("2022-02-02 12:02:00+00:00") freezer.move_to("2022-02-02 12:02:00+00:00")
fake_state = State( fake_state = State(
"binary_sensor.test", "binary_sensor.test",
ON, STATE_ON,
{}, {},
) )
fake_extra_data = { fake_extra_data = {
@ -1427,7 +1424,7 @@ async def test_trigger_entity_restore_state_auto_off_expired(
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get("binary_sensor.test") state = hass.states.get("binary_sensor.test")
assert state.state == OFF assert state.state == STATE_OFF
async def test_device_id( async def test_device_id(