Clean test

This commit is contained in:
G Johansson 2025-01-13 18:26:36 +00:00
parent 3337dd4ed7
commit 1c5eb92c9c

View File

@ -4,19 +4,24 @@ from typing import Any
import pytest import pytest
from homeassistant.const import CONF_ICON, CONF_NAME, STATE_OFF, STATE_ON, STATE_UNKNOWN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers import template from homeassistant.helpers import template
from homeassistant.helpers.trigger_template_entity import ManualTriggerEntity from homeassistant.helpers.trigger_template_entity import (
CONF_AVAILABILITY,
CONF_PICTURE,
ManualTriggerEntity,
)
async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None: async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None:
"""Test manual trigger template entity.""" """Test manual trigger template entity."""
config = { config = {
"name": template.Template("test_entity", hass), CONF_NAME: template.Template("test_entity", hass),
"icon": template.Template( CONF_ICON: template.Template(
'{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass '{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass
), ),
"picture": template.Template( CONF_PICTURE: template.Template(
'{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}', '{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}',
hass, hass,
), ),
@ -24,19 +29,19 @@ async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None:
entity = ManualTriggerEntity(hass, config) entity = ManualTriggerEntity(hass, config)
entity.entity_id = "test.entity" entity.entity_id = "test.entity"
hass.states.async_set("test.entity", "on") hass.states.async_set("test.entity", STATE_ON)
await entity.async_added_to_hass() await entity.async_added_to_hass()
entity._process_manual_data("on") entity._process_manual_data(STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entity.name == "test_entity" assert entity.name == "test_entity"
assert entity.icon == "mdi:on" assert entity.icon == "mdi:on"
assert entity.entity_picture == "/local/picture_on" assert entity.entity_picture == "/local/picture_on"
hass.states.async_set("test.entity", "off") hass.states.async_set("test.entity", STATE_OFF)
await entity.async_added_to_hass() await entity.async_added_to_hass()
entity._process_manual_data("off") entity._process_manual_data(STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entity.name == "test_entity" assert entity.name == "test_entity"
@ -47,23 +52,23 @@ async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None:
async def test_trigger_template_availability(hass: HomeAssistant) -> None: async def test_trigger_template_availability(hass: HomeAssistant) -> None:
"""Test manual trigger template entity availability template.""" """Test manual trigger template entity availability template."""
config = { config = {
"name": template.Template("test_entity", hass), CONF_NAME: template.Template("test_entity", hass),
"icon": template.Template( CONF_ICON: template.Template(
'{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass '{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass
), ),
"picture": template.Template( CONF_PICTURE: template.Template(
'{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}', '{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}',
hass, hass,
), ),
"availability": template.Template('{{ has_value("test.entity") }}', hass), CONF_AVAILABILITY: template.Template('{{ has_value("test.entity") }}', hass),
} }
entity = ManualTriggerEntity(hass, config) entity = ManualTriggerEntity(hass, config)
entity.entity_id = "test.entity" entity.entity_id = "test.entity"
hass.states.async_set("test.entity", "on") hass.states.async_set("test.entity", STATE_ON)
await entity.async_added_to_hass() await entity.async_added_to_hass()
entity._process_manual_data("on") entity._process_manual_data(STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entity.name == "test_entity" assert entity.name == "test_entity"
@ -71,9 +76,9 @@ async def test_trigger_template_availability(hass: HomeAssistant) -> None:
assert entity.entity_picture == "/local/picture_on" assert entity.entity_picture == "/local/picture_on"
assert entity.available is True assert entity.available is True
hass.states.async_set("test.entity", "off") hass.states.async_set("test.entity", STATE_OFF)
await entity.async_added_to_hass() await entity.async_added_to_hass()
entity._process_manual_data("off") entity._process_manual_data(STATE_OFF)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entity.name == "test_entity" assert entity.name == "test_entity"
@ -81,9 +86,9 @@ async def test_trigger_template_availability(hass: HomeAssistant) -> None:
assert entity.entity_picture == "/local/picture_off" assert entity.entity_picture == "/local/picture_off"
assert entity.available is True assert entity.available is True
hass.states.async_set("test.entity", "unknown") hass.states.async_set("test.entity", STATE_UNKNOWN)
await entity.async_added_to_hass() await entity.async_added_to_hass()
entity._process_manual_data("unknown") entity._process_manual_data(STATE_UNKNOWN)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entity.name == "test_entity" assert entity.name == "test_entity"
@ -97,23 +102,23 @@ async def test_trigger_template_availability_fails(
) -> None: ) -> None:
"""Test manual trigger template entity when availability render fails.""" """Test manual trigger template entity when availability render fails."""
config = { config = {
"name": template.Template("test_entity", hass), CONF_NAME: template.Template("test_entity", hass),
"icon": template.Template( CONF_ICON: template.Template(
'{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass '{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass
), ),
"picture": template.Template( CONF_PICTURE: template.Template(
'{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}', '{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}',
hass, hass,
), ),
"availability": template.Template("{{ incorrect ", hass), CONF_AVAILABILITY: template.Template("{{ incorrect ", hass),
} }
entity = ManualTriggerEntity(hass, config) entity = ManualTriggerEntity(hass, config)
entity.entity_id = "test.entity" entity.entity_id = "test.entity"
hass.states.async_set("test.entity", "on") hass.states.async_set("test.entity", STATE_ON)
await entity.async_added_to_hass() await entity.async_added_to_hass()
entity._process_manual_data("on") entity._process_manual_data(STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert "Error rendering availability template for test.entity" in caplog.text assert "Error rendering availability template for test.entity" in caplog.text
@ -127,15 +132,15 @@ async def test_trigger_template_complex(hass: HomeAssistant) -> None:
""" """
config = { config = {
"name": template.Template("test_entity", hass), CONF_NAME: template.Template("test_entity", hass),
"icon": template.Template( CONF_ICON: template.Template(
'{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass '{% if value=="on" %} mdi:on {% else %} mdi:off {% endif %}', hass
), ),
"picture": template.Template( CONF_PICTURE: template.Template(
'{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}', '{% if value=="on" %} /local/picture_on {% else %} /local/picture_off {% endif %}',
hass, hass,
), ),
"availability": template.Template('{{ has_value("test.entity") }}', hass), CONF_AVAILABILITY: template.Template('{{ has_value("test.entity") }}', hass),
"other_key": template.Template(complex_template, hass), "other_key": template.Template(complex_template, hass),
} }
@ -151,10 +156,10 @@ async def test_trigger_template_complex(hass: HomeAssistant) -> None:
entity = TestEntity(hass, config) entity = TestEntity(hass, config)
entity.entity_id = "test.entity" entity.entity_id = "test.entity"
hass.states.async_set("test.entity", "on") hass.states.async_set("test.entity", STATE_ON)
await entity.async_added_to_hass() await entity.async_added_to_hass()
entity._process_manual_data("on") entity._process_manual_data(STATE_ON)
await hass.async_block_till_done() await hass.async_block_till_done()
assert entity.some_other_key == {"test_key": "test_data"} assert entity.some_other_key == {"test_key": "test_data"}