From 35097602d77e4d7813af80353398a5898da2a12f Mon Sep 17 00:00:00 2001 From: Petro31 <35082313+Petro31@users.noreply.github.com> Date: Tue, 15 Jul 2025 11:04:31 -0400 Subject: [PATCH] Remove unnecessary hass if check in AbstractTemplateEntity (#148828) --- homeassistant/components/template/entity.py | 22 +++++++++---------- tests/components/template/test_entity.py | 8 ++----- .../template/test_template_entity.py | 6 +---- 3 files changed, 14 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/template/entity.py b/homeassistant/components/template/entity.py index a97a5ac6571..481db182713 100644 --- a/homeassistant/components/template/entity.py +++ b/homeassistant/components/template/entity.py @@ -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, diff --git a/tests/components/template/test_entity.py b/tests/components/template/test_entity.py index 4a6940c2813..8e98d8c94a7 100644 --- a/tests/components/template/test_entity.py +++ b/tests/components/template/test_entity.py @@ -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, {}) diff --git a/tests/components/template/test_template_entity.py b/tests/components/template/test_template_entity.py index b743f7e2d9f..7fe3870ae1e 100644 --- a/tests/components/template/test_template_entity.py +++ b/tests/components/template/test_template_entity.py @@ -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))