Merge pull request #59 from MrGunnery/Fix_some_issues

Fix some issues
This commit is contained in:
Manuel83 2021-03-21 10:27:11 +01:00 committed by GitHub
commit fb2793eb85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 86 additions and 93 deletions

View file

@ -1,33 +1,32 @@
import asyncio import asyncio
import json from abc import abstractmethod
import logging
import time
from abc import ABCMeta, abstractmethod
from enum import Enum
from cbpi.api.base import CBPiBase from cbpi.api.base import CBPiBase
from cbpi.api.config import ConfigType
__all__ = ["StepResult", "StepState", "StepMove", "CBPiStep"] __all__ = ["StepResult", "StepState", "StepMove", "CBPiStep"]
from enum import Enum from enum import Enum
class StepResult(Enum): class StepResult(Enum):
STOP=1 STOP = 1
NEXT=2 NEXT = 2
DONE=3 DONE = 3
ERROR=4 ERROR = 4
class StepState(Enum): class StepState(Enum):
INITIAL="I" INITIAL = "I"
DONE="D" DONE = "D"
ACTIVE="A" ACTIVE = "A"
ERROR="E" ERROR = "E"
STOP="S" STOP = "S"
class StepMove(Enum): class StepMove(Enum):
UP=-1 UP = -1
DOWN=1 DOWN = 1
class CBPiStep(CBPiBase): class CBPiStep(CBPiBase):
@ -40,21 +39,25 @@ class CBPiStep(CBPiBase):
self.props = props self.props = props
self.cancel_reason: StepResult = None self.cancel_reason: StepResult = None
self.summary = "" self.summary = ""
self.running: bool = False
def _done(self, task): def _done(self, task):
self._done_callback(self, task.result()) self._done_callback(self, task.result())
async def start(self): async def start(self):
self.running = True
self.task = asyncio.create_task(self._run()) self.task = asyncio.create_task(self._run())
self.task.add_done_callback(self._done) self.task.add_done_callback(self._done)
async def next(self): async def next(self):
self.running = False
self.cancel_reason = StepResult.NEXT self.cancel_reason = StepResult.NEXT
self.task.cancel() self.task.cancel()
await self.task await self.task
async def stop(self): async def stop(self):
try: try:
self.running = False
self.cancel_reason = StepResult.STOP self.cancel_reason = StepResult.STOP
self.task.cancel() self.task.cancel()
await self.task await self.task
@ -69,7 +72,7 @@ class CBPiStep(CBPiBase):
async def save_props(self): async def save_props(self):
await self.cbpi.step.save() await self.cbpi.step.save()
async def push_update(self): async def push_update(self):
self.cbpi.step.push_udpate() self.cbpi.step.push_udpate()
@ -84,11 +87,11 @@ class CBPiStep(CBPiBase):
await self.on_start() await self.on_start()
await self.run() await self.run()
self.cancel_reason = StepResult.DONE self.cancel_reason = StepResult.DONE
except asyncio.CancelledError as e: except asyncio.CancelledError as e:
pass pass
finally: finally:
await self.on_stop() await self.on_stop()
return self.cancel_reason return self.cancel_reason
@abstractmethod @abstractmethod

View file

@ -1,37 +1,35 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import asyncio import asyncio
import random import random
import re
import random from cbpi.api import parameters, CBPiSensor
from aiohttp import web
from cbpi.api import *
@parameters([]) @parameters([])
class CustomSensor(CBPiSensor): class CustomSensor(CBPiSensor):
def __init__(self, cbpi, id, props): def __init__(self, cbpi, id, props):
super(CustomSensor, self).__init__(cbpi, id, props) super(CustomSensor, self).__init__(cbpi, id, props)
self.value = 0 self.value = 0
async def run(self):
while self.running == True: async def run(self):
self.value = random.randint(10,100) while self.running:
self.value = random.randint(10, 100)
self.log_data(self.value) self.log_data(self.value)
self.push_update(self.value) self.push_update(self.value)
await asyncio.sleep(1) await asyncio.sleep(1)
def get_state(self): def get_state(self):
return dict(value=self.value) return dict(value=self.value)
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 Here you need to register your plugins at the server
:param cbpi: the cbpi core :param cbpi: the cbpi core
:return: :return:
''' '''
cbpi.plugin.register("CustomSensor", CustomSensor) cbpi.plugin.register("CustomSensor", CustomSensor)

