Trigger automation without skipping condition (#28484)

* Trigger automation without skipping condition

* [] instead of .get()

* Update __init__.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
This commit is contained in:
Santobert 2020-01-08 10:36:11 +01:00 committed by Franck Nijhof
parent 84a1afc785
commit 3d7488d188
4 changed files with 33 additions and 5 deletions

View File

@ -50,6 +50,7 @@ CONF_ACTION = "action"
CONF_TRIGGER = "trigger"
CONF_CONDITION_TYPE = "condition_type"
CONF_INITIAL_STATE = "initial_state"
CONF_SKIP_CONDITION = "skip_condition"
CONDITION_USE_TRIGGER_VALUES = "use_trigger_values"
CONDITION_TYPE_AND = "and"
@ -107,7 +108,10 @@ PLATFORM_SCHEMA = vol.Schema(
)
TRIGGER_SERVICE_SCHEMA = make_entity_service_schema(
{vol.Optional(ATTR_VARIABLES, default={}): dict}
{
vol.Optional(ATTR_VARIABLES, default={}): dict,
vol.Optional(CONF_SKIP_CONDITION, default=True): bool,
}
)
RELOAD_SERVICE_SCHEMA = vol.Schema({})
@ -135,8 +139,8 @@ async def async_setup(hass, config):
for entity in await component.async_extract_from_service(service_call):
tasks.append(
entity.async_trigger(
service_call.data.get(ATTR_VARIABLES),
skip_condition=True,
service_call.data[ATTR_VARIABLES],
skip_condition=service_call.data[CONF_SKIP_CONDITION],
context=service_call.context,
)
)

View File

@ -27,6 +27,9 @@ trigger:
entity_id:
description: Name of the automation to trigger.
example: 'automation.notify_home'
skip_condition:
description: Whether or not the condition will be skipped (defaults to True).
example: True
reload:
description: Reload the automation configuration.

View File

@ -3,7 +3,11 @@
All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly.
"""
from homeassistant.components.automation import DOMAIN, SERVICE_TRIGGER
from homeassistant.components.automation import (
CONF_SKIP_CONDITION,
DOMAIN,
SERVICE_TRIGGER,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
ENTITY_MATCH_ALL,
@ -37,9 +41,10 @@ async def async_toggle(hass, entity_id=ENTITY_MATCH_ALL):
@bind_hass
async def async_trigger(hass, entity_id=ENTITY_MATCH_ALL):
async def async_trigger(hass, entity_id=ENTITY_MATCH_ALL, skip_condition=True):
"""Trigger specified automation or all."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
data[CONF_SKIP_CONDITION] = skip_condition
await hass.services.async_call(DOMAIN, SERVICE_TRIGGER, data)

View File

@ -225,6 +225,22 @@ async def test_trigger_service_ignoring_condition(hass, calls):
)
assert len(calls) == 1
await hass.services.async_call(
"automation",
"trigger",
{"entity_id": "automation.test", "skip_condition": True},
blocking=True,
)
assert len(calls) == 2
await hass.services.async_call(
"automation",
"trigger",
{"entity_id": "automation.test", "skip_condition": False},
blocking=True,
)
assert len(calls) == 2
async def test_two_conditions_with_and(hass, calls):
"""Test two and conditions."""