Add elevation attribute
This commit is contained in:
parent
05978ad88d
commit
41a0f2c198
1 changed files with 26 additions and 5 deletions
|
@ -12,12 +12,14 @@ import urllib
|
||||||
|
|
||||||
import homeassistant.util as util
|
import homeassistant.util as util
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
from homeassistant.helpers.event import track_point_in_utc_time
|
from homeassistant.helpers.event import (
|
||||||
|
track_point_in_utc_time, track_utc_time_change)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
|
||||||
REQUIREMENTS = ['astral==0.8.1']
|
REQUIREMENTS = ['astral==0.8.1']
|
||||||
DOMAIN = "sun"
|
DOMAIN = "sun"
|
||||||
ENTITY_ID = "sun.sun"
|
ENTITY_ID = "sun.sun"
|
||||||
|
ENTITY_ID_ELEVATION = "sun.elevation"
|
||||||
|
|
||||||
CONF_ELEVATION = 'elevation'
|
CONF_ELEVATION = 'elevation'
|
||||||
|
|
||||||
|
@ -26,6 +28,7 @@ STATE_BELOW_HORIZON = "below_horizon"
|
||||||
|
|
||||||
STATE_ATTR_NEXT_RISING = "next_rising"
|
STATE_ATTR_NEXT_RISING = "next_rising"
|
||||||
STATE_ATTR_NEXT_SETTING = "next_setting"
|
STATE_ATTR_NEXT_SETTING = "next_setting"
|
||||||
|
STATE_ATTR_ELEVATION = "elevation"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -139,11 +142,13 @@ class Sun(Entity):
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self.location = location
|
self.location = location
|
||||||
self._state = self.next_rising = self.next_setting = None
|
self._state = self.next_rising = self.next_setting = None
|
||||||
|
track_utc_time_change(hass, self.timer_update, second=30)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
""" We trigger updates ourselves after sunset/sunrise """
|
""" We trigger updates ourselves after sunset/sunrise,
|
||||||
return False
|
but sun angle requires poll """
|
||||||
|
return True
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
@ -159,8 +164,11 @@ class Sun(Entity):
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
return {
|
return {
|
||||||
STATE_ATTR_NEXT_RISING: dt_util.datetime_to_str(self.next_rising),
|
STATE_ATTR_NEXT_RISING:
|
||||||
STATE_ATTR_NEXT_SETTING: dt_util.datetime_to_str(self.next_setting)
|
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
@ -168,6 +176,15 @@ class Sun(Entity):
|
||||||
""" Returns the datetime when the next change to the state is. """
|
""" Returns the datetime when the next change to the state is. """
|
||||||
return min(self.next_rising, self.next_setting)
|
return min(self.next_rising, self.next_setting)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def solar_elevation(self):
|
||||||
|
""" Returns the angle the sun is above the horizon"""
|
||||||
|
from astral import Astral
|
||||||
|
return Astral().solar_elevation(
|
||||||
|
dt_util.utcnow(),
|
||||||
|
self.location.latitude,
|
||||||
|
self.location.longitude)
|
||||||
|
|
||||||
def update_as_of(self, utc_point_in_time):
|
def update_as_of(self, utc_point_in_time):
|
||||||
""" Calculate sun state at a point in UTC time. """
|
""" Calculate sun state at a point in UTC time. """
|
||||||
mod = -1
|
mod = -1
|
||||||
|
@ -198,3 +215,7 @@ class Sun(Entity):
|
||||||
track_point_in_utc_time(
|
track_point_in_utc_time(
|
||||||
self.hass, self.point_in_time_listener,
|
self.hass, self.point_in_time_listener,
|
||||||
self.next_change + timedelta(seconds=1))
|
self.next_change + timedelta(seconds=1))
|
||||||
|
|
||||||
|
def timer_update(self, time):
|
||||||
|
""" Needed to update solar elevation. """
|
||||||
|
self.update_ha_state()
|
||||||
|
|
Loading…
Add table
Reference in a new issue