Merge pull request #425 from basvdheuvel/sun_sunup

Order sunrise and sunset according to current time
This commit is contained in:
tobi-wan-kenobi 2019-08-24 08:08:07 +02:00 committed by GitHub
commit eaad41c6a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,14 +15,22 @@ try:
import requests import requests
except ImportError: except ImportError:
pass pass
try:
from dateutil.tz import tzlocal
except ImportError:
pass
import datetime
import bumblebee.input import bumblebee.input
import bumblebee.output import bumblebee.output
import bumblebee.engine import bumblebee.engine
class Module(bumblebee.engine.Module): class Module(bumblebee.engine.Module):
def __init__(self, engine, config): def __init__(self, engine, config):
super(Module, self).__init__(engine, config, super(Module, self).__init__(
engine, config,
bumblebee.output.Widget(full_text=self.suntimes) bumblebee.output.Widget(full_text=self.suntimes)
) )
self.interval(3600) self.interval(3600)
@ -41,10 +49,16 @@ class Module(bumblebee.engine.Module):
def suntimes(self, _): def suntimes(self, _):
if self._sunset and self._sunrise: if self._sunset and self._sunrise:
return u"\u21A5{} \u21A7{}".format(self._sunrise.strftime('%H:%M'), self._sunset.strftime('%H:%M')) if self._isup:
return u"\u21A7{} \u21A5{}".format(
self._sunset.strftime('%H:%M'),
self._sunrise.strftime('%H:%M'))
return u"\u21A5{} \u21A7{}".format(self._sunrise.strftime('%H:%M'),
self._sunset.strftime('%H:%M'))
return "?" return "?"
def _calculate_times(self): def _calculate_times(self):
self._isup = False
try: try:
sun = Sun(self._lat, self._lon) sun = Sun(self._lat, self._lon)
except Exception: except Exception:
@ -52,15 +66,41 @@ class Module(bumblebee.engine.Module):
self._sunset = None self._sunset = None
return return
order_matters = True
try: try:
self._sunrise = sun.get_local_sunrise_time() self._sunrise = sun.get_local_sunrise_time()
except SunTimeException: except SunTimeException:
self._sunrise = 'no sunrise' self._sunrise = "no sunrise"
order_matters = False
try: try:
self._sunset = sun.get_local_sunset_time() self._sunset = sun.get_local_sunset_time()
except SunTimeException: except SunTimeException:
self._sunset = 'no sunset' self._sunset = "no sunset"
order_matters = False
if not order_matters:
return
now = datetime.datetime.now(tz=tzlocal())
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)
except SunTimeException:
self._sunrise = "no sunrise"
self._sunset = "no sunset"
elif now > self._sunrise:
tomorrow = (now + datetime.timedelta(days=1)).date()
try:
self._sunrise = sun.get_local_sunrise_time(tomorrow)
except SunTimeException:
self._sunrise = "no sunrise"
return
self._isup = True
def update(self, widgets): def update(self, widgets):
if not self._lat or not self._lon: if not self._lat or not self._lon: