[modules/sun] Minor refactoring
This commit is contained in:
parent
87fb82965f
commit
60de268850
1 changed files with 13 additions and 21 deletions
|
@ -28,11 +28,14 @@ class Module(core.module.Module):
|
|||
def __init__(self, config):
|
||||
super().__init__(config, core.widget.Widget(self.suntimes))
|
||||
|
||||
self.__lat = self.parameter('lat', None)
|
||||
self.__lon = self.parameter('lon', None)
|
||||
lat = self.parameter('lat', None)
|
||||
lon = self.parameter('lon', None)
|
||||
self.__sun = None
|
||||
|
||||
if not self.__lat or not self.__lon:
|
||||
self.__lat, self.__lon = util.location.coordinates()
|
||||
if not lat or not lon:
|
||||
lat, lon = util.location.coordinates()
|
||||
if lat and lon:
|
||||
self.__sun = Sun(float(lat), float(lon))
|
||||
|
||||
def suntimes(self, _):
|
||||
if self.__sunset and self.__sunrise:
|
||||
|
@ -46,25 +49,17 @@ class Module(core.module.Module):
|
|||
|
||||
def __calculate_times(self):
|
||||
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
|
||||
|
||||
try:
|
||||
self.__sunrise = sun.get_local_sunrise_time()
|
||||
self.__sunrise = self.__sun.get_local_sunrise_time()
|
||||
except SunTimeException:
|
||||
self.__sunrise = 'no sunrise'
|
||||
order_matters = False
|
||||
|
||||
try:
|
||||
self.__sunset = sun.get_local_sunset_time()
|
||||
self.__sunset = self.__sun.get_local_sunset_time()
|
||||
except SunTimeException:
|
||||
self.__sunset = 'no sunset'
|
||||
order_matters = False
|
||||
|
@ -76,8 +71,8 @@ class Module(core.module.Module):
|
|||
if now > self.__sunset:
|
||||
tomorrow = (now + datetime.timedelta(days=1)).date()
|
||||
try:
|
||||
self.__sunrise = sun.get_local_sunrise_time(tomorrow)
|
||||
self.__sunset = sun.get_local_sunset_time(tomorrow)
|
||||
self.__sunrise = self.__sun.get_local_sunrise_time(tomorrow)
|
||||
self.__sunset = self.__sun.get_local_sunset_time(tomorrow)
|
||||
except SunTimeException:
|
||||
self.__sunrise = 'no sunrise'
|
||||
self.__sunset = 'no sunset'
|
||||
|
@ -85,16 +80,13 @@ class Module(core.module.Module):
|
|||
elif now > self.__sunrise:
|
||||
tomorrow = (now + datetime.timedelta(days=1)).date()
|
||||
try:
|
||||
self.__sunrise = sun.get_local_sunrise_time(tomorrow)
|
||||
self.__sunrise = self.__sun.get_local_sunrise_time(tomorrow)
|
||||
except SunTimeException:
|
||||
self.__sunrise = 'no sunrise'
|
||||
return
|
||||
self.__isup = True
|
||||
|
||||
def update(self):
|
||||
if not self.__lat or not self.__lon:
|
||||
self.__sunrise = None
|
||||
self.__sunset = None
|
||||
self.__calculate_times()
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
Loading…
Reference in a new issue