2021-04-11 22:49:09 -07:00
|
|
|
"""Test template entity."""
|
2024-03-08 19:16:21 +01:00
|
|
|
|
2021-04-11 22:49:09 -07:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from homeassistant.components.template import template_entity
|
2023-02-08 19:10:53 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-04-11 22:49:09 -07:00
|
|
|
from homeassistant.helpers import template
|
|
|
|
|
|
|
|
|
|
|
2023-02-08 19:10:53 +01:00
|
|
|
async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None:
|
2021-04-11 22:49:09 -07:00
|
|
|
"""Test template entity requires hass to be set before accepting templates."""
|
2025-07-15 11:04:31 -04:00
|
|
|
entity = template_entity.TemplateEntity(hass, {}, "something_unique")
|
2021-04-11 22:49:09 -07:00
|
|
|
|
2024-08-13 11:54:36 +02:00
|
|
|
with pytest.raises(ValueError, match="^template.hass cannot be None"):
|
|
|
|
|
entity.add_template_attribute("_hello", template.Template("Hello", None))
|
2021-04-11 22:49:09 -07:00
|
|
|
|
|
|
|
|
tpl_with_hass = template.Template("Hello", entity.hass)
|
|
|
|
|
entity.add_template_attribute("_hello", tpl_with_hass)
|
|
|
|
|
|
2024-08-13 11:54:36 +02:00
|
|
|
assert len(entity._template_attrs.get(tpl_with_hass, [])) == 1
|
2025-08-27 04:49:38 -04:00
|
|
|
|
|
|
|
|
|
2025-08-27 12:06:03 -04:00
|
|
|
async def test_default_entity_id(hass: HomeAssistant) -> None:
|
|
|
|
|
"""Test template entity creates suggested entity_id from the default_entity_id."""
|
2025-08-27 04:49:38 -04:00
|
|
|
|
|
|
|
|
class TemplateTest(template_entity.TemplateEntity):
|
|
|
|
|
_entity_id_format = "test.{}"
|
|
|
|
|
|
2025-08-27 12:06:03 -04:00
|
|
|
entity = TemplateTest(hass, {"default_entity_id": "test.test"}, "a")
|
|
|
|
|
assert entity.entity_id == "test.test"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def test_bad_default_entity_id(hass: HomeAssistant) -> None:
|
|
|
|
|
"""Test template entity creates suggested entity_id from the default_entity_id."""
|
|
|
|
|
|
|
|
|
|
class TemplateTest(template_entity.TemplateEntity):
|
|
|
|
|
_entity_id_format = "test.{}"
|
|
|
|
|
|
|
|
|
|
entity = TemplateTest(hass, {"default_entity_id": "bad.test"}, "a")
|
2025-08-27 04:49:38 -04:00
|
|
|
assert entity.entity_id == "test.test"
|