diff --git a/homeassistant/components/binary_sensor/device_trigger.py b/homeassistant/components/binary_sensor/device_trigger.py index e05713b5c67..5d58131fde9 100644 --- a/homeassistant/components/binary_sensor/device_trigger.py +++ b/homeassistant/components/binary_sensor/device_trigger.py @@ -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" ) diff --git a/homeassistant/components/deconz/device_trigger.py b/homeassistant/components/deconz/device_trigger.py index badbe8b8651..9f66cf156aa 100644 --- a/homeassistant/components/deconz/device_trigger.py +++ b/homeassistant/components/deconz/device_trigger.py @@ -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" ) diff --git a/homeassistant/components/device_automation/toggle_entity.py b/homeassistant/components/device_automation/toggle_entity.py index af29625f3a1..29110144c14 100644 --- a/homeassistant/components/device_automation/toggle_entity.py +++ b/homeassistant/components/device_automation/toggle_entity.py @@ -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" ) diff --git a/homeassistant/components/sensor/device_trigger.py b/homeassistant/components/sensor/device_trigger.py index 0d9e8f5af80..bd53dca0c9d 100644 --- a/homeassistant/components/sensor/device_trigger.py +++ b/homeassistant/components/sensor/device_trigger.py @@ -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" ) diff --git a/homeassistant/components/zha/device_trigger.py b/homeassistant/components/zha/device_trigger.py index c1ea3c2b761..ddf7465e0c0 100644 --- a/homeassistant/components/zha/device_trigger.py +++ b/homeassistant/components/zha/device_trigger.py @@ -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" ) diff --git a/script/scaffold/templates/device_trigger/integration/device_trigger.py b/script/scaffold/templates/device_trigger/integration/device_trigger.py index f7e9fc091f8..e0741734d5f 100644 --- a/script/scaffold/templates/device_trigger/integration/device_trigger.py +++ b/script/scaffold/templates/device_trigger/integration/device_trigger.py @@ -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" ) diff --git a/script/scaffold/templates/device_trigger/tests/test_device_trigger.py b/script/scaffold/templates/device_trigger/tests/test_device_trigger.py index c22197bb136..99e1f8937af 100644 --- a/script/scaffold/templates/device_trigger/tests/test_device_trigger.py +++ b/script/scaffold/templates/device_trigger/tests/test_device_trigger.py @@ -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):