From 2eab0456511d73a98c2fcc997c55936548518f08 Mon Sep 17 00:00:00 2001 From: Jarno Rankinen Date: Sun, 20 Nov 2022 14:13:51 +0200 Subject: [PATCH] Added module elspot, shows Nord Pool electricity spot price information. Currently only price of the passing hour is implemented. --- Dockerfile | 1 + modules/elspot.py | 80 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 modules/elspot.py diff --git a/Dockerfile b/Dockerfile index d6d778e..2670c0d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,6 +9,7 @@ RUN pip install pipenv && \ pip install pipfile-requirements RUN pipfile2req Pipfile > requirements.txt RUN pip install -r requirements.txt +RUN pip install nordpool COPY bot.py *.json *.pickle /bot/ COPY config config diff --git a/modules/elspot.py b/modules/elspot.py new file mode 100644 index 0000000..3c8a6d2 --- /dev/null +++ b/modules/elspot.py @@ -0,0 +1,80 @@ +from modules.common.module import BotModule +from nordpool import elspot +from datetime import datetime, date +import pytz + + +class MatrixModule(BotModule): + def __init__(self,name): + super().__init__(name) + self.country = None + self.timezone = None + self.price_history = [] + self.updated = None + + def get_settings(self): + data = super().get_settings() + data['country'] = self.country + data['timezone'] = self.timezone + data['price_history'] = self.price_history + data['updated'] = self.updated + return data + + def set_settings(self,data): + super().set_settings(data) + if data.get('country'): + self.country = data['country'] + if data.get('timezone'): + self.timezone = data['timezone'] + if data.get('price_history'): + self.price_history = data['price_history'] + if data.get('updated'): + self.updated = data['updated'] + + async def matrix_message(self, bot, room, event): + args = event.body.split() + args.pop(0) + + if len(args) >= 2 and args[0] == 'config': + if args[1] == 'country': + if len(args) >= 3: + bot.must_be_owner(event) + self.country = args[2] + self.logger.info(f"Changing country to {args[2]}") + bot.save_settings() + await bot.send_text(room, f"Elspot will fetch prices for {args[2]}", event) + return + else: + await bot.send_text(room, f"A country must be specified. Currently configured {self.country}", event) + return + elif args[1] == 'timezone': + if len(args) >= 3: + bot.must_be_owner(event) + self.logger.info(f"Changing timezone to {args[2]}") + self.timezone = args[2] + bot.save_settings() + await bot.send_text(room, f"Elspot will show times in {args[2]}", event) + return + else: + await bot.send_text(room, f"A timezone must be specified. Currently configured {self.timezone}", event) + return + elif len(args) == 1 and args[0] == 'config': + await bot.send_text(room, f"Currently configured to show prices for {self.country} in {self.timezone} timezone", event) + + if len(args) == 0 and self.country is not None and self.timezone is not None: + self.logger.info(f"Fetching current price for {self.country}, using timezone {self.timezone}") + UTC = pytz.timezone('UTC') + LOCAL = pytz.timezone(self.timezone) + local_today = datetime.now(LOCAL) + utc_today = datetime.now(UTC) + utc_todaystr = date.strftime(utc_today, "%Y-%m-%d") + utc_curhour = date.strftime(utc_today, "%H") + prices_spot = elspot.Prices() + prices = prices_spot.hourly(areas=[self.country],end_date=utc_todaystr) + price = str(prices['areas']['FI']['values'][int(utc_curhour)]['value']/1000) + " € / kWh" + msg = f"Nord Pool spot price at {local_today}: {price}" + await bot.send_text(room, msg, event) + + + def help(self): + return 'Electricity spot prices'