Add condition to trigger template entities (#119689)

* Add conditions to trigger template entities

* Add tests

* Fix ruff error

* Ruff

* Apply suggestions from code review

* Deduplicate

* Tweak name used in debug message

* Add and improve type annotations of modified code

* Adjust typing

* Adjust typing

* Add typing and remove unused parameter

* Adjust typing

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Adjust return type

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

---------

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
chammp 2024-09-11 09:36:49 +02:00 committed by GitHub
parent 74834b2d88
commit b3377fe5fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 265 additions and 49 deletions

View file

@ -1207,6 +1207,124 @@ async def test_trigger_entity(
assert state.context is context
@pytest.mark.parametrize(("count", "domain"), [(1, template.DOMAIN)])
@pytest.mark.parametrize(
"config",
[
{
"template": [
{
"unique_id": "listening-test-event",
"trigger": {"platform": "event", "event_type": "test_event"},
"condition": [
{
"condition": "template",
"value_template": "{{ trigger.event.data.beer >= 42 }}",
}
],
"sensor": [
{
"name": "Enough Name",
"unique_id": "enough-id",
"state": "You had enough Beer.",
}
],
},
],
},
],
)
async def test_trigger_conditional_entity(hass: HomeAssistant, start_ha) -> None:
"""Test conditional trigger entity works."""
state = hass.states.get("sensor.enough_name")
assert state is not None
assert state.state == STATE_UNKNOWN
hass.bus.async_fire("test_event", {"beer": 2})
await hass.async_block_till_done()
state = hass.states.get("sensor.enough_name")
assert state.state == STATE_UNKNOWN
hass.bus.async_fire("test_event", {"beer": 42})
await hass.async_block_till_done()
state = hass.states.get("sensor.enough_name")
assert state.state == "You had enough Beer."
@pytest.mark.parametrize(("count", "domain"), [(1, template.DOMAIN)])
@pytest.mark.parametrize(
"config",
[
{
"template": [
{
"unique_id": "listening-test-event",
"trigger": {"platform": "event", "event_type": "test_event"},
"condition": [
{
"condition": "template",
"value_template": "{{ trigger.event.data.beer / 0 == 'narf' }}",
}
],
"sensor": [
{
"name": "Enough Name",
"unique_id": "enough-id",
"state": "You had enough Beer.",
}
],
},
],
},
],
)
async def test_trigger_conditional_entity_evaluation_error(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture, start_ha
) -> None:
"""Test trigger entity is not updated when condition evaluation fails."""
hass.bus.async_fire("test_event", {"beer": 1})
await hass.async_block_till_done()
state = hass.states.get("sensor.enough_name")
assert state is not None
assert state.state == STATE_UNKNOWN
assert "Error evaluating condition in 'template entity'" in caplog.text
@pytest.mark.parametrize(("count", "domain"), [(0, template.DOMAIN)])
@pytest.mark.parametrize(
"config",
[
{
"template": [
{
"unique_id": "listening-test-event",
"trigger": {"platform": "event", "event_type": "test_event"},
"condition": [
{"condition": "template", "value_template": "{{ invalid"}
],
"sensor": [
{
"name": "Will Not Exist Name",
"state": "Unimportant",
}
],
},
],
},
],
)
async def test_trigger_conditional_entity_invalid_condition(
hass: HomeAssistant, start_ha
) -> None:
"""Test trigger entity is not created when condition is invalid."""
state = hass.states.get("sensor.will_not_exist_name")
assert state is None
@pytest.mark.parametrize(("count", "domain"), [(1, "template")])
@pytest.mark.parametrize(
"config",
@ -1903,6 +2021,52 @@ async def test_trigger_action(
assert events[0].context.parent_id == context.id
@pytest.mark.parametrize(("count", "domain"), [(1, template.DOMAIN)])
@pytest.mark.parametrize(
"config",
[
{
"template": [
{
"unique_id": "listening-test-event",
"trigger": {"platform": "event", "event_type": "test_event"},
"condition": [
{
"condition": "template",
"value_template": "{{ trigger.event.data.beer >= 42 }}",
}
],
"action": [
{"event": "test_event_by_action"},
],
"sensor": [
{
"name": "Not That Important",
"state": "Really not.",
}
],
},
],
},
],
)
async def test_trigger_conditional_action(hass: HomeAssistant, start_ha) -> None:
"""Test conditional trigger entity with an action works."""
event = "test_event_by_action"
events = async_capture_events(hass, event)
hass.bus.async_fire("test_event", {"beer": 1})
await hass.async_block_till_done()
assert len(events) == 0
hass.bus.async_fire("test_event", {"beer": 42})
await hass.async_block_till_done()
assert len(events) == 1
async def test_device_id(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,