Allow specifying template entities based on triggers (#48169)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
f815ebe9cd
commit
022f56f54d
6 changed files with 456 additions and 16 deletions
|
@ -15,8 +15,10 @@ from homeassistant.const import (
|
|||
STATE_OFF,
|
||||
STATE_ON,
|
||||
STATE_UNAVAILABLE,
|
||||
STATE_UNKNOWN,
|
||||
)
|
||||
from homeassistant.core import CoreState, callback
|
||||
from homeassistant.core import Context, CoreState, callback
|
||||
from homeassistant.helpers import entity_registry
|
||||
from homeassistant.helpers.template import Template
|
||||
from homeassistant.setup import ATTR_COMPONENT, async_setup_component
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
@ -986,3 +988,126 @@ async def test_duplicate_templates(hass):
|
|||
state = hass.states.get("sensor.test_template_sensor")
|
||||
assert state.attributes["friendly_name"] == "Def"
|
||||
assert state.state == "Def"
|
||||
|
||||
|
||||
async def test_trigger_entity(hass):
|
||||
"""Test trigger entity works."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"template",
|
||||
{
|
||||
"template": [
|
||||
{"invalid": "config"},
|
||||
# This one should still be set up
|
||||
{
|
||||
"unique_id": "listening-test-event",
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"sensors": {
|
||||
"hello": {
|
||||
"friendly_name": "Hello Name",
|
||||
"unique_id": "just_a_test",
|
||||
"device_class": "battery",
|
||||
"unit_of_measurement": "%",
|
||||
"value_template": "{{ trigger.event.data.beer }}",
|
||||
"entity_picture_template": "{{ '/local/dogs.png' }}",
|
||||
"icon_template": "{{ 'mdi:pirate' }}",
|
||||
"attribute_templates": {
|
||||
"plus_one": "{{ trigger.event.data.beer + 1 }}"
|
||||
},
|
||||
}
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.hello")
|
||||
assert state is not None
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
context = Context()
|
||||
hass.bus.async_fire("test_event", {"beer": 2}, context=context)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.hello")
|
||||
assert state.state == "2"
|
||||
assert state.attributes.get("device_class") == "battery"
|
||||
assert state.attributes.get("icon") == "mdi:pirate"
|
||||
assert state.attributes.get("entity_picture") == "/local/dogs.png"
|
||||
assert state.attributes.get("plus_one") == 3
|
||||
assert state.attributes.get("unit_of_measurement") == "%"
|
||||
assert state.context is context
|
||||
|
||||
ent_reg = entity_registry.async_get(hass)
|
||||
assert len(ent_reg.entities) == 1
|
||||
assert (
|
||||
ent_reg.entities["sensor.hello"].unique_id == "listening-test-event-just_a_test"
|
||||
)
|
||||
|
||||
|
||||
async def test_trigger_entity_render_error(hass):
|
||||
"""Test trigger entity handles render error."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"template",
|
||||
{
|
||||
"template": {
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"sensors": {
|
||||
"hello": {
|
||||
"unique_id": "no-base-id",
|
||||
"friendly_name": "Hello",
|
||||
"value_template": "{{ non_existing + 1 }}",
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.hello")
|
||||
assert state is not None
|
||||
assert state.state == STATE_UNKNOWN
|
||||
|
||||
context = Context()
|
||||
hass.bus.async_fire("test_event", {"beer": 2}, context=context)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.hello")
|
||||
assert state.state == STATE_UNAVAILABLE
|
||||
|
||||
ent_reg = entity_registry.async_get(hass)
|
||||
assert len(ent_reg.entities) == 1
|
||||
assert ent_reg.entities["sensor.hello"].unique_id == "no-base-id"
|
||||
|
||||
|
||||
async def test_trigger_not_allowed_platform_config(hass, caplog):
|
||||
"""Test we throw a helpful warning if a trigger is configured in platform config."""
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
sensor.DOMAIN,
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "template",
|
||||
"trigger": {"platform": "event", "event_type": "test_event"},
|
||||
"sensors": {
|
||||
"test_template_sensor": {
|
||||
"value_template": "{{ states.sensor.test_state.state }}",
|
||||
"friendly_name_template": "{{ states.sensor.test_state.state }}",
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
await hass.async_block_till_done()
|
||||
|
||||
state = hass.states.get("sensor.test_template_sensor")
|
||||
assert state is None
|
||||
assert (
|
||||
"You can only add triggers to template entities if they are defined under `template:`."
|
||||
in caplog.text
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue