Refactor sun component for correctness (#7295)
* Refactor sun component for correctness * Convert datetimes to dates for astral * Fix tests for updated code * Fix times now that calcs are fixed * Move sun functions to helpers * Fix flake on new file * Additional tweaks from review * Update requirements
This commit is contained in:
1 parent
419d97fc06
commit
40d27cde0e
19 files changed
+754
-756
No files matched your search
@@ -7,8 +7,7 @@ import sys
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.components import (
|
||||
zone as zone_cmp, sun as sun_cmp)
|
||||
from homeassistant.components import zone as zone_cmp
|
||||
from homeassistant.const import (
|
||||
ATTR_GPS_ACCURACY, ATTR_LATITUDE, ATTR_LONGITUDE,
|
||||
CONF_ENTITY_ID, CONF_VALUE_TEMPLATE, CONF_CONDITION,
|
||||
@@ -17,6 +16,7 @@ from homeassistant.const import (
|
||||
CONF_BELOW, CONF_ABOVE)
|
||||
from homeassistant.exceptions import TemplateError, HomeAssistantError
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.sun import get_astral_event_date
|
||||
import homeassistant.util.dt as dt_util
|
||||
from homeassistant.util.async import run_callback_threadsafe
|
||||
|
||||
@@ -234,24 +234,34 @@ def state_from_config(config, config_validation=True):
|
||||
|
||||
def sun(hass, before=None, after=None, before_offset=None, after_offset=None):
|
||||
"""Test if current time matches sun requirements."""
|
||||
now = dt_util.now().time()
|
||||
utcnow = dt_util.utcnow()
|
||||
today = dt_util.as_local(utcnow).date()
|
||||
before_offset = before_offset or timedelta(0)
|
||||
after_offset = after_offset or timedelta(0)
|
||||
|
||||
if before == SUN_EVENT_SUNRISE and now > (sun_cmp.next_rising(hass) +
|
||||
before_offset).time():
|
||||
sunrise = get_astral_event_date(hass, 'sunrise', today)
|
||||
sunset = get_astral_event_date(hass, 'sunset', today)
|
||||
|
||||
if sunrise is None and (before == SUN_EVENT_SUNRISE or
|
||||
after == SUN_EVENT_SUNRISE):
|
||||
# There is no sunrise today
|
||||
return False
|
||||
|
||||
elif before == SUN_EVENT_SUNSET and now > (sun_cmp.next_setting(hass) +
|
||||
before_offset).time():
|
||||
if sunset is None and (before == SUN_EVENT_SUNSET or
|
||||
after == SUN_EVENT_SUNSET):
|
||||
# There is no sunset today
|
||||
return False
|
||||
|
||||
if after == SUN_EVENT_SUNRISE and now < (sun_cmp.next_rising(hass) +
|
||||
after_offset).time():
|
||||
if before == SUN_EVENT_SUNRISE and utcnow > sunrise + before_offset:
|
||||
return False
|
||||
|
||||
elif after == SUN_EVENT_SUNSET and now < (sun_cmp.next_setting(hass) +
|
||||
after_offset).time():
|
||||
elif before == SUN_EVENT_SUNSET and utcnow > sunset + before_offset:
|
||||
return False
|
||||
|
||||
if after == SUN_EVENT_SUNRISE and utcnow < sunrise + after_offset:
|
||||
return False
|
||||
|
||||
elif after == SUN_EVENT_SUNSET and utcnow < sunset + after_offset:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
Reference in new issue
Block a user