From a99efcb5c2ee4f31c73e23f8133b3cc46d7dffa1 Mon Sep 17 00:00:00 2001 From: Phil Bruckner Date: Fri, 28 Aug 2020 13:09:43 -0500 Subject: [PATCH] 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 --- homeassistant/components/sun/__init__.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sun/__init__.py b/homeassistant/components/sun/__init__.py index fe89413f4d5..5fb777bd325 100644 --- a/homeassistant/components/sun/__init__.py +++ b/homeassistant/components/sun/__init__.py @@ -104,7 +104,7 @@ class Sun(Entity): if location == self.location: return self.location = location - self.update_events(dt_util.utcnow()) + self.update_events() update_location(None) self.hass.bus.async_listen(EVENT_CORE_CONFIG_UPDATE, update_location) @@ -148,8 +148,10 @@ class Sun(Entity): return next_utc @callback - def update_events(self, utc_point_in_time): + def update_events(self, now=None): """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) # Work our way around the solar cycle, figure out the next @@ -207,7 +209,7 @@ class Sun(Entity): _LOGGER.debug( "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 event.async_track_point_in_utc_time( @@ -216,8 +218,10 @@ class Sun(Entity): _LOGGER.debug("next time: %s", self._next_change.isoformat()) @callback - def update_sun_position(self, utc_point_in_time): + def update_sun_position(self, now=None): """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_elevation = round( self.location.solar_elevation(utc_point_in_time), 2