diff --git a/homeassistant/components/sun.py b/homeassistant/components/sun.py index 5982af6131f..b1e180c138a 100644 --- a/homeassistant/components/sun.py +++ b/homeassistant/components/sun.py @@ -25,6 +25,7 @@ STATE_BELOW_HORIZON = "below_horizon" STATE_ATTR_NEXT_RISING = "next_rising" STATE_ATTR_NEXT_SETTING = "next_setting" STATE_ATTR_ELEVATION = "elevation" +STATE_ATTR_AZIMUTH = "azimuth" _LOGGER = logging.getLogger(__name__) @@ -80,7 +81,7 @@ def next_rising_utc(hass, entity_id=None): def setup(hass, config): - """Track the state of the sun in HA.""" + """Track the state of the sun.""" if None in (hass.config.latitude, hass.config.longitude): _LOGGER.error("Latitude or longitude not set in Home Assistant config") return False @@ -126,10 +127,12 @@ class Sun(Entity): entity_id = ENTITY_ID def __init__(self, hass, location): - """Initialize the Sun.""" + """Initialize the sun.""" self.hass = hass self.location = location self._state = self.next_rising = self.next_setting = None + self.solar_elevation = self.solar_azimuth = 0 + track_utc_time_change(hass, self.timer_update, second=30) @property @@ -151,7 +154,8 @@ class Sun(Entity): return { STATE_ATTR_NEXT_RISING: self.next_rising.isoformat(), STATE_ATTR_NEXT_SETTING: self.next_setting.isoformat(), - STATE_ATTR_ELEVATION: round(self.solar_elevation, 2) + STATE_ATTR_ELEVATION: round(self.solar_elevation, 2), + STATE_ATTR_AZIMUTH: round(self.solar_azimuth, 2) } @property @@ -159,15 +163,6 @@ class Sun(Entity): """Datetime when the next change to the state is.""" return min(self.next_rising, self.next_setting) - @property - def solar_elevation(self): - """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): """Calculate sun state at a point in UTC time.""" mod = -1 @@ -189,6 +184,20 @@ class Sun(Entity): self.next_rising = next_rising_dt self.next_setting = next_setting_dt + def update_sun_position(self, utc_point_in_time): + """Calculate the position of the sun.""" + from astral import Astral + + self.solar_azimuth = Astral().solar_azimuth( + utc_point_in_time, + self.location.latitude, + self.location.longitude) + + self.solar_elevation = Astral().solar_elevation( + utc_point_in_time, + self.location.latitude, + self.location.longitude) + def point_in_time_listener(self, now): """Called when the state of the sun has changed.""" self.update_as_of(now) @@ -200,5 +209,6 @@ class Sun(Entity): self.next_change + timedelta(seconds=1)) def timer_update(self, time): - """Needed to update solar elevation.""" + """Needed to update solar elevation and azimuth.""" + self.update_sun_position(time) self.update_ha_state()