From 5874b0bda5fa48afbe5bc2fb46df07fa0f81dd29 Mon Sep 17 00:00:00 2001 From: "Mich, Lukas" Date: Thu, 4 Mar 2021 13:06:57 +0100 Subject: [PATCH 1/3] fix HttpSensor --- cbpi/extension/httpsensor/__init__.py | 52 +++++++++++---------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/cbpi/extension/httpsensor/__init__.py b/cbpi/extension/httpsensor/__init__.py index b5a758c..f6f3859 100644 --- a/cbpi/extension/httpsensor/__init__.py +++ b/cbpi/extension/httpsensor/__init__.py @@ -9,42 +9,30 @@ import random cache = {} - +@parameters([Property.Text(label="Key", configurable=True, description="Http Key")]) class HTTPSensor(CBPiSensor): + def __init__(self, cbpi, id, props): + super(HTTPSensor, self).__init__(cbpi, id, props) + self.running = True - # Custom Properties which will can be configured by the user - - key = Property.Text(label="Key", configurable=True) - - def init(self): - super().init() - - self.state = True - - def get_state(self): - return self.state - - def get_value(self): - - return self.value - - def stop(self): - pass - - async def run(self, cbpi): - self.value = 0 - while True: + async def run(self): + ''' + This method is executed asynchronousely + In this example the code is executed every second + ''' + while self.running is True: + try: + cache_value = cache.pop(self.props.get("Key"), None) + if cache_value is not None: + self.value = cache_value + self.push_update(self.value) + except Exception as e: + pass await asyncio.sleep(1) - try: - value = cache.pop(self.key, None) - - if value is not None: - self.log_data(value) - await cbpi.bus.fire("sensor/%s/data" % self.id, value=value) - except Exception as e: - - pass + def get_state(self): + # return the current state of the sensor + return dict(value=self.value) class HTTPSensorEndpoint(CBPiExtension): From 7c948336348fd73492ccda4c7ac30b0fb6263933 Mon Sep 17 00:00:00 2001 From: Lukas <32687706+michluk@users.noreply.github.com> Date: Thu, 4 Mar 2021 14:49:10 +0100 Subject: [PATCH 2/3] Update __init__.py --- cbpi/extension/httpsensor/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbpi/extension/httpsensor/__init__.py b/cbpi/extension/httpsensor/__init__.py index f6f3859..65d62d6 100644 --- a/cbpi/extension/httpsensor/__init__.py +++ b/cbpi/extension/httpsensor/__init__.py @@ -24,7 +24,7 @@ class HTTPSensor(CBPiSensor): try: cache_value = cache.pop(self.props.get("Key"), None) if cache_value is not None: - self.value = cache_value + self.value = float(cache_value) self.push_update(self.value) except Exception as e: pass From 1ddfc56f15c32fea01267cfab335a08e3fd834af Mon Sep 17 00:00:00 2001 From: Lukas <32687706+michluk@users.noreply.github.com> Date: Thu, 4 Mar 2021 15:07:56 +0100 Subject: [PATCH 3/3] Update __init__.py --- cbpi/extension/httpsensor/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cbpi/extension/httpsensor/__init__.py b/cbpi/extension/httpsensor/__init__.py index 65d62d6..3b89c69 100644 --- a/cbpi/extension/httpsensor/__init__.py +++ b/cbpi/extension/httpsensor/__init__.py @@ -14,6 +14,7 @@ class HTTPSensor(CBPiSensor): def __init__(self, cbpi, id, props): super(HTTPSensor, self).__init__(cbpi, id, props) self.running = True + self.value = 0 async def run(self): '''