bumblebee-status/modules/contrib/sun.py

93 lines
2.7 KiB
Python
Raw Normal View History

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, theme):
super().__init__(config, theme, core.widget.Widget(self.suntimes))
2020-04-19 10:04:01 +02:00
2020-04-19 10:07:22 +02:00
lat = self.parameter('lat', None)
lon = self.parameter('lon', None)
self.__sun = None
2020-04-19 10:04:01 +02:00
2020-04-19 10:07:22 +02:00
if not lat or not lon:
lat, lon = util.location.coordinates()
if lat and lon:
self.__sun = Sun(float(lat), float(lon))
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
order_matters = True
try:
2020-04-19 10:07:22 +02:00
self.__sunrise = self.__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:07:22 +02:00
self.__sunset = self.__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:07:22 +02:00
self.__sunrise = self.__sun.get_local_sunrise_time(tomorrow)
self.__sunset = self.__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:07:22 +02:00
self.__sunrise = self.__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:07:22 +02:00
def update(self):
2020-04-19 10:04:01 +02:00
self.__calculate_times()
2020-04-19 09:50:04 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4