core/homeassistant/components/automation/sun.py

48 lines
1.4 KiB
Python
Raw Normal View History

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