2020-04-19 09:50:04 +02:00
|
|
|
# pylint: disable=C0111,R0903
|
|
|
|
|
|
|
|
"""Displays sunrise and sunset times
|
|
|
|
|
|
|
|
Requires the following python packages:
|
|
|
|
* requests
|
|
|
|
* suntime
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
* cpu.lat : Latitude of your location
|
|
|
|
* cpu.lon : Longitude of your location
|
|
|
|
"""
|
|
|
|
|
2020-04-19 10:04:01 +02:00
|
|
|
from suntime import Sun, SunTimeException
|
|
|
|
import requests
|
|
|
|
from dateutil.tz import tzlocal
|
2020-04-19 09:50:04 +02:00
|
|
|
|
|
|
|
import datetime
|
|
|
|
|
2020-04-19 10:04:01 +02:00
|
|
|
import core.module
|
|
|
|
import core.widget
|
|
|
|
import core.decorators
|
2020-04-19 09:50:04 +02:00
|
|
|
|
2020-04-19 10:04:01 +02:00
|
|
|
import util.location
|
2020-04-19 09:50:04 +02:00
|
|
|
|
2020-04-19 10:04:01 +02:00
|
|
|
class Module(core.module.Module):
|
|
|
|
@core.decorators.every(hours=1)
|
|
|
|
def __init__(self, config):
|
|
|
|
super().__init__(config, core.widget.Widget(self.suntimes))
|
|
|
|
|
|
|
|
self.__lat = self.parameter('lat', None)
|
|
|
|
self.__lon = self.parameter('lon', None)
|
|
|
|
|
|
|
|
if not self.__lat or not self.__lon:
|
|
|
|
self.__lat, self.__lon = util.location.coordinates()
|
2020-04-19 09:50:04 +02:00
|
|
|
|
|
|
|
def suntimes(self, _):
|
2020-04-19 10:04:01 +02:00
|
|
|
if self.__sunset and self.__sunrise:
|
|
|
|
if self.__isup:
|
2020-04-19 09:50:18 +02:00
|
|
|
return u'\u21A7{} \u21A5{}'.format(
|
2020-04-19 10:04:01 +02:00
|
|
|
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 'n/a'
|
|
|
|
|
|
|
|
def __calculate_times(self):
|
|
|
|
self.__isup = False
|
2020-04-19 09:50:04 +02:00
|
|
|
try:
|
2020-04-19 10:04:01 +02:00
|
|
|
if not self.__lat or not self.__lon:
|
2020-04-19 09:50:04 +02:00
|
|
|
raise()
|
2020-04-19 10:04:01 +02:00
|
|
|
sun = Sun(float(self.__lat), float(self.__lon))
|
2020-04-19 09:50:04 +02:00
|
|
|
except Exception:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunrise = None
|
|
|
|
self.__sunset = None
|
2020-04-19 09:50:04 +02:00
|
|
|
return
|
|
|
|
|
|
|
|
order_matters = True
|
|
|
|
|
|
|
|
try:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunrise = sun.get_local_sunrise_time()
|
2020-04-19 09:50:04 +02:00
|
|
|
except SunTimeException:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunrise = 'no sunrise'
|
2020-04-19 09:50:04 +02:00
|
|
|
order_matters = False
|
|
|
|
|
|
|
|
try:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunset = sun.get_local_sunset_time()
|
2020-04-19 09:50:04 +02:00
|
|
|
except SunTimeException:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunset = 'no sunset'
|
2020-04-19 09:50:04 +02:00
|
|
|
order_matters = False
|
|
|
|
|
|
|
|
if not order_matters:
|
|
|
|
return
|
|
|
|
|
|
|
|
now = datetime.datetime.now(tz=tzlocal())
|
2020-04-19 10:04:01 +02:00
|
|
|
if now > self.__sunset:
|
2020-04-19 09:50:04 +02:00
|
|
|
tomorrow = (now + datetime.timedelta(days=1)).date()
|
|
|
|
try:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunrise = sun.get_local_sunrise_time(tomorrow)
|
|
|
|
self.__sunset = sun.get_local_sunset_time(tomorrow)
|
2020-04-19 09:50:04 +02:00
|
|
|
except SunTimeException:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunrise = 'no sunrise'
|
|
|
|
self.__sunset = 'no sunset'
|
2020-04-19 09:50:04 +02:00
|
|
|
|
2020-04-19 10:04:01 +02:00
|
|
|
elif now > self.__sunrise:
|
2020-04-19 09:50:04 +02:00
|
|
|
tomorrow = (now + datetime.timedelta(days=1)).date()
|
|
|
|
try:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunrise = sun.get_local_sunrise_time(tomorrow)
|
2020-04-19 09:50:04 +02:00
|
|
|
except SunTimeException:
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__sunrise = 'no sunrise'
|
2020-04-19 09:50:04 +02:00
|
|
|
return
|
2020-04-19 10:04:01 +02:00
|
|
|
self.__isup = True
|
2020-04-19 09:50:04 +02:00
|
|
|
|
2020-04-19 10:04:01 +02:00
|
|
|
def update(self ):
|
|
|
|
if not self.__lat or not self.__lon:
|
|
|
|
self.__sunrise = None
|
|
|
|
self.__sunset = None
|
|
|
|
self.__calculate_times()
|
2020-04-19 09:50:04 +02:00
|
|
|
|
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|