2013-12-11 08:07:30 +00:00
|
|
|
"""
|
2016-03-08 16:55:57 +00:00
|
|
|
Support for functionality to keep track of the sun.
|
2015-02-03 19:18:19 +00:00
|
|
|
|
2015-10-14 08:38:08 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/sun/
|
2013-12-11 08:07:30 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2015-05-15 04:07:15 +00:00
|
|
|
from datetime import timedelta
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-07-17 02:49:54 +00:00
|
|
|
import homeassistant.util as util
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2015-11-29 10:47:20 +00:00
|
|
|
from homeassistant.helpers.event import (
|
|
|
|
track_point_in_utc_time, track_utc_time_change)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.util import dt as dt_util
|
|
|
|
from homeassistant.util import location as location_util
|
2016-04-12 04:44:39 +00:00
|
|
|
from homeassistant.const import CONF_ELEVATION
|
2015-01-28 19:01:16 +00:00
|
|
|
|
2016-04-09 03:51:48 +00:00
|
|
|
REQUIREMENTS = ['astral==1.0']
|
2014-08-13 12:28:45 +00:00
|
|
|
DOMAIN = "sun"
|
2014-01-24 06:03:13 +00:00
|
|
|
ENTITY_ID = "sun.sun"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
STATE_ABOVE_HORIZON = "above_horizon"
|
|
|
|
STATE_BELOW_HORIZON = "below_horizon"
|
|
|
|
|
|
|
|
STATE_ATTR_NEXT_RISING = "next_rising"
|
|
|
|
STATE_ATTR_NEXT_SETTING = "next_setting"
|
2015-11-29 10:47:20 +00:00
|
|
|
STATE_ATTR_ELEVATION = "elevation"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-01-28 19:01:16 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-02-02 06:39:11 +00:00
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
def is_on(hass, entity_id=None):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""Test if the sun is currently up based on the statemachine."""
|
2014-01-24 06:03:13 +00:00
|
|
|
entity_id = entity_id or ENTITY_ID
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
return hass.states.is_state(entity_id, STATE_ABOVE_HORIZON)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-11-25 07:15:14 +00:00
|
|
|
def next_setting(hass, entity_id=None):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""Local datetime object of the next sun setting."""
|
2015-04-29 02:12:05 +00:00
|
|
|
utc_next = next_setting_utc(hass, entity_id)
|
|
|
|
|
|
|
|
return dt_util.as_local(utc_next) if utc_next else None
|
|
|
|
|
|
|
|
|
|
|
|
def next_setting_utc(hass, entity_id=None):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""UTC datetime object of the next sun setting."""
|
2014-11-25 07:15:14 +00:00
|
|
|
entity_id = entity_id or ENTITY_ID
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
state = hass.states.get(ENTITY_ID)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-02-03 05:20:37 +00:00
|
|
|
try:
|
2015-04-29 02:12:05 +00:00
|
|
|
return dt_util.str_to_datetime(
|
|
|
|
state.attributes[STATE_ATTR_NEXT_SETTING])
|
2014-02-03 05:20:37 +00:00
|
|
|
except (AttributeError, KeyError):
|
|
|
|
# AttributeError if state is None
|
|
|
|
# KeyError if STATE_ATTR_NEXT_SETTING does not exist
|
|
|
|
return None
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-11-25 07:15:14 +00:00
|
|
|
def next_rising(hass, entity_id=None):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""Local datetime object of the next sun rising."""
|
2015-04-29 02:12:05 +00:00
|
|
|
utc_next = next_rising_utc(hass, entity_id)
|
|
|
|
|
|
|
|
return dt_util.as_local(utc_next) if utc_next else None
|
|
|
|
|
|
|
|
|
|
|
|
def next_rising_utc(hass, entity_id=None):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""UTC datetime object of the next sun rising."""
|
2014-11-25 07:15:14 +00:00
|
|
|
entity_id = entity_id or ENTITY_ID
|
|
|
|
|
2014-04-24 07:40:45 +00:00
|
|
|
state = hass.states.get(ENTITY_ID)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2014-02-03 05:20:37 +00:00
|
|
|
try:
|
2015-04-29 02:12:05 +00:00
|
|
|
return dt_util.str_to_datetime(
|
|
|
|
state.attributes[STATE_ATTR_NEXT_RISING])
|
2014-02-03 05:20:37 +00:00
|
|
|
except (AttributeError, KeyError):
|
|
|
|
# AttributeError if state is None
|
|
|
|
# KeyError if STATE_ATTR_NEXT_RISING does not exist
|
|
|
|
return None
|
2013-12-11 08:07:30 +00:00
|
|
|
|
|
|
|
|
2014-08-13 12:28:45 +00:00
|
|
|
def setup(hass, config):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""Track the state of the sun in HA."""
|
2015-04-26 00:43:22 +00:00
|
|
|
if None in (hass.config.latitude, hass.config.longitude):
|
2015-07-17 02:49:54 +00:00
|
|
|
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
2015-04-26 00:43:22 +00:00
|
|
|
return False
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2015-07-17 02:49:54 +00:00
|
|
|
latitude = util.convert(hass.config.latitude, float)
|
|
|
|
longitude = util.convert(hass.config.longitude, float)
|
|
|
|
errors = []
|
|
|
|
|
|
|
|
if latitude is None:
|
|
|
|
errors.append('Latitude needs to be a decimal value')
|
|
|
|
elif -90 > latitude < 90:
|
|
|
|
errors.append('Latitude needs to be -90 .. 90')
|
|
|
|
|
|
|
|
if longitude is None:
|
|
|
|
errors.append('Longitude needs to be a decimal value')
|
|
|
|
elif -180 > longitude < 180:
|
|
|
|
errors.append('Longitude needs to be -180 .. 180')
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
_LOGGER.error('Invalid configuration received: %s', ", ".join(errors))
|
2015-04-26 00:43:22 +00:00
|
|
|
return False
|
2014-11-25 07:15:14 +00:00
|
|
|
|
2015-07-17 04:12:18 +00:00
|
|
|
platform_config = config.get(DOMAIN, {})
|
|
|
|
|
|
|
|
elevation = platform_config.get(CONF_ELEVATION)
|
2015-12-27 19:22:10 +00:00
|
|
|
if elevation is None:
|
|
|
|
elevation = location_util.elevation(latitude, longitude)
|
2015-07-17 04:12:18 +00:00
|
|
|
|
2015-12-27 19:22:10 +00:00
|
|
|
from astral import Location
|
2015-07-17 04:12:18 +00:00
|
|
|
|
2016-04-09 03:51:48 +00:00
|
|
|
location = Location(('', '', latitude, longitude,
|
|
|
|
hass.config.time_zone.zone, elevation))
|
2015-07-17 04:12:18 +00:00
|
|
|
|
|
|
|
sun = Sun(hass, location)
|
2015-04-29 02:12:05 +00:00
|
|
|
sun.point_in_time_listener(dt_util.utcnow())
|
|
|
|
|
2015-04-26 00:43:22 +00:00
|
|
|
return True
|
2014-11-25 07:15:14 +00:00
|
|
|
|
|
|
|
|
2015-04-26 00:43:22 +00:00
|
|
|
class Sun(Entity):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Representation of the Sun."""
|
2014-11-26 06:31:36 +00:00
|
|
|
|
2015-04-26 00:43:22 +00:00
|
|
|
entity_id = ENTITY_ID
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-07-17 04:12:18 +00:00
|
|
|
def __init__(self, hass, location):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the Sun."""
|
2015-07-17 02:49:54 +00:00
|
|
|
self.hass = hass
|
2015-07-17 04:12:18 +00:00
|
|
|
self.location = location
|
2015-04-26 00:43:22 +00:00
|
|
|
self._state = self.next_rising = self.next_setting = None
|
2015-11-29 10:47:20 +00:00
|
|
|
track_utc_time_change(hass, self.timer_update, second=30)
|
2015-04-26 00:43:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the name."""
|
2015-04-26 00:43:22 +00:00
|
|
|
return "Sun"
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-04-26 00:43:22 +00:00
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the state of the sun."""
|
2015-04-26 00:43:22 +00:00
|
|
|
if self.next_rising > self.next_setting:
|
|
|
|
return STATE_ABOVE_HORIZON
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-04-26 00:43:22 +00:00
|
|
|
return STATE_BELOW_HORIZON
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state_attributes(self):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Return the state attributes of the sun."""
|
2015-04-26 00:43:22 +00:00
|
|
|
return {
|
2015-11-29 10:47:20 +00:00
|
|
|
STATE_ATTR_NEXT_RISING:
|
|
|
|
dt_util.datetime_to_str(self.next_rising),
|
|
|
|
STATE_ATTR_NEXT_SETTING:
|
|
|
|
dt_util.datetime_to_str(self.next_setting),
|
|
|
|
STATE_ATTR_ELEVATION: round(self.solar_elevation, 2)
|
2013-12-11 08:07:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-26 00:43:22 +00:00
|
|
|
@property
|
|
|
|
def next_change(self):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""Datetime when the next change to the state is."""
|
2015-04-26 00:43:22 +00:00
|
|
|
return min(self.next_rising, self.next_setting)
|
2013-12-11 08:07:30 +00:00
|
|
|
|
2015-11-29 10:47:20 +00:00
|
|
|
@property
|
|
|
|
def solar_elevation(self):
|
2016-01-04 08:12:51 +00:00
|
|
|
"""Angle the sun is above the horizon."""
|
2015-11-29 10:47:20 +00:00
|
|
|
from astral import Astral
|
|
|
|
return Astral().solar_elevation(
|
|
|
|
dt_util.utcnow(),
|
|
|
|
self.location.latitude,
|
|
|
|
self.location.longitude)
|
|
|
|
|
2015-04-29 02:12:05 +00:00
|
|
|
def update_as_of(self, utc_point_in_time):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Calculate sun state at a point in UTC time."""
|
2015-07-17 02:49:54 +00:00
|
|
|
mod = -1
|
|
|
|
while True:
|
2015-07-17 04:12:18 +00:00
|
|
|
next_rising_dt = self.location.sunrise(
|
|
|
|
utc_point_in_time + timedelta(days=mod), local=False)
|
2015-07-17 02:49:54 +00:00
|
|
|
if next_rising_dt > utc_point_in_time:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
mod = -1
|
|
|
|
while True:
|
2015-07-17 04:12:18 +00:00
|
|
|
next_setting_dt = (self.location.sunset(
|
|
|
|
utc_point_in_time + timedelta(days=mod), local=False))
|
2015-07-17 02:49:54 +00:00
|
|
|
if next_setting_dt > utc_point_in_time:
|
|
|
|
break
|
|
|
|
mod += 1
|
|
|
|
|
|
|
|
self.next_rising = next_rising_dt
|
|
|
|
self.next_setting = next_setting_dt
|
2015-04-26 00:43:22 +00:00
|
|
|
|
|
|
|
def point_in_time_listener(self, now):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Called when the state of the sun has changed."""
|
2015-04-26 00:43:22 +00:00
|
|
|
self.update_as_of(now)
|
|
|
|
self.update_ha_state()
|
|
|
|
|
|
|
|
# Schedule next update at next_change+1 second so sun state has changed
|
2015-08-03 15:08:13 +00:00
|
|
|
track_point_in_utc_time(
|
|
|
|
self.hass, self.point_in_time_listener,
|
2015-04-29 02:12:05 +00:00
|
|
|
self.next_change + timedelta(seconds=1))
|
2015-11-29 10:47:20 +00:00
|
|
|
|
|
|
|
def timer_update(self, time):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Needed to update solar elevation."""
|
2015-11-29 10:47:20 +00:00
|
|
|
self.update_ha_state()
|