Use const.SUN_EVENT_* more (#18039)

This commit is contained in:
Ville Skyttä 2018-10-31 10:10:28 +02:00 committed by Daniel Høyer Iversen
parent dcc46226ee
commit 9c840f93f0
11 changed files with 82 additions and 67 deletions

View file

@ -15,7 +15,7 @@ from homeassistant.components.light import (
ATTR_PROFILE, ATTR_TRANSITION, DOMAIN as DOMAIN_LIGHT)
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_HOME,
STATE_NOT_HOME)
STATE_NOT_HOME, SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET)
from homeassistant.helpers.event import (
async_track_point_in_utc_time, async_track_state_change)
from homeassistant.helpers.sun import is_up, get_astral_event_next
@ -79,7 +79,7 @@ async def async_setup(hass, config):
Async friendly.
"""
next_setting = get_astral_event_next(hass, 'sunset')
next_setting = get_astral_event_next(hass, SUN_EVENT_SUNSET)
if not next_setting:
return None
return next_setting - LIGHT_TRANSITION_TIME * len(light_ids)
@ -123,7 +123,8 @@ async def async_setup(hass, config):
start_point + index * LIGHT_TRANSITION_TIME)
async_track_point_in_utc_time(hass, schedule_light_turn_on,
get_astral_event_next(hass, 'sunrise'))
get_astral_event_next(hass,
SUN_EVENT_SUNRISE))
# If the sun is already above horizon schedule the time-based pre-sun set
# event.
@ -153,7 +154,8 @@ async def async_setup(hass, config):
# Check this by seeing if current time is later then the point
# in time when we would start putting the lights on.
elif (start_point and
start_point < now < get_astral_event_next(hass, 'sunset')):
start_point < now < get_astral_event_next(hass,
SUN_EVENT_SUNSET)):
# Check for every light if it would be on if someone was home
# when the fading in started and turn it on if so

View file

@ -10,7 +10,8 @@ import logging
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
from homeassistant.const import (
CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME, SUN_EVENT_SUNSET)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.sun import get_astral_event_date
@ -114,7 +115,7 @@ class JewishCalSensor(Entity):
today = now.date()
upcoming_saturday = today + timedelta((12 - today.weekday()) % 7)
sunset = dt_util.as_local(get_astral_event_date(
self.hass, 'sunset', today))
self.hass, SUN_EVENT_SUNSET, today))
_LOGGER.debug("Now: %s Sunset: %s", now, sunset)

View file

@ -7,7 +7,8 @@ https://home-assistant.io/components/sun/
import logging
from datetime import timedelta
from homeassistant.const import CONF_ELEVATION
from homeassistant.const import (
CONF_ELEVATION, SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET)
from homeassistant.core import callback
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import (
@ -109,9 +110,9 @@ class Sun(Entity):
self.next_noon = get_astral_event_next(
self.hass, 'solar_noon', utc_point_in_time)
self.next_rising = get_astral_event_next(
self.hass, 'sunrise', utc_point_in_time)
self.hass, SUN_EVENT_SUNRISE, utc_point_in_time)
self.next_setting = get_astral_event_next(
self.hass, 'sunset', utc_point_in_time)
self.hass, SUN_EVENT_SUNSET, utc_point_in_time)
@callback
def update_sun_position(self, utc_point_in_time):

View file

@ -18,7 +18,7 @@ from homeassistant.components.light import (
from homeassistant.components.switch import DOMAIN, SwitchDevice
from homeassistant.const import (
ATTR_ENTITY_ID, CONF_NAME, CONF_PLATFORM, CONF_LIGHTS, CONF_MODE,
SERVICE_TURN_ON)
SERVICE_TURN_ON, SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET)
from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.sun import get_astral_event_date
from homeassistant.util import slugify
@ -200,7 +200,7 @@ class FluxSwitch(SwitchDevice):
if now is None:
now = dt_now()
sunset = get_astral_event_date(self.hass, 'sunset', now.date())
sunset = get_astral_event_date(self.hass, SUN_EVENT_SUNSET, now.date())
start_time = self.find_start_time(now)
stop_time = self.find_stop_time(now)
@ -283,7 +283,8 @@ class FluxSwitch(SwitchDevice):
hour=self._start_time.hour, minute=self._start_time.minute,
second=0)
else:
sunrise = get_astral_event_date(self.hass, 'sunrise', now.date())
sunrise = get_astral_event_date(self.hass, SUN_EVENT_SUNRISE,
now.date())
return sunrise
def find_stop_time(self, now):

View file

@ -243,8 +243,8 @@ def sun(hass, before=None, after=None, before_offset=None, after_offset=None):
before_offset = before_offset or timedelta(0)
after_offset = after_offset or timedelta(0)
sunrise = get_astral_event_date(hass, 'sunrise', today)
sunset = get_astral_event_date(hass, 'sunset', today)
sunrise = get_astral_event_date(hass, SUN_EVENT_SUNRISE, today)
sunset = get_astral_event_date(hass, SUN_EVENT_SUNSET, today)
if sunrise is None and SUN_EVENT_SUNRISE in (before, after):
# There is no sunrise today

View file

@ -535,7 +535,8 @@ SUN_CONDITION_SCHEMA = vol.All(vol.Schema({
vol.Required(CONF_CONDITION): 'sun',
vol.Optional('before'): sun_event,
vol.Optional('before_offset'): time_period,
vol.Optional('after'): vol.All(vol.Lower, vol.Any('sunset', 'sunrise')),
vol.Optional('after'): vol.All(vol.Lower, vol.Any(
SUN_EVENT_SUNSET, SUN_EVENT_SUNRISE)),
vol.Optional('after_offset'): time_period,
}), has_at_least_one_key('before', 'after'))

View file

@ -6,7 +6,8 @@ from homeassistant.loader import bind_hass
from homeassistant.helpers.sun import get_astral_event_next
from ..core import HomeAssistant, callback
from ..const import (
ATTR_NOW, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL)
ATTR_NOW, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED, MATCH_ALL,
SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET)
from ..util import dt as dt_util
from ..util.async_ import run_callback_threadsafe
@ -274,12 +275,12 @@ def async_track_sunrise(hass, action, offset=None):
nonlocal remove
remove = async_track_point_in_utc_time(
hass, sunrise_automation_listener, get_astral_event_next(
hass, 'sunrise', offset=offset))
hass, SUN_EVENT_SUNRISE, offset=offset))
hass.async_run_job(action)
remove = async_track_point_in_utc_time(
hass, sunrise_automation_listener, get_astral_event_next(
hass, 'sunrise', offset=offset))
hass, SUN_EVENT_SUNRISE, offset=offset))
def remove_listener():
"""Remove sunset listener."""
@ -303,12 +304,12 @@ def async_track_sunset(hass, action, offset=None):
nonlocal remove
remove = async_track_point_in_utc_time(
hass, sunset_automation_listener, get_astral_event_next(
hass, 'sunset', offset=offset))
hass, SUN_EVENT_SUNSET, offset=offset))
hass.async_run_job(action)
remove = async_track_point_in_utc_time(
hass, sunset_automation_listener, get_astral_event_next(
hass, 'sunset', offset=offset))
hass, SUN_EVENT_SUNSET, offset=offset))
def remove_listener():
"""Remove sunset listener."""

View file

@ -1,6 +1,7 @@
"""Helpers for sun events."""
import datetime
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
from homeassistant.core import callback
from homeassistant.util import dt as dt_util
from homeassistant.loader import bind_hass
@ -86,7 +87,9 @@ def is_up(hass, utc_point_in_time=None):
if utc_point_in_time is None:
utc_point_in_time = dt_util.utcnow()
next_sunrise = get_astral_event_next(hass, 'sunrise', utc_point_in_time)
next_sunset = get_astral_event_next(hass, 'sunset', utc_point_in_time)
next_sunrise = get_astral_event_next(hass, SUN_EVENT_SUNRISE,
utc_point_in_time)
next_sunset = get_astral_event_next(hass, SUN_EVENT_SUNSET,
utc_point_in_time)
return next_sunrise > next_sunset

View file

@ -4,6 +4,7 @@ from datetime import datetime
import pytest
from unittest.mock import patch
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
from homeassistant.setup import async_setup_component
from homeassistant.components import sun
import homeassistant.components.automation as automation
@ -39,7 +40,7 @@ async def test_sunset_trigger(hass, calls):
automation.DOMAIN: {
'trigger': {
'platform': 'sun',
'event': 'sunset',
'event': SUN_EVENT_SUNSET,
},
'action': {
'service': 'test.automation',
@ -75,7 +76,7 @@ async def test_sunrise_trigger(hass, calls):
automation.DOMAIN: {
'trigger': {
'platform': 'sun',
'event': 'sunrise',
'event': SUN_EVENT_SUNRISE,
},
'action': {
'service': 'test.automation',
@ -99,7 +100,7 @@ async def test_sunset_trigger_with_offset(hass, calls):
automation.DOMAIN: {
'trigger': {
'platform': 'sun',
'event': 'sunset',
'event': SUN_EVENT_SUNSET,
'offset': '0:30:00'
},
'action': {
@ -130,7 +131,7 @@ async def test_sunrise_trigger_with_offset(hass, calls):
automation.DOMAIN: {
'trigger': {
'platform': 'sun',
'event': 'sunrise',
'event': SUN_EVENT_SUNRISE,
'offset': '-0:30:00'
},
'action': {
@ -154,7 +155,7 @@ async def test_if_action_before(hass, calls):
},
'condition': {
'condition': 'sun',
'before': 'sunrise',
'before': SUN_EVENT_SUNRISE,
},
'action': {
'service': 'test.automation'
@ -187,7 +188,7 @@ async def test_if_action_after(hass, calls):
},
'condition': {
'condition': 'sun',
'after': 'sunrise',
'after': SUN_EVENT_SUNRISE,
},
'action': {
'service': 'test.automation'
@ -220,7 +221,7 @@ async def test_if_action_before_with_offset(hass, calls):
},
'condition': {
'condition': 'sun',
'before': 'sunrise',
'before': SUN_EVENT_SUNRISE,
'before_offset': '+1:00:00'
},
'action': {
@ -254,7 +255,7 @@ async def test_if_action_after_with_offset(hass, calls):
},
'condition': {
'condition': 'sun',
'after': 'sunrise',
'after': SUN_EVENT_SUNRISE,
'after_offset': '+1:00:00'
},
'action': {
@ -288,8 +289,8 @@ async def test_if_action_before_and_after_during(hass, calls):
},
'condition': {
'condition': 'sun',
'after': 'sunrise',
'before': 'sunset'
'after': SUN_EVENT_SUNRISE,
'before': SUN_EVENT_SUNSET
},
'action': {
'service': 'test.automation'

View file

@ -4,7 +4,8 @@ from unittest.mock import patch
from homeassistant.setup import setup_component
from homeassistant.components import switch, light
from homeassistant.const import CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON
from homeassistant.const import (
CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON, SUN_EVENT_SUNRISE)
import homeassistant.loader as loader
import homeassistant.util.dt as dt_util
@ -91,7 +92,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -131,7 +132,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -176,7 +177,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -222,7 +223,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -269,7 +270,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -314,7 +315,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -364,7 +365,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -415,7 +416,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -465,7 +466,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -514,7 +515,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -564,7 +565,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -611,7 +612,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -660,7 +661,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -720,7 +721,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
print('sunrise {}'.format(sunrise_time))
return sunrise_time
print('sunset {}'.format(sunset_time))
@ -774,7 +775,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time
@ -818,7 +819,7 @@ class TestSwitchFlux(unittest.TestCase):
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
def event_date(hass, event, now=None):
if event == 'sunrise':
if event == SUN_EVENT_SUNRISE:
return sunrise_time
return sunset_time

View file

@ -4,6 +4,7 @@ import unittest
from unittest.mock import patch
from datetime import timedelta, datetime
from homeassistant.const import SUN_EVENT_SUNRISE, SUN_EVENT_SUNSET
import homeassistant.util.dt as dt_util
import homeassistant.helpers.sun as sun
@ -92,9 +93,9 @@ class TestSun(unittest.TestCase):
assert next_noon == sun.get_astral_event_next(
self.hass, 'solar_noon')
assert next_rising == sun.get_astral_event_next(
self.hass, 'sunrise')
self.hass, SUN_EVENT_SUNRISE)
assert next_setting == sun.get_astral_event_next(
self.hass, 'sunset')
self.hass, SUN_EVENT_SUNSET)
def test_date_events(self):
"""Test retrieving next sun events."""
@ -123,9 +124,9 @@ class TestSun(unittest.TestCase):
assert noon == sun.get_astral_event_date(
self.hass, 'solar_noon', utc_today)
assert sunrise == sun.get_astral_event_date(
self.hass, 'sunrise', utc_today)
self.hass, SUN_EVENT_SUNRISE, utc_today)
assert sunset == sun.get_astral_event_date(
self.hass, 'sunset', utc_today)
self.hass, SUN_EVENT_SUNSET, utc_today)
def test_date_events_default_date(self):
"""Test retrieving next sun events."""
@ -155,9 +156,9 @@ class TestSun(unittest.TestCase):
assert noon == sun.get_astral_event_date(
self.hass, 'solar_noon', utc_today)
assert sunrise == sun.get_astral_event_date(
self.hass, 'sunrise', utc_today)
self.hass, SUN_EVENT_SUNRISE, utc_today)
assert sunset == sun.get_astral_event_date(
self.hass, 'sunset', utc_today)
self.hass, SUN_EVENT_SUNSET, utc_today)
def test_date_events_accepts_datetime(self):
"""Test retrieving next sun events."""
@ -186,9 +187,9 @@ class TestSun(unittest.TestCase):
assert noon == sun.get_astral_event_date(
self.hass, 'solar_noon', utc_now)
assert sunrise == sun.get_astral_event_date(
self.hass, 'sunrise', utc_now)
self.hass, SUN_EVENT_SUNRISE, utc_now)
assert sunset == sun.get_astral_event_date(
self.hass, 'sunset', utc_now)
self.hass, SUN_EVENT_SUNSET, utc_now)
def test_is_up(self):
"""Test retrieving next sun events."""
@ -209,19 +210,21 @@ class TestSun(unittest.TestCase):
june = datetime(2016, 6, 1, tzinfo=dt_util.UTC)
print(sun.get_astral_event_date(self.hass, 'sunrise',
print(sun.get_astral_event_date(self.hass, SUN_EVENT_SUNRISE,
datetime(2017, 7, 25)))
print(sun.get_astral_event_date(self.hass, 'sunset',
print(sun.get_astral_event_date(self.hass, SUN_EVENT_SUNSET,
datetime(2017, 7, 25)))
print(sun.get_astral_event_date(self.hass, 'sunrise',
print(sun.get_astral_event_date(self.hass, SUN_EVENT_SUNRISE,
datetime(2017, 7, 26)))
print(sun.get_astral_event_date(self.hass, 'sunset',
print(sun.get_astral_event_date(self.hass, SUN_EVENT_SUNSET,
datetime(2017, 7, 26)))
assert sun.get_astral_event_next(self.hass, 'sunrise', june) == \
datetime(2016, 7, 25, 23, 23, 39, tzinfo=dt_util.UTC)
assert sun.get_astral_event_next(self.hass, 'sunset', june) == \
datetime(2016, 7, 26, 22, 19, 1, tzinfo=dt_util.UTC)
assert sun.get_astral_event_date(self.hass, 'sunrise', june) is None
assert sun.get_astral_event_date(self.hass, 'sunset', june) is None
assert sun.get_astral_event_next(self.hass, SUN_EVENT_SUNRISE, june) \
== datetime(2016, 7, 25, 23, 23, 39, tzinfo=dt_util.UTC)
assert sun.get_astral_event_next(self.hass, SUN_EVENT_SUNSET, june) \
== datetime(2016, 7, 26, 22, 19, 1, tzinfo=dt_util.UTC)
assert sun.get_astral_event_date(self.hass, SUN_EVENT_SUNRISE, june) \
is None
assert sun.get_astral_event_date(self.hass, SUN_EVENT_SUNSET, june) \
is None