Skip to content

Commit

Permalink
Use const.SUN_EVENT_* more (#18039)
Browse files Browse the repository at this point in the history
  • Loading branch information
scop authored and Danielhiversen committed Oct 31, 2018
1 parent dcc4622 commit 9c840f9
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 67 deletions.
10 changes: 6 additions & 4 deletions homeassistant/components/device_sun_light_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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
Expand Down Expand Up @@ -79,7 +79,7 @@ def calc_time_for_light_when_sunset():
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)
Expand Down Expand Up @@ -123,7 +123,8 @@ def schedule_light_turn_on(now):
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.
Expand Down Expand Up @@ -153,7 +154,8 @@ def check_light_on_dev_state_change(entity, old_state, new_state):
# 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
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/components/sensor/jewish_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
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
Expand Down Expand Up @@ -114,7 +115,7 @@ async def async_update(self):
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)

Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
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 (
Expand Down Expand Up @@ -109,9 +110,9 @@ def update_as_of(self, utc_point_in_time):
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):
Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/switch/flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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
Expand Down Expand Up @@ -200,7 +200,7 @@ def flux_update(self, now=None):
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)

Expand Down Expand Up @@ -283,7 +283,8 @@ def find_start_time(self, now):
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):
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/helpers/condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/helpers/config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ def validator(value):
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'))

Expand Down
11 changes: 6 additions & 5 deletions homeassistant/helpers/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
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

Expand Down Expand Up @@ -274,12 +275,12 @@ def sunrise_automation_listener(now):
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."""
Expand All @@ -303,12 +304,12 @@ def sunset_automation_listener(now):
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."""
Expand Down
7 changes: 5 additions & 2 deletions homeassistant/helpers/sun.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
21 changes: 11 additions & 10 deletions tests/components/automation/test_sun.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand All @@ -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': {
Expand Down Expand Up @@ -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': {
Expand All @@ -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'
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -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': {
Expand Down Expand Up @@ -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'
Expand Down
Loading

0 comments on commit 9c840f9

Please sign in to comment.