From 6dc1c22e20ae93d3017c52c6839c44aac820a955 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 18 Mar 2021 19:27:03 +0100 Subject: [PATCH] Fix[extension]: mqtt_sensor did not work with Mash_profile Error NoneType object as no attribute 'get' --- cbpi/extension/dummysensor/__init__.py | 26 ++++++++++++------------- cbpi/extension/mqtt_sensor/__init__.py | 27 +++++++++++++------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/cbpi/extension/dummysensor/__init__.py b/cbpi/extension/dummysensor/__init__.py index 1aab897..34c9c25 100644 --- a/cbpi/extension/dummysensor/__init__.py +++ b/cbpi/extension/dummysensor/__init__.py @@ -1,37 +1,35 @@ # -*- coding: utf-8 -*- import asyncio import random -import re -import random -from aiohttp import web -from cbpi.api import * + +from cbpi.api import parameters, CBPiSensor @parameters([]) class CustomSensor(CBPiSensor): - + def __init__(self, cbpi, id, props): super(CustomSensor, self).__init__(cbpi, id, props) self.value = 0 - async def run(self): - while self.running == True: - self.value = random.randint(10,100) + async def run(self): + while self.running: + self.value = random.randint(10, 100) self.log_data(self.value) - + self.push_update(self.value) await asyncio.sleep(1) + def get_state(self): return dict(value=self.value) def setup(cbpi): - ''' - This method is called by the server during startup + This method is called by the server during startup Here you need to register your plugins at the server - - :param cbpi: the cbpi core - :return: + + :param cbpi: the cbpi core + :return: ''' cbpi.plugin.register("CustomSensor", CustomSensor) diff --git a/cbpi/extension/mqtt_sensor/__init__.py b/cbpi/extension/mqtt_sensor/__init__.py index 4b6f389..481beb0 100644 --- a/cbpi/extension/mqtt_sensor/__init__.py +++ b/cbpi/extension/mqtt_sensor/__init__.py @@ -1,28 +1,27 @@ # -*- coding: utf-8 -*- import asyncio -import random -import re -from aiohttp import web -from cbpi.api import * + +from cbpi.api import parameters, Property, CBPiSensor @parameters([Property.Text(label="Topic", configurable=True)]) class MQTTSensor(CBPiSensor): - + async def on_message(self, message): try: - self.value = message + self.value = float(message) self.log_data(self.value) - self.push_update(message) + 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.value: int = 0 + async def run(self): - while self.running == True: + while self.running: await asyncio.sleep(1) def get_state(self): @@ -36,14 +35,14 @@ class MQTTSensor(CBPiSensor): except asyncio.CancelledError: pass -def setup(cbpi): +def setup(cbpi): ''' - This method is called by the server during startup + This method is called by the server during startup Here you need to register your plugins at the server - - :param cbpi: the cbpi core - :return: + + :param cbpi: the cbpi core + :return: ''' if cbpi.static_config.get("mqtt", False) is True: cbpi.plugin.register("MQTTSensor", MQTTSensor)