59 lines
2.0 KiB
Python
59 lines
2.0 KiB
Python
from typing import Type
|
|
from mautrix.util.config import BaseProxyConfig, ConfigUpdateHelper
|
|
from maubot import Plugin, MessageEvent
|
|
from maubot.handlers import command
|
|
from datetime import datetime
|
|
import aiohttp
|
|
import asyncio
|
|
import json
|
|
|
|
|
|
class Config(BaseProxyConfig):
|
|
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
|
helper.copy("auth_url")
|
|
helper.copy("token")
|
|
helper.copy("ecowatt_url")
|
|
|
|
|
|
class Rte(Plugin):
|
|
async def start(self) -> None:
|
|
self.config.load_and_update()
|
|
|
|
|
|
@command.new(name="rte", help="")
|
|
async def get_eco_watt(self, evt: MessageEvent) -> None:
|
|
await evt.mark_read()
|
|
resp = await self.http.post(self.config["auth_url"], headers={'Authorization' : 'Basic ' + self.config["token"]})
|
|
ans = await resp.text()
|
|
objet = json.loads(ans)
|
|
bearer = objet["access_token"]
|
|
|
|
resp = await self.http.get(self.config["ecowatt_url"], headers={'Authorization' : 'Bearer ' + bearer})
|
|
ans = await resp.text()
|
|
objet = json.loads(ans)
|
|
signals = objet["signals"]
|
|
for signal in signals:
|
|
heure = datetime.fromisoformat(signal["jour"])
|
|
if signal["dvalue"] == 1:
|
|
await evt.respond(heure.strftime("%d/%m/%Y") + " : " + signal["message"])
|
|
|
|
else :
|
|
values = signal["values"]
|
|
respText = heure.strftime("%d/%m/%Y") + " : " + signal["message"] + " : <br />"
|
|
for value in values :
|
|
if value["hvalue"] == 3: #Rouge
|
|
respText += str(value["pas"]) + "H : " + '🟥<br />'
|
|
elif value["hvalue"] == 2: #Orange
|
|
respText += str(value["pas"]) + "H : " + '🟧<br />'
|
|
else : #vert
|
|
respText += str(value["pas"]) + "H : " + '🟩<br />'
|
|
|
|
|
|
await evt.respond(respText, allow_html=True)
|
|
@classmethod
|
|
def get_config_class(cls) -> Type[BaseProxyConfig]:
|
|
return Config
|
|
|
|
|
|
|