craftbeerpi4-pione/cbpi/extension/mashstep/__init__.py

225 lines
8.9 KiB
Python
Raw Normal View History

2021-02-07 13:08:13 +01:00
import asyncio
2021-03-15 19:54:22 +01:00
2021-03-15 19:55:59 +01:00
from cbpi.api import parameters, Property, action
from cbpi.api.step import StepResult, CBPiStep
2021-03-15 19:54:22 +01:00
from cbpi.api.timer import Timer
2021-03-30 21:00:17 +02:00
from datetime import datetime
import time
from voluptuous.schema_builder import message
from cbpi.api.dataclasses import NotificationAction, NotificationType
2021-02-07 13:08:13 +01:00
2021-03-15 12:59:55 +01:00
2021-03-15 19:54:22 +01:00
@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True),
2021-02-07 13:08:13 +01:00
Property.Number(label="Temp", configurable=True),
2021-02-10 07:38:55 +01:00
Property.Sensor(label="Sensor"),
2021-02-07 13:08:13 +01:00
Property.Kettle(label="Kettle")])
class MashStep(CBPiStep):
2021-03-15 19:54:22 +01:00
async def on_timer_done(self, timer):
2021-02-16 20:37:51 +01:00
self.summary = ""
await self.next()
2021-02-10 07:38:55 +01:00
2021-03-15 19:54:22 +01:00
async def on_timer_update(self, timer, seconds):
2021-02-16 20:37:51 +01:00
self.summary = Timer.format_time(seconds)
await self.push_update()
2021-02-10 07:38:55 +01:00
2021-02-16 20:37:51 +01:00
async def on_start(self):
2021-02-10 07:38:55 +01:00
if self.timer is None:
2021-03-15 19:54:22 +01:00
self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done)
2021-03-15 12:59:55 +01:00
if self.cbpi.kettle is not None:
await self.cbpi.kettle.set_target_temp(self.props.Kettle, int(self.props.Temp))
2021-02-16 20:37:51 +01:00
self.summary = "Waiting for Target Temp"
await self.push_update()
2021-02-10 07:38:55 +01:00
2021-02-16 20:37:51 +01:00
async def on_stop(self):
await self.timer.stop()
self.summary = ""
await self.push_update()
2021-02-10 07:38:55 +01:00
async def reset(self):
2021-02-27 20:09:19 +01:00
self.summary = ""
2021-03-15 19:54:22 +01:00
self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done)
2021-02-07 13:08:13 +01:00
2021-02-16 20:37:51 +01:00
async def run(self):
while self.running == True:
2021-02-10 07:38:55 +01:00
await asyncio.sleep(1)
2021-02-16 20:37:51 +01:00
sensor_value = self.get_sensor_value(self.props.Sensor)
2021-03-08 07:54:58 +01:00
if sensor_value.get("value") >= int(self.props.Temp) and self.timer.is_running is not True:
self.timer.start()
2021-03-08 07:54:58 +01:00
self.timer.is_running = True
2021-02-16 20:37:51 +01:00
return StepResult.DONE
2021-03-08 07:54:58 +01:00
2021-02-10 07:38:55 +01:00
@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True)])
class WaitStep(CBPiStep):
2021-02-20 12:59:23 +01:00
@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")
2021-03-15 19:54:22 +01:00
async def on_timer_done(self, timer):
2021-02-16 20:37:51 +01:00
self.summary = ""
await self.next()
2021-02-10 07:38:55 +01:00
2021-03-15 19:54:22 +01:00
async def on_timer_update(self, timer, seconds):
2021-02-16 20:37:51 +01:00
self.summary = Timer.format_time(seconds)
await self.push_update()
2021-02-10 07:38:55 +01:00
2021-02-16 20:37:51 +01:00
async def on_start(self):
2021-02-10 07:38:55 +01:00
if self.timer is None:
2021-03-15 19:54:22 +01:00
self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done)
2021-02-10 07:38:55 +01:00
self.timer.start()
2021-02-16 20:37:51 +01:00
async def on_stop(self):
await self.timer.stop()
self.summary = ""
await self.push_update()
2021-02-10 07:38:55 +01:00
async def reset(self):
2021-03-15 19:54:22 +01:00
self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done)
2021-02-10 07:38:55 +01:00
2021-02-16 20:37:51 +01:00
async def run(self):
while self.running == True:
2021-02-10 07:38:55 +01:00
await asyncio.sleep(1)
2021-02-16 20:37:51 +01:00
return StepResult.DONE
2021-02-10 07:38:55 +01:00
2021-03-15 19:54:22 +01:00
2021-02-12 00:03:18 +01:00
@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True),
2021-03-15 19:54:22 +01:00
Property.Actor(label="Actor")])
2021-02-10 07:38:55 +01:00
class ActorStep(CBPiStep):
2021-03-15 19:54:22 +01:00
async def on_timer_done(self, timer):
2021-02-16 20:37:51 +01:00
self.summary = ""
await self.next()
2021-02-10 07:38:55 +01:00
2021-03-15 19:54:22 +01:00
async def on_timer_update(self, timer, seconds):
2021-02-16 20:37:51 +01:00
self.summary = Timer.format_time(seconds)
await self.push_update()
2021-02-10 07:38:55 +01:00
2021-02-16 20:37:51 +01:00
async def on_start(self):
2021-02-10 07:38:55 +01:00
if self.timer is None:
2021-03-15 19:54:22 +01:00
self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done)
2021-02-10 07:38:55 +01:00
self.timer.start()
2021-02-16 20:37:51 +01:00
await self.actor_on(self.props.Actor)
2021-02-10 07:38:55 +01:00
2021-02-16 20:37:51 +01:00
async def on_stop(self):
await self.actor_off(self.props.Actor)
await self.timer.stop()
self.summary = ""
await self.push_update()
2021-03-15 19:54:22 +01:00
2021-02-16 20:37:51 +01:00
async def reset(self):
2021-03-15 19:54:22 +01:00
self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done)
2021-02-10 07:38:55 +01:00
2021-02-16 20:37:51 +01:00
async def run(self):
while self.running == True:
2021-02-10 07:38:55 +01:00
await asyncio.sleep(1)
2021-02-16 20:37:51 +01:00
return StepResult.DONE
2021-03-30 21:00:17 +02:00
@parameters([Property.Number(label="Timer", description="Time in Minutes", configurable=True),
Property.Number(label="Temp", description="Boil temperature", configurable=True),
Property.Sensor(label="Sensor"),
2021-03-30 21:00:17 +02:00
Property.Kettle(label="Kettle"),
Property.Select("First_Wort", options=["Yes","No"], description="First Wort Hop alert if set to Yes"),
Property.Number("Hop_1", configurable = True, description="First Hop alert (minutes before finish)"),
Property.Number("Hop_2", configurable=True, description="Second Hop alert (minutes before finish)"),
Property.Number("Hop_3", configurable=True, description="Third Hop alert (minutes before finish)"),
Property.Number("Hop_4", configurable=True, description="Fourth Hop alert (minutes before finish)"),
Property.Number("Hop_5", configurable=True, description="Fifth Hop alert (minutes before finish)"),
Property.Number("Hop_6", configurable=True, description="Sixth Hop alert (minutes before finish)")])
class BoilStep(CBPiStep):
2021-03-30 21:00:17 +02:00
@action("Start Timer", [])
async def start_timer(self):
if self.timer.is_running == None:
self.cbpi.notify(self.name, 'Timer started', NotificationType.INFO)
self.timer.start()
else:
self.cbpi.notify(self.name, 'Timer is already running', NotificationType.WARNING)
@action("Add 5 Minutes to Timer", [])
async def add_timer(self):
if self.timer.is_running != None:
self.cbpi.notify(self.name, '5 Minutes added', NotificationType.INFO)
await self.timer.add(300)
else:
self.cbpi.notify(self.name, 'Timer must be running to add time', NotificationType.WARNING)
2021-03-15 19:54:22 +01:00
async def on_timer_done(self, timer):
self.summary = ""
await self.next()
2021-03-15 19:54:22 +01:00
async def on_timer_update(self, timer, seconds):
self.summary = Timer.format_time(seconds)
2021-03-30 21:00:17 +02:00
self.remaining_seconds = seconds
await self.push_update()
2021-03-30 21:00:17 +02:00
async def check_hop_timer(self, number, value):
if value is not None and value != '' and self.hops_added[number-1] is not True:
if self.remaining_seconds != None and self.remaining_seconds <= (int(value) * 60 + 1):
self.hops_added[number-1]= True
self.cbpi.notify('Hop Alert', "Please add Hop %s" % number, NotificationType.INFO)
async def on_start(self):
if self.timer is None:
2021-03-15 19:54:22 +01:00
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()
2021-03-30 21:00:17 +02:00
self.first_wort_hop_flag = False
self.first_wort_hop=self.props.First_Wort
self.hops_added=["","","","","",""]
self.remaining_seconds = None
async def on_stop(self):
await self.timer.stop()
self.summary = ""
await self.push_update()
async def reset(self):
2021-03-15 19:54:22 +01:00
self.timer = Timer(int(self.props.Timer) * 60, on_update=self.on_timer_update, on_done=self.on_timer_done)
async def run(self):
2021-03-30 21:00:17 +02:00
if self.first_wort_hop_flag == False and self.first_wort_hop == "Yes":
self.first_wort_hop_flag = True
self.cbpi.notify('First Wort Hop Addition!', 'Please add hops for first wort', NotificationType.INFO)
while self.running == True:
await asyncio.sleep(1)
sensor_value = self.get_sensor_value(self.props.Sensor)
2021-03-30 21:00:17 +02:00
2021-03-08 07:54:58 +01:00
if sensor_value.get("value") >= int(self.props.Temp) and self.timer.is_running is not True:
self.timer.start()
2021-03-30 21:00:17 +02:00
estimated_completion_time = datetime.fromtimestamp(time.time()+ (int(self.props.Timer))*60)
self.cbpi.notify(self.name, 'Timer started. Estimated completion: {}'.format(estimated_completion_time.strftime("%H:%M")), NotificationType.INFO)
2021-03-08 07:54:58 +01:00
self.timer.is_running = True
2021-03-30 21:00:17 +02:00
else:
await self.check_hop_timer(1, self.props.Hop_1)
await self.check_hop_timer(2, self.props.Hop_2)
await self.check_hop_timer(3, self.props.Hop_3)
await self.check_hop_timer(4, self.props.Hop_4)
await self.check_hop_timer(5, self.props.Hop_5)
await self.check_hop_timer(6, self.props.Hop_6)
return StepResult.DONE
2021-03-15 19:54:22 +01:00
2021-02-07 13:08:13 +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:
2021-03-15 19:54:22 +01:00
'''
cbpi.plugin.register("BoilStep", BoilStep)
2021-02-10 07:38:55 +01:00
cbpi.plugin.register("WaitStep", WaitStep)
2021-02-07 13:08:13 +01:00
cbpi.plugin.register("MashStep", MashStep)
2021-02-16 20:37:51 +01:00
cbpi.plugin.register("ActorStep", ActorStep)