Validate generated device triggers (#27264)

* Validate generated trigger

* Update scaffold
pull/27326/head
Erik Montnemery 2019-10-07 22:09:48 +02:00 committed by Paulus Schoutsen
parent 6565c17828
commit dabdf8b577
7 changed files with 41 additions and 25 deletions

View File

@ -191,6 +191,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
to_state = "off"
state_config = {
state_automation.CONF_PLATFORM: "state",
state_automation.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state_automation.CONF_FROM: from_state,
state_automation.CONF_TO: to_state,
@ -198,6 +199,7 @@ async def async_attach_trigger(hass, config, action, automation_info):
if CONF_FOR in config:
state_config[CONF_FOR] = config[CONF_FOR]
state_config = state_automation.TRIGGER_SCHEMA(state_config)
return await state_automation.async_attach_trigger(
hass, state_config, action, automation_info, platform_type="device"
)

View File

@ -222,13 +222,14 @@ async def async_attach_trigger(hass, config, action, automation_info):
event_id = deconz_event.serial
state_config = {
event_config = {
event.CONF_EVENT_TYPE: CONF_DECONZ_EVENT,
event.CONF_EVENT_DATA: {CONF_UNIQUE_ID: event_id, CONF_EVENT: trigger},
}
event_config = event.TRIGGER_SCHEMA(event_config)
return await event.async_attach_trigger(
hass, state_config, action, automation_info, platform_type="device"
hass, event_config, action, automation_info, platform_type="device"
)

View File

@ -3,7 +3,10 @@ from typing import Any, Dict, List
import voluptuous as vol
from homeassistant.core import Context, HomeAssistant, CALLBACK_TYPE
from homeassistant.components.automation import state, AutomationActionType
from homeassistant.components.automation import (
state as state_automation,
AutomationActionType,
)
from homeassistant.components.device_automation.const import (
CONF_IS_OFF,
CONF_IS_ON,
@ -152,14 +155,16 @@ async def async_attach_trigger(
from_state = "on"
to_state = "off"
state_config = {
state.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state.CONF_FROM: from_state,
state.CONF_TO: to_state,
state_automation.CONF_PLATFORM: "state",
state_automation.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state_automation.CONF_FROM: from_state,
state_automation.CONF_TO: to_state,
}
if CONF_FOR in config:
state_config[CONF_FOR] = config[CONF_FOR]
return await state.async_attach_trigger(
state_config = state_automation.TRIGGER_SCHEMA(state_config)
return await state_automation.async_attach_trigger(
hass, state_config, action, automation_info, platform_type="device"
)

View File

@ -87,14 +87,17 @@ TRIGGER_SCHEMA = vol.All(
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
numeric_state_config = {
numeric_state_automation.CONF_PLATFORM: "numeric_state",
numeric_state_automation.CONF_ENTITY_ID: config[CONF_ENTITY_ID],
numeric_state_automation.CONF_ABOVE: config.get(CONF_ABOVE),
numeric_state_automation.CONF_BELOW: config.get(CONF_BELOW),
numeric_state_automation.CONF_FOR: config.get(CONF_FOR),
}
if CONF_ABOVE in config:
numeric_state_config[numeric_state_automation.CONF_ABOVE] = config[CONF_ABOVE]
if CONF_BELOW in config:
numeric_state_config[numeric_state_automation.CONF_BELOW] = config[CONF_BELOW]
if CONF_FOR in config:
numeric_state_config[CONF_FOR] = config[CONF_FOR]
numeric_state_config = numeric_state_automation.TRIGGER_SCHEMA(numeric_state_config)
return await numeric_state_automation.async_attach_trigger(
hass, numeric_state_config, action, automation_info, platform_type="device"
)

View File

@ -35,10 +35,12 @@ async def async_attach_trigger(hass, config, action, automation_info):
trigger = zha_device.device_automation_triggers[trigger]
event_config = {
event.CONF_PLATFORM: "event",
event.CONF_EVENT_TYPE: ZHA_EVENT,
event.CONF_EVENT_DATA: {DEVICE_IEEE: str(zha_device.ieee), **trigger},
}
event_config = event.TRIGGER_SCHEMA(event_config)
return await event.async_attach_trigger(
hass, event_config, action, automation_info, platform_type="device"
)

View File

@ -12,7 +12,7 @@ from homeassistant.const import (
STATE_OFF,
)
from homeassistant.core import HomeAssistant, CALLBACK_TYPE
from homeassistant.helpers import entity_registry
from homeassistant.helpers import config_validation as cv, entity_registry
from homeassistant.helpers.typing import ConfigType
from homeassistant.components.automation import state, AutomationActionType
from homeassistant.components.device_automation import TRIGGER_BASE_SCHEMA
@ -22,7 +22,10 @@ from . import DOMAIN
TRIGGER_TYPES = {"turned_on", "turned_off"}
TRIGGER_SCHEMA = TRIGGER_BASE_SCHEMA.extend(
{vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES)}
{
vol.Required(CONF_ENTITY_ID): cv.entity_id,
vol.Required(CONF_TYPE): vol.In(TRIGGER_TYPES),
}
)
@ -87,14 +90,13 @@ async def async_attach_trigger(
from_state = STATE_ON
to_state = STATE_OFF
return state.async_attach_trigger(
hass,
{
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state.CONF_FROM: from_state,
state.CONF_TO: to_state,
},
action,
automation_info,
platform_type="device",
state_config = {
state.CONF_PLATFORM: "state",
CONF_ENTITY_ID: config[CONF_ENTITY_ID],
state.CONF_FROM: from_state,
state.CONF_TO: to_state,
}
state_config = state.TRIGGER_SCHEMA(state_config)
return await state.async_attach_trigger(
hass, state_config, action, automation_info, platform_type="device"
)

View File

@ -1,7 +1,7 @@
"""The tests for NEW_NAME device triggers."""
import pytest
from homeassistant.components.switch import DOMAIN
from homeassistant.components.NEW_DOMAIN import DOMAIN
from homeassistant.const import STATE_ON, STATE_OFF
from homeassistant.setup import async_setup_component
import homeassistant.components.automation as automation
@ -9,6 +9,7 @@ from homeassistant.helpers import device_registry
from tests.common import (
MockConfigEntry,
assert_lists_same,
async_mock_service,
mock_device_registry,
mock_registry,
@ -35,7 +36,7 @@ def calls(hass):
async def test_get_triggers(hass, device_reg, entity_reg):
"""Test we get the expected triggers from a switch."""
"""Test we get the expected triggers from a NEW_DOMAIN."""
config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create(
@ -60,7 +61,7 @@ async def test_get_triggers(hass, device_reg, entity_reg):
},
]
triggers = await async_get_device_automations(hass, "trigger", device_entry.id)
assert triggers == expected_triggers
assert_lists_same(triggers, expected_triggers)
async def test_if_fires_on_state_change(hass, calls):