From 12d487bd3acd5dfc6d7f95765d0eb4e8ce7b9fef Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 15 Mar 2021 12:59:55 +0100 Subject: [PATCH 1/7] Fix: issuses #57 --- cbpi/extension/mashstep/__init__.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cbpi/extension/mashstep/__init__.py b/cbpi/extension/mashstep/__init__.py index b4547ef..b94fa70 100644 --- a/cbpi/extension/mashstep/__init__.py +++ b/cbpi/extension/mashstep/__init__.py @@ -5,15 +5,12 @@ from cbpi.api.timer import Timer from cbpi.api import * import logging + @parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True), Property.Number(label="Temp", configurable=True), Property.Sensor(label="Sensor"), Property.Kettle(label="Kettle")]) class MashStep(CBPiStep): - - - - async def on_timer_done(self,timer): self.summary = "" await self.next() @@ -25,6 +22,8 @@ class MashStep(CBPiStep): async def on_start(self): if self.timer is None: self.timer = Timer(int(self.props.Timer) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done) + if self.cbpi.kettle is not None: + await self.cbpi.kettle.set_target_temp(self.props.Kettle, int(self.props.Temp)) self.summary = "Waiting for Target Temp" await self.push_update() @@ -72,6 +71,8 @@ class WaitStep(CBPiStep): async def on_start(self): if self.timer is None: self.timer = Timer(int(self.props.Timer) * 60,on_update=self.on_timer_update, on_done=self.on_timer_done) + if self.cbpi.kettle is not None: + await self.cbpi.kettle.set_target_temp(self.props.Kettle, int(self.props.Temp)) self.timer.start() async def on_stop(self): From 93a07ac6132fa5118f34b42915143eb5d154cbeb Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 15 Mar 2021 13:02:35 +0100 Subject: [PATCH 2/7] Fix: issuses #56 --- cbpi/extension/gpioactor/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbpi/extension/gpioactor/__init__.py b/cbpi/extension/gpioactor/__init__.py index 8be45b9..c1e891d 100644 --- a/cbpi/extension/gpioactor/__init__.py +++ b/cbpi/extension/gpioactor/__init__.py @@ -105,7 +105,7 @@ class GPIOPWMActor(CBPiActor): return self.state async def run(self): - while self.runnin == True: + while self.running == True: await asyncio.sleep(1) From de38fbc80cf04e3168816d76388b033db970c450 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 15 Mar 2021 17:21:57 +0100 Subject: [PATCH 3/7] fix: error, i put the modification on bad class --- cbpi/extension/mashstep/__init__.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cbpi/extension/mashstep/__init__.py b/cbpi/extension/mashstep/__init__.py index b94fa70..700ebf4 100644 --- a/cbpi/extension/mashstep/__init__.py +++ b/cbpi/extension/mashstep/__init__.py @@ -71,8 +71,6 @@ class WaitStep(CBPiStep): async def on_start(self): if self.timer is None: self.timer = Timer(int(self.props.Timer) * 60,on_update=self.on_timer_update, on_done=self.on_timer_done) - if self.cbpi.kettle is not None: - await self.cbpi.kettle.set_target_temp(self.props.Kettle, int(self.props.Temp)) self.timer.start() async def on_stop(self): @@ -137,7 +135,8 @@ class BoilStep(CBPiStep): async def on_start(self): if self.timer is None: self.timer = Timer(int(self.props.Timer) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done) - + if self.cbpi.kettle is not None: + await self.cbpi.kettle.set_target_temp(self.props.Kettle, int(self.props.Temp)) self.summary = "Waiting for Target Temp" await self.push_update() From d8b083003d49a65dff31173e6dbc0284062fc0ea Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 15 Mar 2021 19:54:22 +0100 Subject: [PATCH 4/7] fix issues #55 --- cbpi/api/step.py | 47 +++++++++++---------- cbpi/extension/mashstep/__init__.py | 63 +++++++++++++---------------- 2 files changed, 53 insertions(+), 57 deletions(-) diff --git a/cbpi/api/step.py b/cbpi/api/step.py index 0f099ca..8e16b07 100644 --- a/cbpi/api/step.py +++ b/cbpi/api/step.py @@ -1,33 +1,32 @@ import asyncio -import json -import logging -import time -from abc import ABCMeta, abstractmethod -from enum import Enum +from abc import abstractmethod from cbpi.api.base import CBPiBase -from cbpi.api.config import ConfigType __all__ = ["StepResult", "StepState", "StepMove", "CBPiStep"] from enum import Enum + class StepResult(Enum): - STOP=1 - NEXT=2 - DONE=3 - ERROR=4 + STOP = 1 + NEXT = 2 + DONE = 3 + ERROR = 4 + class StepState(Enum): - INITIAL="I" - DONE="D" - ACTIVE="A" - ERROR="E" - STOP="S" + INITIAL = "I" + DONE = "D" + ACTIVE = "A" + ERROR = "E" + STOP = "S" + class StepMove(Enum): - UP=-1 - DOWN=1 + UP = -1 + DOWN = 1 + class CBPiStep(CBPiBase): @@ -40,21 +39,25 @@ class CBPiStep(CBPiBase): self.props = props self.cancel_reason: StepResult = None self.summary = "" + self.running: bool = False def _done(self, task): self._done_callback(self, task.result()) async def start(self): + self.running = True self.task = asyncio.create_task(self._run()) self.task.add_done_callback(self._done) - async def next(self): + async def next(self): + self.running = False self.cancel_reason = StepResult.NEXT self.task.cancel() await self.task - async def stop(self): + async def stop(self): try: + self.running = False self.cancel_reason = StepResult.STOP self.task.cancel() await self.task @@ -69,7 +72,7 @@ class CBPiStep(CBPiBase): async def save_props(self): await self.cbpi.step.save() - + async def push_update(self): self.cbpi.step.push_udpate() @@ -84,11 +87,11 @@ class CBPiStep(CBPiBase): await self.on_start() await self.run() self.cancel_reason = StepResult.DONE - except asyncio.CancelledError as e: + except asyncio.CancelledError as e: pass finally: await self.on_stop() - + return self.cancel_reason @abstractmethod diff --git a/cbpi/extension/mashstep/__init__.py b/cbpi/extension/mashstep/__init__.py index 700ebf4..972b6b9 100644 --- a/cbpi/extension/mashstep/__init__.py +++ b/cbpi/extension/mashstep/__init__.py @@ -1,27 +1,26 @@ - import asyncio -from cbpi.api.step import CBPiStep, StepResult -from cbpi.api.timer import Timer + from cbpi.api import * -import logging +from cbpi.api.step import StepResult +from cbpi.api.timer import Timer -@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True), +@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True), Property.Number(label="Temp", configurable=True), Property.Sensor(label="Sensor"), Property.Kettle(label="Kettle")]) class MashStep(CBPiStep): - async def on_timer_done(self,timer): + async def on_timer_done(self, timer): self.summary = "" await self.next() - async def on_timer_update(self,timer, seconds): + async def on_timer_update(self, timer, seconds): self.summary = Timer.format_time(seconds) await self.push_update() async def on_start(self): if self.timer is None: - self.timer = Timer(int(self.props.Timer) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) if self.cbpi.kettle is not None: await self.cbpi.kettle.set_target_temp(self.props.Kettle, int(self.props.Temp)) self.summary = "Waiting for Target Temp" @@ -34,7 +33,7 @@ class MashStep(CBPiStep): async def reset(self): self.summary = "" - self.timer = Timer(int(self.props.Timer) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) async def run(self): while True: @@ -45,7 +44,6 @@ class MashStep(CBPiStep): self.timer.is_running = True return StepResult.DONE - @parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True)]) class WaitStep(CBPiStep): @@ -53,24 +51,22 @@ class WaitStep(CBPiStep): @action(key="Custom Step Action", parameters=[]) async def hello(self, **kwargs): print("ACTION") - @action(key="Custom Step Action 2", parameters=[]) async def hello2(self, **kwargs): print("ACTION2") - - async def on_timer_done(self,timer): + async def on_timer_done(self, timer): self.summary = "" await self.next() - async def on_timer_update(self,timer, seconds): + async def on_timer_update(self, timer, seconds): self.summary = Timer.format_time(seconds) await self.push_update() async def on_start(self): if self.timer is None: - self.timer = Timer(int(self.props.Timer) * 60,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) self.timer.start() async def on_stop(self): @@ -79,27 +75,28 @@ class WaitStep(CBPiStep): await self.push_update() async def reset(self): - self.timer = Timer(int(self.props.Timer) * 60,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) async def run(self): while self.running == True: await asyncio.sleep(1) return StepResult.DONE + @parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True), - Property.Actor(label="Actor")]) + Property.Actor(label="Actor")]) class ActorStep(CBPiStep): - async def on_timer_done(self,timer): + async def on_timer_done(self, timer): self.summary = "" await self.next() - async def on_timer_update(self,timer, seconds): + async def on_timer_update(self, timer, seconds): self.summary = Timer.format_time(seconds) await self.push_update() async def on_start(self): if self.timer is None: - self.timer = Timer(int(self.props.Timer) * 60,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) self.timer.start() await self.actor_on(self.props.Actor) @@ -108,9 +105,9 @@ class ActorStep(CBPiStep): await self.timer.stop() self.summary = "" await self.push_update() - + async def reset(self): - self.timer = Timer(int(self.props.Timer) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) async def run(self): while self.running == True: @@ -118,23 +115,23 @@ class ActorStep(CBPiStep): return StepResult.DONE -@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True), +@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True), Property.Number(label="Temp", description="Boil temperature", configurable=True), Property.Sensor(label="Sensor"), Property.Kettle(label="Kettle")]) class BoilStep(CBPiStep): - async def on_timer_done(self,timer): + async def on_timer_done(self, timer): self.summary = "" await self.next() - async def on_timer_update(self,timer, seconds): + async def on_timer_update(self, timer, seconds): self.summary = Timer.format_time(seconds) await self.push_update() async def on_start(self): if self.timer is None: - self.timer = Timer(int(self.props.Timer) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) if self.cbpi.kettle is not None: await self.cbpi.kettle.set_target_temp(self.props.Kettle, int(self.props.Temp)) self.summary = "Waiting for Target Temp" @@ -146,11 +143,11 @@ class BoilStep(CBPiStep): await self.push_update() async def reset(self): - self.timer = Timer(int(self.props.Timer) *60 ,on_update=self.on_timer_update, on_done=self.on_timer_done) + self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) @action("Start Timer", []) async def star_timer(self): - + self.timer.start() async def run(self): @@ -162,6 +159,7 @@ class BoilStep(CBPiStep): self.timer.is_running = True return StepResult.DONE + def setup(cbpi): ''' This method is called by the server during startup @@ -169,14 +167,9 @@ def setup(cbpi): :param cbpi: the cbpi core :return: - ''' - + ''' + cbpi.plugin.register("BoilStep", BoilStep) cbpi.plugin.register("WaitStep", WaitStep) cbpi.plugin.register("MashStep", MashStep) cbpi.plugin.register("ActorStep", ActorStep) - - - - - From aabe76d15da34c94fc8a7f72e7b0f26942b20461 Mon Sep 17 00:00:00 2001 From: Martin Date: Mon, 15 Mar 2021 19:55:59 +0100 Subject: [PATCH 5/7] optimize import --- cbpi/extension/mashstep/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cbpi/extension/mashstep/__init__.py b/cbpi/extension/mashstep/__init__.py index 972b6b9..ace4e01 100644 --- a/cbpi/extension/mashstep/__init__.py +++ b/cbpi/extension/mashstep/__init__.py @@ -1,7 +1,7 @@ import asyncio -from cbpi.api import * -from cbpi.api.step import StepResult +from cbpi.api import parameters, Property, action +from cbpi.api.step import StepResult, CBPiStep from cbpi.api.timer import Timer From 919d6c8e93c748a0efbd4391c7df8c93175c89ca Mon Sep 17 00:00:00 2001 From: Martin <34959687+MrGunnery@users.noreply.github.com> Date: Mon, 15 Mar 2021 22:09:23 +0100 Subject: [PATCH 6/7] Update cbpi/extension/mashstep/__init__.py Co-authored-by: Marcel Schulz --- cbpi/extension/mashstep/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbpi/extension/mashstep/__init__.py b/cbpi/extension/mashstep/__init__.py index ace4e01..4cd76b1 100644 --- a/cbpi/extension/mashstep/__init__.py +++ b/cbpi/extension/mashstep/__init__.py @@ -36,7 +36,7 @@ class MashStep(CBPiStep): self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done) async def run(self): - while True: + while self.running == True: await asyncio.sleep(1) sensor_value = self.get_sensor_value(self.props.Sensor) if sensor_value.get("value") >= int(self.props.Temp) and self.timer.is_running is not True: From 6dc1c22e20ae93d3017c52c6839c44aac820a955 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 18 Mar 2021 19:27:03 +0100 Subject: [PATCH 7/7] 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)