mirror of
https://github.com/PiBrewing/craftbeerpi4.git
synced 2024-11-09 17:07:43 +01:00
added timeout notification to mqtt sensor; => 0 value will deactivate function
This commit is contained in:
parent
47e3bcb529
commit
85490cebc8
3 changed files with 35 additions and 10 deletions
|
@ -1,3 +1,3 @@
|
||||||
__version__ = "4.1.0.rc3"
|
__version__ = "4.1.0.rc4"
|
||||||
__codename__ = "Groundhog Day"
|
__codename__ = "Groundhog Day"
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ from cbpi.api.dataclasses import NotificationAction, NotificationType
|
||||||
cache = {}
|
cache = {}
|
||||||
|
|
||||||
@parameters([Property.Text(label="Key", configurable=True, description="Http Key"),
|
@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):
|
class HTTPSensor(CBPiSensor):
|
||||||
def __init__(self, cbpi, id, props):
|
def __init__(self, cbpi, id, props):
|
||||||
|
@ -37,17 +37,19 @@ class HTTPSensor(CBPiSensor):
|
||||||
In this example the code is executed every second
|
In this example the code is executed every second
|
||||||
'''
|
'''
|
||||||
while self.running is True:
|
while self.running is True:
|
||||||
currenttime=time.time()
|
if self.timeout !=0:
|
||||||
if currenttime > self.nextchecktime and self.notificationsend == False:
|
currenttime=time.time()
|
||||||
await self.message()
|
if currenttime > self.nextchecktime and self.notificationsend == False:
|
||||||
self.notificationsend=True
|
await self.message()
|
||||||
|
self.notificationsend=True
|
||||||
try:
|
try:
|
||||||
cache_value = cache.pop(self.props.get("Key"), None)
|
cache_value = cache.pop(self.props.get("Key"), None)
|
||||||
if cache_value is not None:
|
if cache_value is not None:
|
||||||
self.value = float(cache_value)
|
self.value = float(cache_value)
|
||||||
self.push_update(self.value)
|
self.push_update(self.value)
|
||||||
self.nextchecktime = currenttime + self.timeout
|
if self.timeout !=0:
|
||||||
self.notificationsend = False
|
self.nextchecktime = currenttime + self.timeout
|
||||||
|
self.notificationsend = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
pass
|
pass
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from cbpi.api.dataclasses import NotificationAction, NotificationType
|
||||||
from cbpi.api import parameters, Property, CBPiSensor
|
from cbpi.api import parameters, Property, CBPiSensor
|
||||||
from cbpi.api import *
|
from cbpi.api import *
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
import time
|
||||||
|
|
||||||
@parameters([Property.Text(label="Topic", configurable=True, description="MQTT Topic"),
|
@parameters([Property.Text(label="Topic", configurable=True, description="MQTT Topic"),
|
||||||
Property.Text(label="PayloadDictionary", configurable=True, default_value="",
|
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):
|
class MQTTSensor(CBPiSensor):
|
||||||
|
|
||||||
def __init__(self, cbpi, id, props):
|
def __init__(self, cbpi, id, props):
|
||||||
|
@ -19,6 +22,19 @@ class MQTTSensor(CBPiSensor):
|
||||||
self.payload_text = self.payload_text.split('.')
|
self.payload_text = self.payload_text.split('.')
|
||||||
self.mqtt_task = self.cbpi.satellite.subcribe(self.Topic, self.on_message)
|
self.mqtt_task = self.cbpi.satellite.subcribe(self.Topic, self.on_message)
|
||||||
self.value: float = 999
|
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):
|
async def on_message(self, message):
|
||||||
val = json.loads(message)
|
val = json.loads(message)
|
||||||
|
@ -31,11 +47,18 @@ class MQTTSensor(CBPiSensor):
|
||||||
self.value = float(val)
|
self.value = float(val)
|
||||||
self.log_data(self.value)
|
self.log_data(self.value)
|
||||||
self.push_update(self.value)
|
self.push_update(self.value)
|
||||||
|
if self.timeout !=0:
|
||||||
|
self.nextchecktime = time.time() + self.timeout
|
||||||
|
self.notificationsend = False
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.info("MQTT Sensor Error {}".format(e))
|
logging.info("MQTT Sensor Error {}".format(e))
|
||||||
|
|
||||||
async def run(self):
|
async def run(self):
|
||||||
while self.running:
|
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)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
def get_state(self):
|
def get_state(self):
|
||||||
|
|
Loading…
Reference in a new issue