Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 1 addition & 65 deletions homeassistant/helpers/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from operator import attrgetter
import random
import re
from typing import Any, Dict, Generator, Iterable, List, Optional, Type, Union
from typing import Any, Dict, Generator, Iterable, Optional, Type, Union
from urllib.parse import urlencode as urllib_urlencode
import weakref

Expand All @@ -27,13 +27,11 @@
ATTR_LONGITUDE,
ATTR_UNIT_OF_MEASUREMENT,
LENGTH_METERS,
MATCH_ALL,
STATE_UNKNOWN,
)
from homeassistant.core import State, callback, split_entity_id, valid_entity_id
from homeassistant.exceptions import TemplateError
from homeassistant.helpers import config_validation as cv, location as loc_helper
from homeassistant.helpers.frame import report
from homeassistant.helpers.typing import HomeAssistantType, TemplateVarsType
from homeassistant.loader import bind_hass
from homeassistant.util import convert, dt as dt_util, location as loc_util
Expand Down Expand Up @@ -184,59 +182,6 @@ def __str__(self) -> str:
RESULT_WRAPPERS[tuple] = TupleWrapper


def extract_entities(
hass: HomeAssistantType,
template: Optional[str],
variables: TemplateVarsType = None,
) -> Union[str, List[str]]:
"""Extract all entities for state_changed listener from template string."""

report(
"called template.extract_entities. Please use event.async_track_template_result instead as it can accurately handle watching entities"
)

if template is None or not is_template_string(template):
return []

if _RE_NONE_ENTITIES.search(template):
return MATCH_ALL

extraction_final = []

for result in _RE_GET_ENTITIES.finditer(template):
if (
result.group("entity_id") == "trigger.entity_id"
and variables
and "trigger" in variables
and "entity_id" in variables["trigger"]
):
extraction_final.append(variables["trigger"]["entity_id"])
elif result.group("entity_id"):
if result.group("func") == "expand":
for entity in expand(hass, result.group("entity_id")):
extraction_final.append(entity.entity_id)

extraction_final.append(result.group("entity_id"))
elif result.group("domain_inner") or result.group("domain_outer"):
extraction_final.extend(
hass.states.async_entity_ids(
result.group("domain_inner") or result.group("domain_outer")
)
)

if (
variables
and result.group("variable") in variables
and isinstance(variables[result.group("variable")], str)
and valid_entity_id(variables[result.group("variable")])
):
extraction_final.append(variables[result.group("variable")])

if extraction_final:
return list(set(extraction_final))
return MATCH_ALL


def _true(arg: Any) -> bool:
return True

Expand Down Expand Up @@ -370,15 +315,6 @@ def ensure_valid(self):
except jinja2.TemplateError as err:
raise TemplateError(err) from err

def extract_entities(
self, variables: TemplateVarsType = None
) -> Union[str, List[str]]:
"""Extract all entities for state_changed listener."""
if self.is_static:
return []

return extract_entities(self.hass, self.template, variables)

def render(
self,
variables: TemplateVarsType = None,
Expand Down
Loading