Add device automation action (#26455)

* Add support for device actions, with light as example.

* Add translation; return list
This commit is contained in:
Erik Montnemery 2019-09-06 01:26:22 +02:00 committed by Paulus Schoutsen
parent 23fdc04554
commit b1c2a5fa08
8 changed files with 274 additions and 2 deletions

View file

@ -9,7 +9,7 @@ from typing import Optional, Sequence, Callable, Dict, List, Set, Tuple
import voluptuous as vol
from homeassistant.core import HomeAssistant, Context, callback, CALLBACK_TYPE
from homeassistant.const import CONF_CONDITION, CONF_TIMEOUT
from homeassistant.const import CONF_CONDITION, CONF_DEVICE, CONF_DOMAIN, CONF_TIMEOUT
from homeassistant import exceptions
from homeassistant.helpers import (
service,
@ -22,6 +22,7 @@ from homeassistant.helpers.event import (
async_track_template,
)
from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import async_get_integration
import homeassistant.util.dt as date_util
from homeassistant.util.async_ import run_coroutine_threadsafe, run_callback_threadsafe
@ -48,6 +49,7 @@ ACTION_WAIT_TEMPLATE = "wait_template"
ACTION_CHECK_CONDITION = "condition"
ACTION_FIRE_EVENT = "event"
ACTION_CALL_SERVICE = "call_service"
ACTION_DEVICE_AUTOMATION = "device"
def _determine_action(action):
@ -64,6 +66,9 @@ def _determine_action(action):
if CONF_EVENT in action:
return ACTION_FIRE_EVENT
if CONF_DEVICE in action:
return ACTION_DEVICE_AUTOMATION
return ACTION_CALL_SERVICE
@ -117,6 +122,7 @@ class Script:
ACTION_CHECK_CONDITION: self._async_check_condition,
ACTION_FIRE_EVENT: self._async_fire_event,
ACTION_CALL_SERVICE: self._async_call_service,
ACTION_DEVICE_AUTOMATION: self._async_device_automation,
}
@property
@ -318,6 +324,17 @@ class Script:
context=context,
)
async def _async_device_automation(self, action, variables, context):
"""Perform the device automation specified in the action.
This method is a coroutine.
"""
self.last_action = action.get(CONF_ALIAS, "device automation")
self._log("Executing step %s" % self.last_action)
integration = await async_get_integration(self.hass, action[CONF_DOMAIN])
platform = integration.get_platform("device_automation")
await platform.async_action_from_config(self.hass, action, variables, context)
async def _async_fire_event(self, action, variables, context):
"""Fire an event."""
self.last_action = action.get(CONF_ALIAS, action[CONF_EVENT])