Some MQTT updates

- MQTT actor power change also if actor is off
- payload Filter for MQTT sensor
This commit is contained in:
avollkopf 2021-11-22 17:33:46 +01:00
parent 01386ef0da
commit 71897c8276
3 changed files with 32 additions and 14 deletions

View file

@ -1 +1 @@
__version__ = "4.0.0.49"
__version__ = "4.0.0.50"

View file

@ -38,7 +38,7 @@ class MQTTActor(CBPiActor):
async def off(self):
self.state = False
await self.cbpi.satellite.publish(self.topic, json.dumps(
{"state": "off"}), True)
{"state": "off", "power": self.power}), True)
pass
async def run(self):
@ -49,7 +49,11 @@ class MQTTActor(CBPiActor):
return self.state
async def set_power(self, power):
await self.on(power)
self.power = power
if self.state == True:
await self.on(power)
else:
await self.off()
await self.cbpi.actor.actor_update(self.id,power)
pass

View file

@ -2,24 +2,38 @@
import asyncio
from cbpi.api import parameters, Property, CBPiSensor
from cbpi.api import *
import logging
import json
@parameters([Property.Text(label="Topic", configurable=True)])
@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")])
class MQTTSensor(CBPiSensor):
async def on_message(self, message):
try:
self.value = float(message)
self.log_data(self.value)
self.push_update(self.value)
except Exception as e:
print(e)
def __init__(self, cbpi, id, props):
super(MQTTSensor, self).__init__(cbpi, id, props)
self.mqtt_task = self.cbpi.satellite.subcribe(self.props.Topic, self.on_message)
self.Topic = self.props.get("Topic", None)
self.payload_text = self.props.get("PayloadDictionary", None)
if self.payload_text != None:
self.payload_text = self.payload_text.split('.')
self.mqtt_task = self.cbpi.satellite.subcribe(self.Topic, self.on_message)
self.value: int = 0
async def on_message(self, message):
val = json.loads(message)
try:
if self.payload_text is not None:
for key in self.payload_text:
val = val.get(key, None)
if isinstance(val, (int, float, str)):
self.value = float(val)
self.log_data(self.value)
self.push_update(self.value)
except Exception as e:
logging.info("MQTT Sensor Error {}".format(e))
async def run(self):
while self.running:
await asyncio.sleep(1)