bumblebee-status/modules/contrib/sun.py

100 lines
2.8 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
(if none of those are set, location is determined automatically via location APIs)
contributed by `lonesomebyte537 <https://github.com/lonesomebyte537>`_ - many thanks!
2020-04-19 09:50:04 +02:00
"""
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
lat = self.parameter("lat", None)
lon = self.parameter("lon", None)
2020-04-19 10:07:22 +02:00
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:
return "\u21A7{} \u21A5{}".format(
self.__sunset.strftime("%H:%M"), self.__sunrise.strftime("%H:%M")
)
return "\u21A5{} \u21A7{}".format(
self.__sunrise.strftime("%H:%M"), self.__sunset.strftime("%H:%M")
)
return "n/a"
2020-04-19 10:04:01 +02:00
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:
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:
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:
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:
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
2020-04-19 09:50:04 +02:00
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4