1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Update trigger based template entity resolution order (#140660)

* Update trigger based template entity resolution order

* add test

* fix most comments

* Move resolution to base class

* add comment

* remove uncessary if statement

* add more tests

* update availability tests

* update logic stage 1

* phase 2 changes

* fix trigger template entity tests

* fix trigger template entities

* command line tests

* sql tests

* scrape test

* update doc string

* add rest tests

* update sql sensor _update signature

* fix scrape test constructor

* move state check to trigger_entity

* fix comments

* Update homeassistant/components/template/trigger_entity.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/helpers/trigger_template_entity.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* Update homeassistant/helpers/trigger_template_entity.py

Co-authored-by: Erik Montnemery <erik@montnemery.com>

* update command_line and rest

* update scrape

* update sql

* add case to command_line sensor

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
Petro31
2025-04-25 07:17:25 -04:00
committed by GitHub
parent dc8e1773f1
commit ff2c901930
28 changed files with 1892 additions and 185 deletions

View File

@@ -735,7 +735,9 @@ async def test_updating_manually(
"command_on": "echo 2",
"command_off": "echo 3",
"name": "Test",
"availability": '{{ states("sensor.input1")=="on" }}',
"value_template": "{{ value_json == 0 }}",
"availability": '{{ "sensor.input1" | has_value }}',
"icon": 'mdi:{{ states("sensor.input1") }}',
},
}
]
@@ -749,16 +751,17 @@ async def test_availability(
) -> None:
"""Test availability."""
hass.states.async_set("sensor.input1", "on")
hass.states.async_set("sensor.input1", STATE_OFF)
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
entity_state = hass.states.get("switch.test")
assert entity_state
assert entity_state.state == STATE_ON
assert entity_state.state == STATE_OFF
assert entity_state.attributes["icon"] == "mdi:off"
hass.states.async_set("sensor.input1", "off")
hass.states.async_set("sensor.input1", STATE_UNAVAILABLE)
await hass.async_block_till_done()
with mock_asyncio_subprocess_run(b"50\n"):
freezer.tick(timedelta(minutes=1))
@@ -768,3 +771,64 @@ async def test_availability(
entity_state = hass.states.get("switch.test")
assert entity_state
assert entity_state.state == STATE_UNAVAILABLE
assert "icon" not in entity_state.attributes
hass.states.async_set("sensor.input1", STATE_ON)
await hass.async_block_till_done()
with mock_asyncio_subprocess_run(b"0\n"):
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
entity_state = hass.states.get("switch.test")
assert entity_state
assert entity_state.state == STATE_ON
assert entity_state.attributes["icon"] == "mdi:on"
@pytest.mark.parametrize(
"get_config",
[
{
"command_line": [
{
"switch": {
"command_state": "echo 1",
"command_on": "echo 2",
"command_off": "echo 3",
"name": "Test",
"value_template": "{{ x - 1 }}",
"availability": "{{ value == '50' }}",
},
}
]
}
],
)
async def test_availability_blocks_value_template(
hass: HomeAssistant,
load_yaml_integration: None,
freezer: FrozenDateTimeFactory,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test availability blocks value_template from rendering."""
error = "Error parsing value for switch.test: 'x' is undefined"
await hass.async_block_till_done()
with mock_asyncio_subprocess_run(b"51\n"):
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert error not in caplog.text
entity_state = hass.states.get("switch.test")
assert entity_state
assert entity_state.state == STATE_UNAVAILABLE
await hass.async_block_till_done()
with mock_asyncio_subprocess_run(b"50\n"):
freezer.tick(timedelta(minutes=1))
async_fire_time_changed(hass)
await hass.async_block_till_done(wait_background_tasks=True)
assert error in caplog.text