Validate generated device triggers (#27264)
* Validate generated trigger * Update scaffold
This commit is contained in:
parent
6565c17828
commit
dabdf8b577
7 changed files with 41 additions and 25 deletions
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue