core/tests/components/template/test_template_entity.py
Petro31 890d3f4af4
Add a base class for template entities to inherit from (#139645)
* add-abstract-template-entity-base-class

* review 1 changes
2025-03-04 07:23:05 +01:00

25 lines
924 B
Python

"""Test template entity."""
import pytest
from homeassistant.components.template import template_entity
from homeassistant.core import HomeAssistant
from homeassistant.helpers import template
async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None:
"""Test template entity requires hass to be set before accepting templates."""
entity = template_entity.TemplateEntity(None)
with pytest.raises(ValueError, match="^hass cannot be None"):
entity.add_template_attribute("_hello", template.Template("Hello"))
entity.hass = object()
with pytest.raises(ValueError, match="^template.hass cannot be None"):
entity.add_template_attribute("_hello", template.Template("Hello", None))
tpl_with_hass = template.Template("Hello", entity.hass)
entity.add_template_attribute("_hello", tpl_with_hass)
assert len(entity._template_attrs.get(tpl_with_hass, [])) == 1