Fix sun integration vulnerability to sudden large clock changes (#39335)

* Fix sun integration vulnerability to sudden large clock changes

* Fix update_sun_position as well
This commit is contained in:
Phil Bruckner 2020-08-28 13:09:43 -05:00 committed by GitHub
parent 57848cdf35
commit a99efcb5c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -104,7 +104,7 @@ class Sun(Entity):
if location == self.location: if location == self.location:
return return
self.location = location self.location = location
self.update_events(dt_util.utcnow()) self.update_events()
update_location(None) update_location(None)
self.hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, update_location) self.hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, update_location)
@ -148,8 +148,10 @@ class Sun(Entity):
return next_utc return next_utc
@callback @callback
def update_events(self, utc_point_in_time): def update_events(self, now=None):
"""Update the attributes containing solar events.""" """Update the attributes containing solar events."""
# Grab current time in case system clock changed since last time we ran.
utc_point_in_time = dt_util.utcnow()
self._next_change = utc_point_in_time + timedelta(days=400) self._next_change = utc_point_in_time + timedelta(days=400)
# Work our way around the solar cycle, figure out the next # Work our way around the solar cycle, figure out the next
@ -207,7 +209,7 @@ class Sun(Entity):
_LOGGER.debug( _LOGGER.debug(
"sun phase_update@%s: phase=%s", utc_point_in_time.isoformat(), self.phase "sun phase_update@%s: phase=%s", utc_point_in_time.isoformat(), self.phase
) )
self.update_sun_position(utc_point_in_time) self.update_sun_position()
# Set timer for the next solar event # Set timer for the next solar event
event.async_track_point_in_utc_time( event.async_track_point_in_utc_time(
@ -216,8 +218,10 @@ class Sun(Entity):
_LOGGER.debug("next time: %s", self._next_change.isoformat()) _LOGGER.debug("next time: %s", self._next_change.isoformat())
@callback @callback
def update_sun_position(self, utc_point_in_time): def update_sun_position(self, now=None):
"""Calculate the position of the sun.""" """Calculate the position of the sun."""
# Grab current time in case system clock changed since last time we ran.
utc_point_in_time = dt_util.utcnow()
self.solar_azimuth = round(self.location.solar_azimuth(utc_point_in_time), 2) self.solar_azimuth = round(self.location.solar_azimuth(utc_point_in_time), 2)
self.solar_elevation = round( self.solar_elevation = round(
self.location.solar_elevation(utc_point_in_time), 2 self.location.solar_elevation(utc_point_in_time), 2