[modules/sun] Minor refactoring

This commit is contained in:
tobi-wan-kenobi 2020-04-19 10:07:22 +02:00
parent 87fb82965f
commit 60de268850

View file

@ -28,11 +28,14 @@ class Module(core.module.Module):
def __init__(self, config): def __init__(self, config):
super().__init__(config, core.widget.Widget(self.suntimes)) super().__init__(config, core.widget.Widget(self.suntimes))
self.__lat = self.parameter('lat', None) lat = self.parameter('lat', None)
self.__lon = self.parameter('lon', None) lon = self.parameter('lon', None)
self.__sun = None
if not self.__lat or not self.__lon: if not lat or not lon:
self.__lat, self.__lon = util.location.coordinates() lat, lon = util.location.coordinates()
if lat and lon:
self.__sun = Sun(float(lat), float(lon))
def suntimes(self, _): def suntimes(self, _):
if self.__sunset and self.__sunrise: if self.__sunset and self.__sunrise:
@ -46,25 +49,17 @@ class Module(core.module.Module):
def __calculate_times(self): def __calculate_times(self):
self.__isup = False self.__isup = False
try:
if not self.__lat or not self.__lon:
raise()
sun = Sun(float(self.__lat), float(self.__lon))
except Exception:
self.__sunrise = None
self.__sunset = None
return
order_matters = True order_matters = True
try: try:
self.__sunrise = sun.get_local_sunrise_time() self.__sunrise = self.__sun.get_local_sunrise_time()
except SunTimeException: except SunTimeException:
self.__sunrise = 'no sunrise' self.__sunrise = 'no sunrise'
order_matters = False order_matters = False
try: try:
self.__sunset = sun.get_local_sunset_time() self.__sunset = self.__sun.get_local_sunset_time()
except SunTimeException: except SunTimeException:
self.__sunset = 'no sunset' self.__sunset = 'no sunset'
order_matters = False order_matters = False
@ -76,8 +71,8 @@ class Module(core.module.Module):
if now > self.__sunset: if now > self.__sunset:
tomorrow = (now + datetime.timedelta(days=1)).date() tomorrow = (now + datetime.timedelta(days=1)).date()
try: try:
self.__sunrise = sun.get_local_sunrise_time(tomorrow) self.__sunrise = self.__sun.get_local_sunrise_time(tomorrow)
self.__sunset = sun.get_local_sunset_time(tomorrow) self.__sunset = self.__sun.get_local_sunset_time(tomorrow)
except SunTimeException: except SunTimeException:
self.__sunrise = 'no sunrise' self.__sunrise = 'no sunrise'
self.__sunset = 'no sunset' self.__sunset = 'no sunset'
@ -85,16 +80,13 @@ class Module(core.module.Module):
elif now > self.__sunrise: elif now > self.__sunrise:
tomorrow = (now + datetime.timedelta(days=1)).date() tomorrow = (now + datetime.timedelta(days=1)).date()
try: try:
self.__sunrise = sun.get_local_sunrise_time(tomorrow) self.__sunrise = self.__sun.get_local_sunrise_time(tomorrow)
except SunTimeException: except SunTimeException:
self.__sunrise = 'no sunrise' self.__sunrise = 'no sunrise'
return return
self.__isup = True self.__isup = True
def update(self): def update(self):
if not self.__lat or not self.__lon:
self.__sunrise = None
self.__sunset = None
self.__calculate_times() self.__calculate_times()
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4