Remove unnecessary hass if check in AbstractTemplateEntity (#148828)

This commit is contained in:
Petro31 2025-07-15 11:04:31 -04:00 committed by GitHub
parent e5fe243a86
commit 35097602d7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 22 deletions

View File

@ -1,5 +1,6 @@
"""Template entity base class."""
from abc import abstractmethod
from collections.abc import Sequence
from typing import Any
@ -25,26 +26,25 @@ class AbstractTemplateEntity(Entity):
self.hass = hass
self._action_scripts: dict[str, Script] = {}
if self.hass:
if (object_id := config.get(CONF_OBJECT_ID)) is not None:
self.entity_id = async_generate_entity_id(
self._entity_id_format, object_id, hass=self.hass
)
self._attr_device_info = async_device_info_to_link_from_device_id(
self.hass,
config.get(CONF_DEVICE_ID),
if (object_id := config.get(CONF_OBJECT_ID)) is not None:
self.entity_id = async_generate_entity_id(
self._entity_id_format, object_id, hass=self.hass
)
self._attr_device_info = async_device_info_to_link_from_device_id(
self.hass,
config.get(CONF_DEVICE_ID),
)
@property
@abstractmethod
def referenced_blueprint(self) -> str | None:
"""Return referenced blueprint or None."""
raise NotImplementedError
@callback
@abstractmethod
def _render_script_variables(self) -> dict:
"""Render configured variables."""
raise NotImplementedError
def add_script(
self,

View File

@ -9,9 +9,5 @@ from homeassistant.core import HomeAssistant
async def test_template_entity_not_implemented(hass: HomeAssistant) -> None:
"""Test abstract template entity raises not implemented error."""
entity = abstract_entity.AbstractTemplateEntity(None, {})
with pytest.raises(NotImplementedError):
_ = entity.referenced_blueprint
with pytest.raises(NotImplementedError):
entity._render_script_variables()
with pytest.raises(TypeError):
_ = abstract_entity.AbstractTemplateEntity(hass, {})

View File

@ -9,12 +9,8 @@ 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, {}, "something_unique")
entity = template_entity.TemplateEntity(hass, {}, "something_unique")
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))