Reorganize trigger code (#38655)

This commit is contained in:
Phil Bruckner authored and GitHub committed 2020-08-17 11:54:56 -05:00
1 parent fca071742d
commit ca9dd0c833
51 files changed
+306 -194

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
"""Offer sun based automation rules."""
from datetime import timedelta
import logging
import voluptuous as vol
from homeassistant.const import (
CONF_EVENT,
CONF_OFFSET,
CONF_PLATFORM,
SUN_EVENT_SUNRISE,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_track_sunrise, async_track_sunset
# mypy: allow-untyped-defs, no-check-untyped-defs
_LOGGER = logging.getLogger(__name__)
TRIGGER_SCHEMA = vol.Schema(
{
vol.Required(CONF_PLATFORM): "sun",
vol.Required(CONF_EVENT): cv.sun_event,
vol.Required(CONF_OFFSET, default=timedelta(0)): cv.time_period,
}
)
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)
@callback
def call_action():
"""Call action with right context."""
hass.async_run_job(
action, {"trigger": {"platform": "sun", "event": event, "offset": offset}}
)
if event == SUN_EVENT_SUNRISE:
return async_track_sunrise(hass, call_action, offset)
return async_track_sunset(hass, call_action, offset)