mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Reword numeric_state trigger (#43367)
This commit is contained in:
parent
3ca8f0c475
commit
390b45b149
@ -63,7 +63,7 @@ async def async_attach_trigger(
|
|||||||
hass, config, action, automation_info, *, platform_type="numeric_state"
|
hass, config, action, automation_info, *, platform_type="numeric_state"
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Listen for state changes based on configuration."""
|
"""Listen for state changes based on configuration."""
|
||||||
entity_id = config.get(CONF_ENTITY_ID)
|
entity_ids = config.get(CONF_ENTITY_ID)
|
||||||
below = config.get(CONF_BELOW)
|
below = config.get(CONF_BELOW)
|
||||||
above = config.get(CONF_ABOVE)
|
above = config.get(CONF_ABOVE)
|
||||||
time_delta = config.get(CONF_FOR)
|
time_delta = config.get(CONF_FOR)
|
||||||
@ -78,29 +78,32 @@ async def async_attach_trigger(
|
|||||||
if value_template is not None:
|
if value_template is not None:
|
||||||
value_template.hass = hass
|
value_template.hass = hass
|
||||||
|
|
||||||
@callback
|
def variables(entity_id):
|
||||||
def check_numeric_state(entity, from_s, to_s):
|
"""Return a dict with trigger variables."""
|
||||||
"""Return True if criteria are now met."""
|
return {
|
||||||
if to_s is None:
|
|
||||||
return False
|
|
||||||
|
|
||||||
variables = {
|
|
||||||
"trigger": {
|
"trigger": {
|
||||||
"platform": "numeric_state",
|
"platform": "numeric_state",
|
||||||
"entity_id": entity,
|
"entity_id": entity_id,
|
||||||
"below": below,
|
"below": below,
|
||||||
"above": above,
|
"above": above,
|
||||||
"attribute": attribute,
|
"attribute": attribute,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def check_numeric_state(entity_id, from_s, to_s):
|
||||||
|
"""Return True if criteria are now met."""
|
||||||
|
if to_s is None:
|
||||||
|
return False
|
||||||
|
|
||||||
return condition.async_numeric_state(
|
return condition.async_numeric_state(
|
||||||
hass, to_s, below, above, value_template, variables, attribute
|
hass, to_s, below, above, value_template, variables(entity_id), attribute
|
||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def state_automation_listener(event):
|
def state_automation_listener(event):
|
||||||
"""Listen for state changes and calls action."""
|
"""Listen for state changes and calls action."""
|
||||||
entity = event.data.get("entity_id")
|
entity_id = event.data.get("entity_id")
|
||||||
from_s = event.data.get("old_state")
|
from_s = event.data.get("old_state")
|
||||||
to_s = event.data.get("new_state")
|
to_s = event.data.get("new_state")
|
||||||
|
|
||||||
@ -112,38 +115,29 @@ async def async_attach_trigger(
|
|||||||
{
|
{
|
||||||
"trigger": {
|
"trigger": {
|
||||||
"platform": platform_type,
|
"platform": platform_type,
|
||||||
"entity_id": entity,
|
"entity_id": entity_id,
|
||||||
"below": below,
|
"below": below,
|
||||||
"above": above,
|
"above": above,
|
||||||
"from_state": from_s,
|
"from_state": from_s,
|
||||||
"to_state": to_s,
|
"to_state": to_s,
|
||||||
"for": time_delta if not time_delta else period[entity],
|
"for": time_delta if not time_delta else period[entity_id],
|
||||||
"description": f"numeric state of {entity}",
|
"description": f"numeric state of {entity_id}",
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
to_s.context,
|
to_s.context,
|
||||||
)
|
)
|
||||||
|
|
||||||
matching = check_numeric_state(entity, from_s, to_s)
|
matching = check_numeric_state(entity_id, from_s, to_s)
|
||||||
|
|
||||||
if not matching:
|
if not matching:
|
||||||
entities_triggered.discard(entity)
|
entities_triggered.discard(entity_id)
|
||||||
elif entity not in entities_triggered:
|
elif entity_id not in entities_triggered:
|
||||||
entities_triggered.add(entity)
|
entities_triggered.add(entity_id)
|
||||||
|
|
||||||
if time_delta:
|
if time_delta:
|
||||||
variables = {
|
|
||||||
"trigger": {
|
|
||||||
"platform": "numeric_state",
|
|
||||||
"entity_id": entity,
|
|
||||||
"below": below,
|
|
||||||
"above": above,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
period[entity] = cv.positive_time_period(
|
period[entity_id] = cv.positive_time_period(
|
||||||
template.render_complex(time_delta, variables)
|
template.render_complex(time_delta, variables(entity_id))
|
||||||
)
|
)
|
||||||
except (exceptions.TemplateError, vol.Invalid) as ex:
|
except (exceptions.TemplateError, vol.Invalid) as ex:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
@ -151,20 +145,20 @@ async def async_attach_trigger(
|
|||||||
automation_info["name"],
|
automation_info["name"],
|
||||||
ex,
|
ex,
|
||||||
)
|
)
|
||||||
entities_triggered.discard(entity)
|
entities_triggered.discard(entity_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
unsub_track_same[entity] = async_track_same_state(
|
unsub_track_same[entity_id] = async_track_same_state(
|
||||||
hass,
|
hass,
|
||||||
period[entity],
|
period[entity_id],
|
||||||
call_action,
|
call_action,
|
||||||
entity_ids=entity,
|
entity_ids=entity_id,
|
||||||
async_check_same_func=check_numeric_state,
|
async_check_same_func=check_numeric_state,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
call_action()
|
call_action()
|
||||||
|
|
||||||
unsub = async_track_state_change_event(hass, entity_id, state_automation_listener)
|
unsub = async_track_state_change_event(hass, entity_ids, state_automation_listener)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_remove():
|
def async_remove():
|
||||||
|
@ -34,6 +34,31 @@ def setup_comp(hass):
|
|||||||
mock_component(hass, "group")
|
mock_component(hass, "group")
|
||||||
|
|
||||||
|
|
||||||
|
async def test_if_not_fires_on_entity_removal(hass, calls):
|
||||||
|
"""Test the firing with removed entity."""
|
||||||
|
hass.states.async_set("test.entity", 11)
|
||||||
|
|
||||||
|
assert await async_setup_component(
|
||||||
|
hass,
|
||||||
|
automation.DOMAIN,
|
||||||
|
{
|
||||||
|
automation.DOMAIN: {
|
||||||
|
"trigger": {
|
||||||
|
"platform": "numeric_state",
|
||||||
|
"entity_id": "test.entity",
|
||||||
|
"below": 10,
|
||||||
|
},
|
||||||
|
"action": {"service": "test.automation"},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
# Entity disappears
|
||||||
|
hass.states.async_remove("test.entity")
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
assert len(calls) == 0
|
||||||
|
|
||||||
|
|
||||||
async def test_if_fires_on_entity_change_below(hass, calls):
|
async def test_if_fires_on_entity_change_below(hass, calls):
|
||||||
"""Test the firing with changed entity."""
|
"""Test the firing with changed entity."""
|
||||||
context = Context()
|
context = Context()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user