From 0e379a2cb52c3aadea8a8c2bfebfe693ca338681 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 21 Sep 2013 21:37:36 -0700 Subject: [PATCH] Fixed a bug where sun set and rise times would not be updated. --- homeassistant/observer/WeatherWatcher.py | 36 +++++++++++++++++------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/homeassistant/observer/WeatherWatcher.py b/homeassistant/observer/WeatherWatcher.py index c83039b2f26..102ae3cfb72 100644 --- a/homeassistant/observer/WeatherWatcher.py +++ b/homeassistant/observer/WeatherWatcher.py @@ -19,10 +19,6 @@ class WeatherWatcher(object): self.eventbus = eventbus self.statemachine = statemachine - self.observer = ephem.Observer() - self.observer.lat = self.config.get('common','latitude') - self.observer.long = self.config.get('common','longitude') - self.sun = ephem.Sun() statemachine.add_category(STATE_CATEGORY_SUN, SUN_STATE_BELOW_HORIZON) @@ -30,20 +26,31 @@ class WeatherWatcher(object): self._update_sun_state() - def next_sun_rising(self): + def next_sun_rising(self, observer=None): """ Returns a datetime object that points at the next sun rising. """ - return ephem.localtime(self.observer.next_rising(self.sun)) + + if observer is None: + observer = self._get_observer() + + return ephem.localtime(observer.next_rising(self.sun)) - def next_sun_setting(self): + def next_sun_setting(self, observer=None): """ Returns a datetime object that points at the next sun setting. """ - return ephem.localtime(self.observer.next_setting(self.sun)) + + if observer is None: + observer = self._get_observer() + + return ephem.localtime(observer.next_setting(self.sun)) def _update_sun_state(self, now=None): """ Updates the state of the sun and schedules when to check next. """ - next_rising = self.next_sun_rising() - next_setting = self.next_sun_setting() + + observer = self._get_observer() + + next_rising = self.next_sun_rising(observer) + next_setting = self.next_sun_setting(observer) if next_rising > next_setting: new_state = SUN_STATE_ABOVE_HORIZON @@ -59,3 +66,12 @@ class WeatherWatcher(object): # +10 seconds to be sure that the change has occured track_time_change(self.eventbus, self._update_sun_state, point_in_time=next_change + timedelta(seconds=10)) + + + def _get_observer(self): + """ Creates an observer representing the location from the config and the current time. """ + observer = ephem.Observer() + observer.lat = self.config.get('common','latitude') + observer.long = self.config.get('common','longitude') + + return observer