mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Remove unnecessary hass if check in AbstractTemplateEntity (#148828)
This commit is contained in:
parent
e5fe243a86
commit
35097602d7
@ -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,
|
||||
|
@ -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, {})
|
||||
|
@ -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))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user