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 authored and Paulus Schoutsen committed 2019-09-05 16:26:22 -07:00
1 parent 23fdc04554
commit b1c2a5fa08
8 files changed
+274 -2

No files matched your search

@@ -5,25 +5,48 @@ import homeassistant.components.automation.state as state
from homeassistant.components.device_automation.const import (
CONF_IS_OFF,
CONF_IS_ON,
CONF_TOGGLE,
CONF_TURN_OFF,
CONF_TURN_ON,
)
from homeassistant.core import split_entity_id
from homeassistant.const import (
CONF_CONDITION,
CONF_DEVICE,
CONF_DEVICE_ID,
CONF_DOMAIN,
CONF_ENTITY_ID,
CONF_PLATFORM,
CONF_TYPE,
)
from homeassistant.helpers import condition, config_validation as cv
from homeassistant.helpers import condition, config_validation as cv, service
from homeassistant.helpers.entity_registry import async_entries_for_device
from . import DOMAIN
# mypy: allow-untyped-defs, no-check-untyped-defs
ENTITY_ACTIONS = [
{
# Turn light off
CONF_DEVICE: None,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TURN_OFF,
},
{
# Turn light on
CONF_DEVICE: None,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TURN_ON,
},
{
# Toggle light
CONF_DEVICE: None,
CONF_DOMAIN: DOMAIN,
CONF_TYPE: CONF_TOGGLE,
},
]
ENTITY_CONDITIONS = [
{
# True when light is turned off
@@ -54,6 +77,18 @@ ENTITY_TRIGGERS = [
},
]
ACTION_SCHEMA = vol.All(
vol.Schema(
{
vol.Required(CONF_DEVICE): None,
vol.Optional(CONF_DEVICE_ID): str,
vol.Required(CONF_DOMAIN): DOMAIN,
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In([CONF_TOGGLE, CONF_TURN_OFF, CONF_TURN_ON]),
}
)
)
CONDITION_SCHEMA = vol.All(
vol.Schema(
{
@@ -83,6 +118,31 @@ def _is_domain(entity, domain):
return split_entity_id(entity.entity_id)[0] == domain
async def async_action_from_config(hass, config, variables, context):
"""Change state based on configuration."""
config = ACTION_SCHEMA(config)
action_type = config[CONF_TYPE]
if action_type == CONF_TURN_ON:
action = "turn_on"
elif action_type == CONF_TURN_OFF:
action = "turn_off"
else:
action = "toggle"
service_action = {
service.CONF_SERVICE: "light.{}".format(action),
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
}
await service.async_call_from_config(
hass,
service_action,
blocking=True,
variables=variables,
# validate_config=False,
context=context,
)
def async_condition_from_config(config, config_validation):
"""Evaluate state based on configuration."""
config = CONDITION_SCHEMA(config)
@@ -140,6 +200,11 @@ async def _async_get_automations(hass, device_id, automation_templates):
return automations
async def async_get_actions(hass, device_id):
"""List device actions."""
return await _async_get_automations(hass, device_id, ENTITY_ACTIONS)
async def async_get_conditions(hass, device_id):
"""List device conditions."""
return await _async_get_automations(hass, device_id, ENTITY_CONDITIONS)
@@ -1,5 +1,10 @@
{
"device_automation": {
"action_type": {
"toggle": "Toggle {name}",
"turn_on": "Turn on {name}",
"turn_off": "Turn off {name}"
},
"condition_type": {
"is_on": "{name} is on",
"is_off": "{name} is off"