View file

@ -105,7 +105,7 @@ class GPIOPWMActor(CBPiActor):
return self.state return self.state
async def run(self): async def run(self):
while self.runnin == True: while self.running == True:
await asyncio.sleep(1) await asyncio.sleep(1)

View file

@ -1,30 +1,28 @@
import asyncio import asyncio
from cbpi.api.step import CBPiStep, StepResult
from cbpi.api.timer import Timer
from cbpi.api import *
import logging
@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True), from cbpi.api import parameters, Property, action
from cbpi.api.step import StepResult, CBPiStep
from cbpi.api.timer import Timer
@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True),
Property.Number(label="Temp", configurable=True), Property.Number(label="Temp", configurable=True),
Property.Sensor(label="Sensor"), Property.Sensor(label="Sensor"),
Property.Kettle(label="Kettle")]) Property.Kettle(label="Kettle")])
class MashStep(CBPiStep): class MashStep(CBPiStep):
async def on_timer_done(self, timer):
async def on_timer_done(self,timer):
self.summary = "" self.summary = ""
await self.next() 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) self.summary = Timer.format_time(seconds)
await self.push_update() await self.push_update()
async def on_start(self): async def on_start(self):
if self.timer is None: 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" self.summary = "Waiting for Target Temp"
await self.push_update() await self.push_update()
@ -35,10 +33,10 @@ class MashStep(CBPiStep):
async def reset(self): async def reset(self):
self.summary = "" 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): async def run(self):
while True: while self.running == True:
await asyncio.sleep(1) await asyncio.sleep(1)
sensor_value = self.get_sensor_value(self.props.Sensor) 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: if sensor_value.get("value") >= int(self.props.Temp) and self.timer.is_running is not True:
@ -46,7 +44,6 @@ class MashStep(CBPiStep):
self.timer.is_running = True self.timer.is_running = True
return StepResult.DONE 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)])
class WaitStep(CBPiStep): class WaitStep(CBPiStep):
@ -54,24 +51,22 @@ class WaitStep(CBPiStep):
@action(key="Custom Step Action", parameters=[]) @action(key="Custom Step Action", parameters=[])
async def hello(self, **kwargs): async def hello(self, **kwargs):
print("ACTION") print("ACTION")
@action(key="Custom Step Action 2", parameters=[]) @action(key="Custom Step Action 2", parameters=[])
async def hello2(self, **kwargs): async def hello2(self, **kwargs):
print("ACTION2") print("ACTION2")
async def on_timer_done(self,timer): async def on_timer_done(self, timer):
self.summary = "" self.summary = ""
await self.next() 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) self.summary = Timer.format_time(seconds)
await self.push_update() await self.push_update()
async def on_start(self): async def on_start(self):
if self.timer is None: 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() self.timer.start()
async def on_stop(self): async def on_stop(self):
@ -80,27 +75,28 @@ class WaitStep(CBPiStep):
await self.push_update() await self.push_update()
async def reset(self): 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): async def run(self):
while self.running == True: while self.running == True:
await asyncio.sleep(1) await asyncio.sleep(1)
return StepResult.DONE 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.Actor(label="Actor")]) Property.Actor(label="Actor")])
class ActorStep(CBPiStep): class ActorStep(CBPiStep):
async def on_timer_done(self,timer): async def on_timer_done(self, timer):
self.summary = "" self.summary = ""
await self.next() 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) self.summary = Timer.format_time(seconds)
await self.push_update() await self.push_update()
async def on_start(self): async def on_start(self):
if self.timer is None: 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() self.timer.start()
await self.actor_on(self.props.Actor) await self.actor_on(self.props.Actor)
@ -109,9 +105,9 @@ class ActorStep(CBPiStep):
await self.timer.stop() await self.timer.stop()
self.summary = "" self.summary = ""
await self.push_update() await self.push_update()
async def reset(self): 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): async def run(self):
while self.running == True: while self.running == True:
@ -119,24 +115,25 @@ class ActorStep(CBPiStep):
return StepResult.DONE 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.Number(label="Temp", description="Boil temperature", configurable=True),
Property.Sensor(label="Sensor"), Property.Sensor(label="Sensor"),
Property.Kettle(label="Kettle")]) Property.Kettle(label="Kettle")])
class BoilStep(CBPiStep): class BoilStep(CBPiStep):
async def on_timer_done(self,timer): async def on_timer_done(self, timer):
self.summary = "" self.summary = ""
await self.next() 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) self.summary = Timer.format_time(seconds)
await self.push_update() await self.push_update()
async def on_start(self): async def on_start(self):
if self.timer is None: 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" self.summary = "Waiting for Target Temp"
await self.push_update() await self.push_update()
@ -146,11 +143,11 @@ class BoilStep(CBPiStep):
await self.push_update() await self.push_update()
async def reset(self): 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", []) @action("Start Timer", [])
async def star_timer(self): async def star_timer(self):
self.timer.start() self.timer.start()
async def run(self): async def run(self):
@ -162,6 +159,7 @@ class BoilStep(CBPiStep):
self.timer.is_running = True self.timer.is_running = True
return StepResult.DONE return StepResult.DONE
def setup(cbpi): def setup(cbpi):
''' '''
This method is called by the server during startup This method is called by the server during startup
@ -169,14 +167,9 @@ def setup(cbpi):
:param cbpi: the cbpi core :param cbpi: the cbpi core
:return: :return:
''' '''
cbpi.plugin.register("BoilStep", BoilStep) cbpi.plugin.register("BoilStep", BoilStep)
cbpi.plugin.register("WaitStep", WaitStep) cbpi.plugin.register("WaitStep", WaitStep)
cbpi.plugin.register("MashStep", MashStep) cbpi.plugin.register("MashStep", MashStep)
cbpi.plugin.register("ActorStep", ActorStep) cbpi.plugin.register("ActorStep", ActorStep)

