1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-26 22:18:40 +00:00

Reorg device automation (#26880)

* async_trigger -> async_attach_trigger

* Reorg device automations

* Update docstrings

* Fix types

* Fix extending schemas
This commit is contained in:
Paulus Schoutsen
2019-09-24 14:57:05 -07:00
committed by GitHub
parent b52cfd3409
commit 6fdff9ffab
48 changed files with 2014 additions and 1771 deletions

View File

@@ -3,7 +3,7 @@ import asyncio
from functools import partial
import importlib
import logging
from typing import Any
from typing import Any, Awaitable, Callable
import voluptuous as vol
@@ -23,7 +23,7 @@ from homeassistant.const import (
SERVICE_TURN_ON,
STATE_ON,
)
from homeassistant.core import Context, CoreState
from homeassistant.core import Context, CoreState, HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import condition, extract_domain_configs, script
import homeassistant.helpers.config_validation as cv
@@ -31,6 +31,7 @@ from homeassistant.helpers.config_validation import ENTITY_SERVICE_SCHEMA
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.helpers.typing import TemplateVarsType
from homeassistant.loader import bind_hass
from homeassistant.util.dt import parse_datetime, utcnow
@@ -67,6 +68,8 @@ SERVICE_TRIGGER = "trigger"
_LOGGER = logging.getLogger(__name__)
AutomationActionType = Callable[[HomeAssistant, TemplateVarsType], Awaitable[None]]
def _platform_validator(config):
"""Validate it is a valid platform."""
@@ -474,7 +477,7 @@ async def _async_process_trigger(hass, config, trigger_configs, name, action):
platform = importlib.import_module(".{}".format(conf[CONF_PLATFORM]), __name__)
try:
remove = await platform.async_trigger(hass, conf, action, info)
remove = await platform.async_attach_trigger(hass, conf, action, info)
except InvalidDeviceAutomationConfig:
remove = False

View File

@@ -13,8 +13,8 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for trigger."""
integration = await async_get_integration(hass, config[CONF_DOMAIN])
platform = integration.get_platform("device_automation")
return await platform.async_trigger(hass, config, action, automation_info)
platform = integration.get_platform("device_trigger")
return await platform.async_attach_trigger(hass, config, action, automation_info)

View File

@@ -24,7 +24,9 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(
hass, config, action, automation_info, *, platform_type="event"
):
"""Listen for events based on configuration."""
event_type = config.get(CONF_EVENT_TYPE)
event_data_schema = (
@@ -47,7 +49,7 @@ async def async_trigger(hass, config, action, automation_info):
hass.async_run_job(
action(
{"trigger": {"platform": "event", "event": event}},
{"trigger": {"platform": platform_type, "event": event}},
context=event.context,
)
)

View File

@@ -37,7 +37,7 @@ def source_match(state, source):
return state and state.attributes.get("source") == source
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
source = config.get(CONF_SOURCE).lower()
zone_entity_id = config.get(CONF_ZONE)

View File

@@ -21,7 +21,7 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for events based on configuration."""
event = config.get(CONF_EVENT)

View File

@@ -32,7 +32,7 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for events based on configuration."""
number = config.get(CONF_NUMBER)
held_more_than = config.get(CONF_HELD_MORE_THAN)

View File

@@ -25,7 +25,7 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
topic = config[CONF_TOPIC]
payload = config.get(CONF_PAYLOAD)

View File

@@ -40,7 +40,7 @@ TRIGGER_SCHEMA = vol.All(
_LOGGER = logging.getLogger(__name__)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
entity_id = config.get(CONF_ENTITY_ID)
below = config.get(CONF_BELOW)

View File

@@ -37,7 +37,9 @@ TRIGGER_SCHEMA = vol.All(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(
hass, config, action, automation_info, *, platform_type="state"
):
"""Listen for state changes based on configuration."""
entity_id = config.get(CONF_ENTITY_ID)
from_state = config.get(CONF_FROM, MATCH_ALL)
@@ -59,7 +61,7 @@ async def async_trigger(hass, config, action, automation_info):
action(
{
"trigger": {
"platform": "state",
"platform": platform_type,
"entity_id": entity,
"from_state": from_s,
"to_state": to_s,

View File

@@ -28,7 +28,7 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for events based on configuration."""
event = config.get(CONF_EVENT)
offset = config.get(CONF_OFFSET)

View File

@@ -28,7 +28,7 @@ TRIGGER_SCHEMA = IF_ACTION_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
value_template = config.get(CONF_VALUE_TEMPLATE)
value_template.hass = hass

View File

@@ -18,7 +18,7 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
at_time = config.get(CONF_AT)
hours, minutes, seconds = at_time.hour, at_time.minute, at_time.second

View File

@@ -30,7 +30,7 @@ TRIGGER_SCHEMA = vol.All(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
hours = config.get(CONF_HOURS)
minutes = config.get(CONF_MINUTES)

View File

@@ -36,7 +36,7 @@ async def _handle_webhook(action, hass, webhook_id, request):
hass.async_run_job(action, {"trigger": result})
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Trigger based on incoming webhooks."""
webhook_id = config.get(CONF_WEBHOOK_ID)
hass.components.webhook.async_register(

View File

@@ -31,7 +31,7 @@ TRIGGER_SCHEMA = vol.Schema(
)
async def async_trigger(hass, config, action, automation_info):
async def async_attach_trigger(hass, config, action, automation_info):
"""Listen for state changes based on configuration."""
entity_id = config.get(CONF_ENTITY_ID)
zone_entity_id = config.get(CONF_ZONE)