2015-09-15 00:02:54 -07:00
|
|
|
"""
|
2016-03-07 20:20:07 +01:00
|
|
|
Offer sun based automation rules.
|
2015-10-13 21:08:34 +02:00
|
|
|
|
|
|
|
For more details about this automation rule, please refer to the documentation
|
2017-04-05 23:23:02 -07:00
|
|
|
at https://home-assistant.io/docs/automation/trigger/#sun-trigger
|
2015-09-15 00:02:54 -07:00
|
|
|
"""
|
|
|
|
from datetime import timedelta
|
2016-04-04 12:18:58 -07:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
2015-09-15 00:02:54 -07:00
|
|
|
|
2016-10-04 20:44:32 -07:00
|
|
|
from homeassistant.core import callback
|
2016-05-02 22:05:09 -07:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_EVENT, CONF_OFFSET, CONF_PLATFORM, SUN_EVENT_SUNRISE)
|
2016-10-01 01:22:13 -07:00
|
|
|
from homeassistant.helpers.event import async_track_sunrise, async_track_sunset
|
2016-04-04 12:18:58 -07:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2015-09-15 00:02:54 -07:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-04-04 12:18:58 -07:00
|
|
|
TRIGGER_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): 'sun',
|
2016-05-02 22:05:09 -07:00
|
|
|
vol.Required(CONF_EVENT): cv.sun_event,
|
2016-04-21 15:52:20 -07:00
|
|
|
vol.Required(CONF_OFFSET, default=timedelta(0)): cv.time_period,
|
2016-04-04 12:18:58 -07:00
|
|
|
})
|
2015-09-15 00:02:54 -07:00
|
|
|
|
|
|
|
|
2018-11-05 09:23:58 +01:00
|
|
|
async def async_trigger(hass, config, action, automation_info):
|
2016-04-04 12:18:58 -07:00
|
|
|
"""Listen for events based on configuration."""
|
|
|
|
event = config.get(CONF_EVENT)
|
|
|
|
offset = config.get(CONF_OFFSET)
|
2015-09-15 00:02:54 -07:00
|
|
|
|
2016-10-04 20:44:32 -07:00
|
|
|
@callback
|
2016-04-21 13:59:42 -07:00
|
|
|
def call_action():
|
|
|
|
"""Call action with right context."""
|
2016-10-04 20:44:32 -07:00
|
|
|
hass.async_run_job(action, {
|
2016-04-21 13:59:42 -07:00
|
|
|
'trigger': {
|
|
|
|
'platform': 'sun',
|
|
|
|
'event': event,
|
|
|
|
'offset': offset,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2016-05-02 22:05:09 -07:00
|
|
|
if event == SUN_EVENT_SUNRISE:
|
2016-10-01 01:22:13 -07:00
|
|
|
return async_track_sunrise(hass, call_action, offset)
|
2017-07-05 20:02:16 -07:00
|
|
|
return async_track_sunset(hass, call_action, offset)
|