View file

@ -1,28 +1,27 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import asyncio import asyncio
import random
import re from cbpi.api import parameters, Property, CBPiSensor
from aiohttp import web
from cbpi.api import *
@parameters([Property.Text(label="Topic", configurable=True)]) @parameters([Property.Text(label="Topic", configurable=True)])
class MQTTSensor(CBPiSensor): class MQTTSensor(CBPiSensor):
async def on_message(self, message): async def on_message(self, message):
try: try:
self.value = message self.value = float(message)
self.log_data(self.value) self.log_data(self.value)
self.push_update(message) self.push_update(self.value)
except Exception as e: except Exception as e:
print(e) print(e)
def __init__(self, cbpi, id, props): def __init__(self, cbpi, id, props):
super(MQTTSensor, self).__init__(cbpi, id, props) super(MQTTSensor, self).__init__(cbpi, id, props)
self.mqtt_task = self.cbpi.satellite.subcribe(self.props.Topic, self.on_message) self.mqtt_task = self.cbpi.satellite.subcribe(self.props.Topic, self.on_message)
self.value: int = 0
async def run(self): async def run(self):
while self.running == True: while self.running:
await asyncio.sleep(1) await asyncio.sleep(1)
def get_state(self): def get_state(self):
@ -36,14 +35,14 @@ class MQTTSensor(CBPiSensor):
except asyncio.CancelledError: except asyncio.CancelledError:
pass 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 Here you need to register your plugins at the server
:param cbpi: the cbpi core :param cbpi: the cbpi core
:return: :return:
''' '''
if cbpi.static_config.get("mqtt", False) is True: if cbpi.static_config.get("mqtt", False) is True:
cbpi.plugin.register("MQTTSensor", MQTTSensor) cbpi.plugin.register("MQTTSensor", MQTTSensor)