mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Support extracting entities by domain from templates (#38647)
This commit is contained in:
parent
d659502e35
commit
ef8e74786f
@ -46,8 +46,7 @@ _ENVIRONMENT = "template.environment"
|
|||||||
|
|
||||||
_RE_NONE_ENTITIES = re.compile(r"distance\(|closest\(", re.I | re.M)
|
_RE_NONE_ENTITIES = re.compile(r"distance\(|closest\(", re.I | re.M)
|
||||||
_RE_GET_ENTITIES = re.compile(
|
_RE_GET_ENTITIES = re.compile(
|
||||||
r"(?:(?:states\.|(?P<func>is_state|is_state_attr|state_attr|states|expand)"
|
r"(?:(?:(?:states\.|(?P<func>is_state|is_state_attr|state_attr|states|expand)\((?:[\ \'\"]?))(?P<entity_id>[\w]+\.[\w]+)|states\.(?P<domain_outer>[a-z]+)|states\[(?:[\'\"]?)(?P<domain_inner>[\w]+))|(?P<variable>[\w]+))",
|
||||||
r"\((?:[\ \'\"]?))(?P<entity_id>[\w]+\.[\w]+)|(?P<variable>[\w]+))",
|
|
||||||
re.I | re.M,
|
re.I | re.M,
|
||||||
)
|
)
|
||||||
_RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{")
|
_RE_JINJA_DELIMITERS = re.compile(r"\{%|\{\{")
|
||||||
@ -105,6 +104,12 @@ def extract_entities(
|
|||||||
extraction_final.append(entity.entity_id)
|
extraction_final.append(entity.entity_id)
|
||||||
|
|
||||||
extraction_final.append(result.group("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 (
|
if (
|
||||||
variables
|
variables
|
||||||
|
@ -1812,6 +1812,53 @@ def test_extract_entities_with_variables(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_extract_entities_domain_states_inner(hass):
|
||||||
|
"""Test extract entities function by domain."""
|
||||||
|
hass.states.async_set("light.switch", "on")
|
||||||
|
hass.states.async_set("light.switch2", "on")
|
||||||
|
hass.states.async_set("light.switch3", "off")
|
||||||
|
|
||||||
|
assert set(
|
||||||
|
template.extract_entities(
|
||||||
|
hass,
|
||||||
|
"{{ states['light'] | selectattr('state','eq','on') | list | count > 0 }}",
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
) == {"light.switch", "light.switch2", "light.switch3"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_extract_entities_domain_states_outer(hass):
|
||||||
|
"""Test extract entities function by domain."""
|
||||||
|
hass.states.async_set("light.switch", "on")
|
||||||
|
hass.states.async_set("light.switch2", "on")
|
||||||
|
hass.states.async_set("light.switch3", "off")
|
||||||
|
|
||||||
|
assert set(
|
||||||
|
template.extract_entities(
|
||||||
|
hass,
|
||||||
|
"{{ states.light | selectattr('state','eq','off') | list | count > 0 }}",
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
) == {"light.switch", "light.switch2", "light.switch3"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_extract_entities_domain_states_outer_with_group(hass):
|
||||||
|
"""Test extract entities function by domain."""
|
||||||
|
hass.states.async_set("light.switch", "on")
|
||||||
|
hass.states.async_set("light.switch2", "on")
|
||||||
|
hass.states.async_set("light.switch3", "off")
|
||||||
|
hass.states.async_set("switch.pool_light", "off")
|
||||||
|
hass.states.async_set("group.lights", "off", {"entity_id": ["switch.pool_light"]})
|
||||||
|
|
||||||
|
assert set(
|
||||||
|
template.extract_entities(
|
||||||
|
hass,
|
||||||
|
"{{ states.light | selectattr('entity_id', 'in', state_attr('group.lights', 'entity_id')) }}",
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
) == {"light.switch", "light.switch2", "light.switch3", "group.lights"}
|
||||||
|
|
||||||
|
|
||||||
def test_jinja_namespace(hass):
|
def test_jinja_namespace(hass):
|
||||||
"""Test Jinja's namespace command can be used."""
|
"""Test Jinja's namespace command can be used."""
|
||||||
test_template = template.Template(
|
test_template = template.Template(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user