2018-05-04 03:24:35 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
from platypush.backend import Backend
|
|
|
|
from platypush.context import get_plugin
|
|
|
|
from platypush.message.event.weather import NewWeatherConditionEvent
|
|
|
|
|
2018-06-06 20:09:18 +02:00
|
|
|
|
2019-12-31 08:51:19 +01:00
|
|
|
class WeatherDarkskyBackend(Backend):
|
2018-06-26 00:36:01 +02:00
|
|
|
"""
|
|
|
|
Weather forecast backend - listens and propagates new weather events.
|
|
|
|
|
|
|
|
Triggers:
|
|
|
|
|
|
|
|
* :class:`platypush.message.event.weather.NewWeatherConditionEvent` when there is a weather condition update
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
2019-12-31 08:51:19 +01:00
|
|
|
* The :class:`platypush.plugins.weather.darksky.WeatherDarkskyPlugin` plugin configured
|
2018-06-26 00:36:01 +02:00
|
|
|
"""
|
|
|
|
|
2018-05-04 03:24:35 +02:00
|
|
|
def __init__(self, poll_seconds, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
|
|
|
self.poll_seconds = poll_seconds
|
|
|
|
self.latest_update = {}
|
|
|
|
|
2018-10-25 20:45:58 +02:00
|
|
|
def run(self):
|
|
|
|
super().run()
|
2019-12-31 08:51:19 +01:00
|
|
|
weather = get_plugin('weather.darksky')
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.info('Initialized weather forecast backend')
|
2018-05-04 03:24:35 +02:00
|
|
|
|
|
|
|
while not self.should_stop():
|
|
|
|
current_weather = weather.get_current_weather().output
|
|
|
|
del current_weather['time']
|
|
|
|
|
|
|
|
if current_weather != self.latest_update:
|
|
|
|
self.bus.post(NewWeatherConditionEvent(**current_weather))
|
|
|
|
|
|
|
|
self.latest_update = current_weather
|
|
|
|
time.sleep(self.poll_seconds)
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|