diff --git a/requirements.txt b/requirements.txt index 0ece5484f3e0dbee5d7a71d403f1fbb806aa6a39..89f6cdfb51c2ca625faf151486c110adba08b7a6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ prometheus-client==0.7.1 pyserial==3.4 -pyserial-asyncio==0.4 \ No newline at end of file +pyserial-asyncio==0.4 +matrix-nio==0.24.0 diff --git a/tempermonitor/plugins/matrix.py b/tempermonitor/plugins/matrix.py new file mode 100644 index 0000000000000000000000000000000000000000..939188419d77cfbf6f989d248ffe823f95a0c2dd --- /dev/null +++ b/tempermonitor/plugins/matrix.py @@ -0,0 +1,56 @@ +import nio + +from . import Plugin + +SENSOR_TEMPERATURE_WARNING_BODY = """@vorstand, Container Temperatur zu hoch!!! {temperatures} Auslöser: {reason} +Aktuelle Temperaturen: +{alltemperatures} +""" + +class Matrix(Plugin): + """ + Handle matrix integration + """ + + def __init__(self, monitor): + self.monitor = monitor + self.config = self.monitor.config + + self.client = nio.AsyncClient(self.config['matrix']['server'], + self.config['matrix']['user']) + self.room_id = self.config['matrix']['room'] + self.client.join(self.room_id) + res = self.client.joined_rooms() + if self.room_id not in res.rooms: + self.client.join(self.room_id) + + async def send_message(self, body): + self.client.room_send(room_id=self.room_id, + message_type="m.room.message", content=body) + + async def temperature_warning(self, source, urgent=False, **kwargs): + if source == "tempdiff": + temperatures = "{name1}:{temp1}\n{name2}:{temp2}".format(**kwargs) + reason = "Differenztemperatur: {tempdiff}".format(**kwargs) + elif source == "singlehot": + temperatures = "{name}:{temp}".format(**kwargs) + reason = "Einzeltemperatur zu hoch" + + alltemperatures = '\n'.join([ + "{}: {}".format(sensor.name, sensor.temperature) if sensor.valid + else "{}: INVALID".format(sensor.name) + for sensor in self.monitor.sensors.values()]) + + await self.send_message(SENSOR_TEMPERATURE_WARNING_BODY.format( + temperatures=temperatures, + reason=reason, + alltemperatures=alltemperatures), + urgent=urgent + ) + + await self.send_mail( + SENSOR_TEMPERATURE_WARNING_BODY.format( + temperatures=temperatures, + reason=reason, + alltemperatures=alltemperatures), + )