mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Add azimuth (#1951)
* Add azimuth * Place elevation and azimuth together in update part
This commit is contained in:
parent
eaa8e5f29d
commit
fed2584d8a
@ -25,6 +25,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"
|
STATE_ATTR_ELEVATION = "elevation"
|
||||||
|
STATE_ATTR_AZIMUTH = "azimuth"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -80,7 +81,7 @@ def next_rising_utc(hass, entity_id=None):
|
|||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
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):
|
if None in (hass.config.latitude, hass.config.longitude):
|
||||||
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
_LOGGER.error("Latitude or longitude not set in Home Assistant config")
|
||||||
return False
|
return False
|
||||||
@ -126,10 +127,12 @@ class Sun(Entity):
|
|||||||
entity_id = ENTITY_ID
|
entity_id = ENTITY_ID
|
||||||
|
|
||||||
def __init__(self, hass, location):
|
def __init__(self, hass, location):
|
||||||
"""Initialize the Sun."""
|
"""Initialize the sun."""
|
||||||
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
|
||||||
|
self.solar_elevation = self.solar_azimuth = 0
|
||||||
|
|
||||||
track_utc_time_change(hass, self.timer_update, second=30)
|
track_utc_time_change(hass, self.timer_update, second=30)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -151,7 +154,8 @@ class Sun(Entity):
|
|||||||
return {
|
return {
|
||||||
STATE_ATTR_NEXT_RISING: self.next_rising.isoformat(),
|
STATE_ATTR_NEXT_RISING: self.next_rising.isoformat(),
|
||||||
STATE_ATTR_NEXT_SETTING: self.next_setting.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
|
@property
|
||||||
@ -159,15 +163,6 @@ class Sun(Entity):
|
|||||||
"""Datetime when the next change to the state is."""
|
"""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):
|
|
||||||
"""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
|
||||||
@ -189,6 +184,20 @@ class Sun(Entity):
|
|||||||
self.next_rising = next_rising_dt
|
self.next_rising = next_rising_dt
|
||||||
self.next_setting = next_setting_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):
|
def point_in_time_listener(self, now):
|
||||||
"""Called when the state of the sun has changed."""
|
"""Called when the state of the sun has changed."""
|
||||||
self.update_as_of(now)
|
self.update_as_of(now)
|
||||||
@ -200,5 +209,6 @@ class Sun(Entity):
|
|||||||
self.next_change + timedelta(seconds=1))
|
self.next_change + timedelta(seconds=1))
|
||||||
|
|
||||||
def timer_update(self, time):
|
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()
|
self.update_ha_state()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user