diff --git a/cbpi/__init__.py b/cbpi/__init__.py index ecee302..023dc6d 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,3 +1,3 @@ -__version__ = "4.1.0.rc3" +__version__ = "4.1.0.rc4" __codename__ = "Groundhog Day" diff --git a/cbpi/extension/httpsensor/__init__.py b/cbpi/extension/httpsensor/__init__.py index 5b77b6a..7da1cdf 100644 --- a/cbpi/extension/httpsensor/__init__.py +++ b/cbpi/extension/httpsensor/__init__.py @@ -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 diff --git a/cbpi/extension/mqtt_sensor/__init__.py b/cbpi/extension/mqtt_sensor/__init__.py index e2d6e48..e121eb8 100644 --- a/cbpi/extension/mqtt_sensor/__init__.py +++ b/cbpi/extension/mqtt_sensor/__init__.py @@ -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):