added timeout notification to mqtt sensor; => 0 value will deactivate function

This commit is contained in:
avollkopf 2023-01-22 19:53:54 +01:00
parent 47e3bcb529
commit 85490cebc8
3 changed files with 35 additions and 10 deletions

View file

@ -1,3 +1,3 @@
__version__ = "4.1.0.rc3"
__version__ = "4.1.0.rc4"
__codename__ = "Groundhog Day"

View file

@ -10,7 +10,7 @@ from cbpi.api.dataclasses import NotificationAction, NotificationType
cache = {}
@parameters([Property.Text(label="Key", configurable=True, description="Http Key"),
Property.Number(label="Timeout", configurable="True",unit="sec",description="Timeout in seconds to send notification (default:60)")
Property.Number(label="Timeout", configurable="True",unit="sec",description="Timeout in seconds to send notification (default:60 | deactivated: 0)")
])
class HTTPSensor(CBPiSensor):
def __init__(self, cbpi, id, props):
@ -37,17 +37,19 @@ class HTTPSensor(CBPiSensor):
In this example the code is executed every second
'''
while self.running is True:
currenttime=time.time()
if currenttime > self.nextchecktime and self.notificationsend == False:
await self.message()
self.notificationsend=True
if self.timeout !=0:
currenttime=time.time()
if currenttime > self.nextchecktime and self.notificationsend == False:
await self.message()
self.notificationsend=True
try:
cache_value = cache.pop(self.props.get("Key"), None)
if cache_value is not None:
self.value = float(cache_value)
self.push_update(self.value)
self.nextchecktime = currenttime + self.timeout
self.notificationsend = False
if self.timeout !=0:
self.nextchecktime = currenttime + self.timeout
self.notificationsend = False
except Exception as e:
logging.error(e)
pass

View file

@ -1,14 +1,17 @@
# -*- coding: utf-8 -*-
import asyncio
from cbpi.api.dataclasses import NotificationAction, NotificationType
from cbpi.api import parameters, Property, CBPiSensor
from cbpi.api import *
import logging
import json
import time
@parameters([Property.Text(label="Topic", configurable=True, description="MQTT Topic"),
Property.Text(label="PayloadDictionary", configurable=True, default_value="",
description="Where to find msg in payload, leave blank for raw payload")])
description="Where to find msg in payload, leave blank for raw payload"),
Property.Number(label="Timeout", configurable="True",unit="sec",
description="Timeout in seconds to send notification (default:60 | deactivated: 0)")])
class MQTTSensor(CBPiSensor):
def __init__(self, cbpi, id, props):
@ -19,6 +22,19 @@ class MQTTSensor(CBPiSensor):
self.payload_text = self.payload_text.split('.')
self.mqtt_task = self.cbpi.satellite.subcribe(self.Topic, self.on_message)
self.value: float = 999
self.timeout=int(self.props.get("Timeout", 60))
self.starttime = time.time()
self.notificationsend = False
self.nextchecktime=self.starttime+self.timeout
async def Confirm(self, **kwargs):
self.nextchecktime = time.time() + self.timeout
self.notificationsend = False
pass
async def message(self):
self.cbpi.notify("MQTTSensor Timeout", "Sensor " + str(self.Topic) + " did not respond", NotificationType.WARNING, action=[NotificationAction("OK", self.Confirm)])
pass
async def on_message(self, message):
val = json.loads(message)
@ -31,11 +47,18 @@ class MQTTSensor(CBPiSensor):
self.value = float(val)
self.log_data(self.value)
self.push_update(self.value)
if self.timeout !=0:
self.nextchecktime = time.time() + self.timeout
self.notificationsend = False
except Exception as e:
logging.info("MQTT Sensor Error {}".format(e))
async def run(self):
while self.running:
if self.timeout !=0:
if time.time() > self.nextchecktime and self.notificationsend == False:
await self.message()
self.notificationsend=True
await asyncio.sleep(1)
def get_state(self):