core/tests/components/template/test_trigger_entity.py
Petro31 ff2c901930
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>
2025-04-25 13:17:25 +02:00

121 lines
4.1 KiB
Python

"""Test trigger template entity."""
import pytest
from homeassistant.components.template import trigger_entity
from homeassistant.components.template.coordinator import TriggerUpdateCoordinator
from homeassistant.const import CONF_ICON, CONF_NAME, CONF_STATE, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers import template
from homeassistant.helpers.trigger_template_entity import CONF_PICTURE
_ICON_TEMPLATE = 'mdi:o{{ "n" if value=="on" else "ff" }}'
_PICTURE_TEMPLATE = '/local/picture_o{{ "n" if value=="on" else "ff" }}'
class TestEntity(trigger_entity.TriggerEntity):
"""Test entity class."""
__test__ = False
extra_template_keys = (CONF_STATE,)
@property
def state(self) -> bool | None:
"""Return extra attributes."""
return self._rendered.get(CONF_STATE)
async def test_reference_blueprints_is_none(hass: HomeAssistant) -> None:
"""Test template entity requires hass to be set before accepting templates."""
coordinator = TriggerUpdateCoordinator(hass, {})
entity = trigger_entity.TriggerEntity(hass, coordinator, {})
assert entity.referenced_blueprint is None
async def test_template_state(hass: HomeAssistant) -> None:
"""Test manual trigger template entity with a state."""
config = {
CONF_NAME: template.Template("test_entity", hass),
CONF_ICON: template.Template(_ICON_TEMPLATE, hass),
CONF_PICTURE: template.Template(_PICTURE_TEMPLATE, hass),
CONF_STATE: template.Template("{{ value == 'on' }}", hass),
}
coordinator = TriggerUpdateCoordinator(hass, {})
entity = TestEntity(hass, coordinator, config)
entity.entity_id = "test.entity"
coordinator._execute_update({"value": STATE_ON})
entity._handle_coordinator_update()
await hass.async_block_till_done()
assert entity.state == "True"
assert entity.icon == "mdi:on"
assert entity.entity_picture == "/local/picture_on"
coordinator._execute_update({"value": STATE_OFF})
entity._handle_coordinator_update()
await hass.async_block_till_done()
assert entity.state == "False"
assert entity.icon == "mdi:off"
assert entity.entity_picture == "/local/picture_off"
async def test_bad_template_state(hass: HomeAssistant) -> None:
"""Test manual trigger template entity with a state."""
config = {
CONF_NAME: template.Template("test_entity", hass),
CONF_ICON: template.Template(_ICON_TEMPLATE, hass),
CONF_PICTURE: template.Template(_PICTURE_TEMPLATE, hass),
CONF_STATE: template.Template("{{ x - 1 }}", hass),
}
coordinator = TriggerUpdateCoordinator(hass, {})
entity = TestEntity(hass, coordinator, config)
entity.entity_id = "test.entity"
coordinator._execute_update({"x": 1})
entity._handle_coordinator_update()
await hass.async_block_till_done()
assert entity.available is True
assert entity.state == "0"
assert entity.icon == "mdi:off"
assert entity.entity_picture == "/local/picture_off"
coordinator._execute_update({"value": STATE_OFF})
entity._handle_coordinator_update()
await hass.async_block_till_done()
assert entity.available is False
assert entity.state is None
assert entity.icon is None
assert entity.entity_picture is None
async def test_template_state_syntax_error(
hass: HomeAssistant, caplog: pytest.LogCaptureFixture
) -> None:
"""Test manual trigger template entity when state render fails."""
config = {
CONF_NAME: template.Template("test_entity", hass),
CONF_ICON: template.Template(_ICON_TEMPLATE, hass),
CONF_PICTURE: template.Template(_PICTURE_TEMPLATE, hass),
CONF_STATE: template.Template("{{ incorrect ", hass),
}
coordinator = TriggerUpdateCoordinator(hass, {})
entity = TestEntity(hass, coordinator, config)
entity.entity_id = "test.entity"
coordinator._execute_update({"value": STATE_ON})
entity._handle_coordinator_update()
await hass.async_block_till_done()
assert f"Error rendering {CONF_STATE} template for test.entity" in caplog.text
assert entity.state is None
assert entity.icon is None
assert entity.entity_picture is None