2019-02-13 20:21:14 +00:00
|
|
|
"""Offer sun based automation rules."""
|
2015-09-15 07:02:54 +00:00
|
|
|
from datetime import timedelta
|
2016-04-04 19:18:58 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2015-09-15 07:02:54 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
from homeassistant.core import callback
|
2016-05-03 05:05:09 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_EVENT,
|
|
|
|
CONF_OFFSET,
|
|
|
|
CONF_PLATFORM,
|
|
|
|
SUN_EVENT_SUNRISE,
|
|
|
|
)
|
2016-10-01 08:22:13 +00:00
|
|
|
from homeassistant.helpers.event import async_track_sunrise, async_track_sunset
|
2016-04-04 19:18:58 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-15 07:02:54 +00:00
|
|
|
|
2019-08-12 03:38:18 +00:00
|
|
|
|
|
|
|
# mypy: allow-untyped-defs, no-check-untyped-defs
|
|
|
|
|
2015-09-15 07:02:54 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
)
|
2015-09-15 07:02:54 +00:00
|
|
|
|
|
|
|
|
2018-11-05 08:23:58 +00:00
|
|
|
async def async_trigger(hass, config, action, automation_info):
|
2016-04-04 19:18:58 +00:00
|
|
|
"""Listen for events based on configuration."""
|
|
|
|
event = config.get(CONF_EVENT)
|
|
|
|
offset = config.get(CONF_OFFSET)
|
2015-09-15 07:02:54 +00:00
|
|
|
|
2016-10-05 03:44:32 +00:00
|
|
|
@callback
|
2016-04-21 20:59:42 +00:00
|
|
|
def call_action():
|
|
|
|
"""Call action with right context."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_run_job(
|
|
|
|
action, {"trigger": {"platform": "sun", "event": event, "offset": offset}}
|
|
|
|
)
|
2016-04-21 20:59:42 +00:00
|
|
|
|
2016-05-03 05:05:09 +00:00
|
|
|
if event == SUN_EVENT_SUNRISE:
|
2016-10-01 08:22:13 +00:00
|
|
|
return async_track_sunrise(hass, call_action, offset)
|
2017-07-06 03:02:16 +00:00
|
|
|
return async_track_sunset(hass, call_action, offset)
|