diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 3cf324b..128db3d 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.0.49" +__version__ = "4.0.0.50" diff --git a/cbpi/extension/mqtt_actor/__init__.py b/cbpi/extension/mqtt_actor/__init__.py index 3b3453d..0c48ec6 100644 --- a/cbpi/extension/mqtt_actor/__init__.py +++ b/cbpi/extension/mqtt_actor/__init__.py @@ -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 diff --git a/cbpi/extension/mqtt_sensor/__init__.py b/cbpi/extension/mqtt_sensor/__init__.py index 481beb0..e5267a0 100644 --- a/cbpi/extension/mqtt_sensor/__init__.py +++ b/cbpi/extension/mqtt_sensor/__init__.py @@ -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)