mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Moved sunrise/sunset tracking to helpers
The automation component contained some very handy and generic functions for tracking sunset and sunrise. This was moved to helpers/event.py.
This commit is contained in:
parent
a65d0f0549
commit
81dd1515ae
@ -10,7 +10,7 @@ import logging
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
|
|
||||||
from homeassistant.components import sun
|
from homeassistant.components import sun
|
||||||
from homeassistant.helpers.event import track_point_in_utc_time
|
from homeassistant.helpers.event import track_sunrise, track_sunset
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
DEPENDENCIES = ['sun']
|
DEPENDENCIES = ['sun']
|
||||||
@ -47,9 +47,9 @@ def trigger(hass, config, action):
|
|||||||
|
|
||||||
# Do something to call action
|
# Do something to call action
|
||||||
if event == EVENT_SUNRISE:
|
if event == EVENT_SUNRISE:
|
||||||
trigger_sunrise(hass, action, offset)
|
track_sunrise(hass, action, offset)
|
||||||
else:
|
else:
|
||||||
trigger_sunset(hass, action, offset)
|
track_sunset(hass, action, offset)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@ -125,44 +125,6 @@ def if_action(hass, config):
|
|||||||
return time_if
|
return time_if
|
||||||
|
|
||||||
|
|
||||||
def trigger_sunrise(hass, action, offset):
|
|
||||||
""" Trigger action at next sun rise. """
|
|
||||||
def next_rise():
|
|
||||||
""" Returns next sunrise. """
|
|
||||||
next_time = sun.next_rising_utc(hass) + offset
|
|
||||||
|
|
||||||
while next_time < dt_util.utcnow():
|
|
||||||
next_time = next_time + timedelta(days=1)
|
|
||||||
|
|
||||||
return next_time
|
|
||||||
|
|
||||||
def sunrise_automation_listener(now):
|
|
||||||
""" Called when it's time for action. """
|
|
||||||
track_point_in_utc_time(hass, sunrise_automation_listener, next_rise())
|
|
||||||
action()
|
|
||||||
|
|
||||||
track_point_in_utc_time(hass, sunrise_automation_listener, next_rise())
|
|
||||||
|
|
||||||
|
|
||||||
def trigger_sunset(hass, action, offset):
|
|
||||||
""" Trigger action at next sun set. """
|
|
||||||
def next_set():
|
|
||||||
""" Returns next sunrise. """
|
|
||||||
next_time = sun.next_setting_utc(hass) + offset
|
|
||||||
|
|
||||||
while next_time < dt_util.utcnow():
|
|
||||||
next_time = next_time + timedelta(days=1)
|
|
||||||
|
|
||||||
return next_time
|
|
||||||
|
|
||||||
def sunset_automation_listener(now):
|
|
||||||
""" Called when it's time for action. """
|
|
||||||
track_point_in_utc_time(hass, sunset_automation_listener, next_set())
|
|
||||||
action()
|
|
||||||
|
|
||||||
track_point_in_utc_time(hass, sunset_automation_listener, next_set())
|
|
||||||
|
|
||||||
|
|
||||||
def _parse_offset(raw_offset):
|
def _parse_offset(raw_offset):
|
||||||
if raw_offset is None:
|
if raw_offset is None:
|
||||||
return timedelta(0)
|
return timedelta(0)
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
"""
|
"""
|
||||||
Helpers for listening to events
|
Helpers for listening to events
|
||||||
"""
|
"""
|
||||||
|
from datetime import timedelta
|
||||||
import functools as ft
|
import functools as ft
|
||||||
|
|
||||||
from ..util import dt as dt_util
|
from ..util import dt as dt_util
|
||||||
from ..const import (
|
from ..const import (
|
||||||
ATTR_NOW, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL)
|
ATTR_NOW, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL)
|
||||||
|
from homeassistant.components import sun
|
||||||
|
|
||||||
|
|
||||||
def track_state_change(hass, entity_ids, action, from_state=None,
|
def track_state_change(hass, entity_ids, action, from_state=None,
|
||||||
@ -95,6 +97,52 @@ def track_point_in_utc_time(hass, action, point_in_time):
|
|||||||
return point_in_time_listener
|
return point_in_time_listener
|
||||||
|
|
||||||
|
|
||||||
|
def track_sunrise(hass, action, offset=None):
|
||||||
|
"""
|
||||||
|
Adds a listener that will fire a specified offset from sunrise daily.
|
||||||
|
"""
|
||||||
|
offset = offset or timedelta()
|
||||||
|
|
||||||
|
def next_rise():
|
||||||
|
""" Returns next sunrise. """
|
||||||
|
next_time = sun.next_rising_utc(hass) + offset
|
||||||
|
|
||||||
|
while next_time < dt_util.utcnow():
|
||||||
|
next_time = next_time + timedelta(days=1)
|
||||||
|
|
||||||
|
return next_time
|
||||||
|
|
||||||
|
def sunrise_automation_listener(now):
|
||||||
|
""" Called when it's time for action. """
|
||||||
|
track_point_in_utc_time(hass, sunrise_automation_listener, next_rise())
|
||||||
|
action()
|
||||||
|
|
||||||
|
track_point_in_utc_time(hass, sunrise_automation_listener, next_rise())
|
||||||
|
|
||||||
|
|
||||||
|
def track_sunset(hass, action, offset=None):
|
||||||
|
"""
|
||||||
|
Adds a listener that will fire a specified offset from sunset daily.
|
||||||
|
"""
|
||||||
|
offset = offset or timedelta()
|
||||||
|
|
||||||
|
def next_set():
|
||||||
|
""" Returns next sunrise. """
|
||||||
|
next_time = sun.next_setting_utc(hass) + offset
|
||||||
|
|
||||||
|
while next_time < dt_util.utcnow():
|
||||||
|
next_time = next_time + timedelta(days=1)
|
||||||
|
|
||||||
|
return next_time
|
||||||
|
|
||||||
|
def sunset_automation_listener(now):
|
||||||
|
""" Called when it's time for action. """
|
||||||
|
track_point_in_utc_time(hass, sunset_automation_listener, next_set())
|
||||||
|
action()
|
||||||
|
|
||||||
|
track_point_in_utc_time(hass, sunset_automation_listener, next_set())
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-arguments
|
# pylint: disable=too-many-arguments
|
||||||
def track_utc_time_change(hass, action, year=None, month=None, day=None,
|
def track_utc_time_change(hass, action, year=None, month=None, day=None,
|
||||||
hour=None, minute=None, second=None, local=False):
|
hour=None, minute=None, second=None, local=False):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user