Add device automation condition (#26313)
* Add support for device conditions * Lint * Update test case * Make and+or conditions async, adjust tests * Cleanup tests * Remove non callback versions of conditions, correct typing * Correct typing * Update light/strings.json * Address review comments * Make device automation lists simple lists, not dicts * Add device_automation/const.py * Use IS_ON/IS_OFF everywhere for conditions
This commit is contained in:
1 parent
c50faaef3c
commit
f7dc537275
11 files changed
+480
-263
No files matched your search
@@ -2,39 +2,70 @@
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.components.automation.state as state
|
||||
from homeassistant.components.device_automation.const import (
|
||||
CONF_IS_OFF,
|
||||
CONF_IS_ON,
|
||||
CONF_TURN_OFF,
|
||||
CONF_TURN_ON,
|
||||
)
|
||||
from homeassistant.core import split_entity_id
|
||||
from homeassistant.const import (
|
||||
CONF_CONDITION,
|
||||
CONF_DEVICE_ID,
|
||||
CONF_DOMAIN,
|
||||
CONF_ENTITY_ID,
|
||||
CONF_PLATFORM,
|
||||
CONF_TYPE,
|
||||
)
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers import condition, config_validation as cv
|
||||
from homeassistant.helpers.entity_registry import async_entries_for_device
|
||||
from . import DOMAIN
|
||||
|
||||
|
||||
# mypy: allow-untyped-defs, no-check-untyped-defs
|
||||
|
||||
CONF_TURN_OFF = "turn_off"
|
||||
CONF_TURN_ON = "turn_on"
|
||||
ENTITY_CONDITIONS = [
|
||||
{
|
||||
# True when light is turned off
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_TYPE: CONF_IS_OFF,
|
||||
},
|
||||
{
|
||||
# True when light is turned on
|
||||
CONF_CONDITION: "device",
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_TYPE: CONF_IS_ON,
|
||||
},
|
||||
]
|
||||
|
||||
ENTITY_TRIGGERS = [
|
||||
{
|
||||
# Trigger when light is turned on
|
||||
# Trigger when light is turned off
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_TYPE: CONF_TURN_OFF,
|
||||
},
|
||||
{
|
||||
# Trigger when light is turned off
|
||||
# Trigger when light is turned on
|
||||
CONF_PLATFORM: "device",
|
||||
CONF_DOMAIN: DOMAIN,
|
||||
CONF_TYPE: CONF_TURN_ON,
|
||||
},
|
||||
]
|
||||
|
||||
CONDITION_SCHEMA = vol.All(
|
||||
vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_CONDITION): "device",
|
||||
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_IS_OFF, CONF_IS_ON]),
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
TRIGGER_SCHEMA = vol.All(
|
||||
vol.Schema(
|
||||
{
|
||||
@@ -42,7 +73,7 @@ TRIGGER_SCHEMA = vol.All(
|
||||
vol.Optional(CONF_DEVICE_ID): str,
|
||||
vol.Required(CONF_DOMAIN): DOMAIN,
|
||||
vol.Required(CONF_ENTITY_ID): cv.entity_id,
|
||||
vol.Required(CONF_TYPE): str,
|
||||
vol.Required(CONF_TYPE): vol.In([CONF_TURN_OFF, CONF_TURN_ON]),
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -52,9 +83,27 @@ def _is_domain(entity, domain):
|
||||
return split_entity_id(entity.entity_id)[0] == domain
|
||||
|
||||
|
||||
def async_condition_from_config(config, config_validation):
|
||||
"""Evaluate state based on configuration."""
|
||||
config = CONDITION_SCHEMA(config)
|
||||
condition_type = config[CONF_TYPE]
|
||||
if condition_type == CONF_IS_ON:
|
||||
stat = "on"
|
||||
else:
|
||||
stat = "off"
|
||||
state_config = {
|
||||
condition.CONF_CONDITION: "state",
|
||||
condition.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
|
||||
condition.CONF_STATE: stat,
|
||||
}
|
||||
|
||||
return condition.state_from_config(state_config, config_validation)
|
||||
|
||||
|
||||
async def async_attach_trigger(hass, config, action, automation_info):
|
||||
"""Listen for state changes based on configuration."""
|
||||
trigger_type = config.get(CONF_TYPE)
|
||||
config = TRIGGER_SCHEMA(config)
|
||||
trigger_type = config[CONF_TYPE]
|
||||
if trigger_type == CONF_TURN_ON:
|
||||
from_state = "off"
|
||||
to_state = "on"
|
||||
@@ -75,17 +124,27 @@ async def async_trigger(hass, config, action, automation_info):
|
||||
return await async_attach_trigger(hass, config, action, automation_info)
|
||||
|
||||
|
||||
async def async_get_triggers(hass, device_id):
|
||||
"""List device triggers."""
|
||||
triggers = []
|
||||
async def _async_get_automations(hass, device_id, automation_templates):
|
||||
"""List device automations."""
|
||||
automations = []
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
|
||||
entities = async_entries_for_device(entity_registry, device_id)
|
||||
domain_entities = [x for x in entities if _is_domain(x, DOMAIN)]
|
||||
for entity in domain_entities:
|
||||
for trigger in ENTITY_TRIGGERS:
|
||||
trigger = dict(trigger)
|
||||
trigger.update(device_id=device_id, entity_id=entity.entity_id)
|
||||
triggers.append(trigger)
|
||||
for automation in automation_templates:
|
||||
automation = dict(automation)
|
||||
automation.update(device_id=device_id, entity_id=entity.entity_id)
|
||||
automations.append(automation)
|
||||
|
||||
return triggers
|
||||
return automations
|
||||
|
||||
|
||||
async def async_get_conditions(hass, device_id):
|
||||
"""List device conditions."""
|
||||
return await _async_get_automations(hass, device_id, ENTITY_CONDITIONS)
|
||||
|
||||
|
||||
async def async_get_triggers(hass, device_id):
|
||||
"""List device triggers."""
|
||||
return await _async_get_automations(hass, device_id, ENTITY_TRIGGERS)
|
||||
@@ -1,5 +1,9 @@
|
||||
{
|
||||
"device_automation": {
|
||||
"condition_type": {
|
||||
"is_on": "{name} is on",
|
||||
"is_off": "{name} is off"
|
||||
},
|
||||
"trigger_type": {
|
||||
"turn_on": "{name} turned on",
|
||||
"turn_off": "{name} turned off"
|
||||
|
||||
Reference in new issue
Block a user