fix HttpSensor

This commit is contained in:
Mich, Lukas 2021-03-04 13:06:57 +01:00
parent 441e235d1f
commit 5874b0bda5

View file

@ -9,42 +9,30 @@ import random
cache = {} cache = {}
@parameters([Property.Text(label="Key", configurable=True, description="Http Key")])
class HTTPSensor(CBPiSensor): 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 async def run(self):
'''
key = Property.Text(label="Key", configurable=True) This method is executed asynchronousely
In this example the code is executed every second
def init(self): '''
super().init() while self.running is True:
try:
self.state = True cache_value = cache.pop(self.props.get("Key"), None)
if cache_value is not None:
def get_state(self): self.value = cache_value
return self.state self.push_update(self.value)
except Exception as e:
def get_value(self): pass
return self.value
def stop(self):
pass
async def run(self, cbpi):
self.value = 0
while True:
await asyncio.sleep(1) await asyncio.sleep(1)
try: def get_state(self):
value = cache.pop(self.key, None) # return the current state of the sensor
return dict(value=self.value)
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
class HTTPSensorEndpoint(CBPiExtension): class HTTPSensorEndpoint(CBPiExtension):