Skip to content

Commit 3509760

Browse files
authored
Remove unnecessary hass if check in AbstractTemplateEntity (home-assistant#148828)
1 parent e5fe243 commit 3509760

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

homeassistant/components/template/entity.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Template entity base class."""
22

3+
from abc import abstractmethod
34
from collections.abc import Sequence
45
from typing import Any
56

@@ -25,26 +26,25 @@ def __init__(self, hass: HomeAssistant, config: ConfigType) -> None:
2526
self.hass = hass
2627
self._action_scripts: dict[str, Script] = {}
2728

28-
if self.hass:
29-
if (object_id := config.get(CONF_OBJECT_ID)) is not None:
30-
self.entity_id = async_generate_entity_id(
31-
self._entity_id_format, object_id, hass=self.hass
32-
)
33-
34-
self._attr_device_info = async_device_info_to_link_from_device_id(
35-
self.hass,
36-
config.get(CONF_DEVICE_ID),
29+
if (object_id := config.get(CONF_OBJECT_ID)) is not None:
30+
self.entity_id = async_generate_entity_id(
31+
self._entity_id_format, object_id, hass=self.hass
3732
)
3833

34+
self._attr_device_info = async_device_info_to_link_from_device_id(
35+
self.hass,
36+
config.get(CONF_DEVICE_ID),
37+
)
38+
3939
@property
40+
@abstractmethod
4041
def referenced_blueprint(self) -> str | None:
4142
"""Return referenced blueprint or None."""
42-
raise NotImplementedError
4343

4444
@callback
45+
@abstractmethod
4546
def _render_script_variables(self) -> dict:
4647
"""Render configured variables."""
47-
raise NotImplementedError
4848

4949
def add_script(
5050
self,

tests/components/template/test_entity.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,5 @@
99
async def test_template_entity_not_implemented(hass: HomeAssistant) -> None:
1010
"""Test abstract template entity raises not implemented error."""
1111

12-
entity = abstract_entity.AbstractTemplateEntity(None, {})
13-
with pytest.raises(NotImplementedError):
14-
_ = entity.referenced_blueprint
15-
16-
with pytest.raises(NotImplementedError):
17-
entity._render_script_variables()
12+
with pytest.raises(TypeError):
13+
_ = abstract_entity.AbstractTemplateEntity(hass, {})

tests/components/template/test_template_entity.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99

1010
async def test_template_entity_requires_hass_set(hass: HomeAssistant) -> None:
1111
"""Test template entity requires hass to be set before accepting templates."""
12-
entity = template_entity.TemplateEntity(None, {}, "something_unique")
12+
entity = template_entity.TemplateEntity(hass, {}, "something_unique")
1313

14-
with pytest.raises(ValueError, match="^hass cannot be None"):
15-
entity.add_template_attribute("_hello", template.Template("Hello"))
16-
17-
entity.hass = object()
1814
with pytest.raises(ValueError, match="^template.hass cannot be None"):
1915
entity.add_template_attribute("_hello", template.Template("Hello", None))
2016

0 commit comments

Comments
 (0)