craftbeerpi4-pione/core/extension/dummylogic/__init__.py

82 lines
1.7 KiB
Python
Raw Normal View History

2018-11-18 15:40:10 +01:00
import asyncio
2018-12-29 00:27:19 +01:00
from cbpi_api import *
2018-11-18 15:40:10 +01:00
class CustomLogic(CBPiKettleLogic):
2018-11-18 23:09:17 +01:00
test = Property.Number(label="Test")
2018-11-18 15:40:10 +01:00
running = True
2018-11-29 21:59:08 +01:00
2018-11-30 23:27:11 +01:00
async def wait_for_event(self, topic, callback=None, timeout=None):
2018-11-29 21:59:08 +01:00
future_obj = self.cbpi.app.loop.create_future()
2018-11-30 23:27:11 +01:00
async def default_callback(id, **kwargs):
future_obj.set_result("HELLO")
2018-11-29 21:59:08 +01:00
2018-11-30 23:27:11 +01:00
if callback is None:
self.cbpi.bus.register(topic=topic, method=default_callback)
else:
callback.future = future_obj
self.cbpi.bus.register(topic=topic, method=callback)
2018-11-29 21:59:08 +01:00
if timeout is not None:
try:
2018-12-29 00:27:19 +01:00
2018-11-30 23:27:11 +01:00
await asyncio.wait_for(future_obj, timeout=timeout)
2018-12-29 00:27:19 +01:00
2018-11-29 21:59:08 +01:00
return future_obj.result()
except asyncio.TimeoutError:
2018-12-29 00:27:19 +01:00
pass
2018-11-29 21:59:08 +01:00
else:
2018-12-29 00:27:19 +01:00
2018-11-29 21:59:08 +01:00
await future_obj
return future_obj.result()
2018-11-18 15:40:10 +01:00
async def run(self):
2018-11-30 23:27:11 +01:00
2018-12-03 22:16:03 +01:00
async def my_callback(value, **kwargs):
2018-11-30 23:27:11 +01:00
2018-12-03 22:16:03 +01:00
if value == 5:
self.cbpi.bus.unregister(my_callback)
kwargs["future"].set_result("AMAZING")
else:
2018-12-29 00:27:19 +01:00
pass
2018-12-03 22:16:03 +01:00
result = await self.wait_for_event("sensor/1", callback=my_callback)
2018-12-29 00:27:19 +01:00
2018-11-29 21:59:08 +01:00
2018-11-18 15:40:10 +01:00
2018-11-29 21:59:08 +01:00
'''
while self.running:
2018-11-18 23:09:17 +01:00
print("RUN", self.test)
value = await self.cbpi.sensor.get_value(1)
print(value)
if value >= 10:
break
2018-11-18 15:40:10 +01:00
await asyncio.sleep(1)
2018-11-29 21:59:08 +01:00
'''
2018-12-29 00:27:19 +01:00
2018-11-18 23:09:17 +01:00
2018-11-18 15:40:10 +01:00
def setup(cbpi):
'''
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:
'''
cbpi.plugin.register("CustomKettleLogic", CustomLogic)