From d8eda552864564b907ad207ce1b52dee09cfdf75 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Thu, 15 Jul 2021 07:16:26 +0200 Subject: [PATCH] Automatic config update in case of new config parameters, the new extension can update the config automatically. --- cbpi/config/config.json | 16 +++ cbpi/controller/upload_controller.py | 96 ++++++++++++++-- cbpi/extension/ConfigUpdate/__init__.py | 145 ++++++++++++++++++++++++ cbpi/extension/ConfigUpdate/config.yaml | 3 + 4 files changed, 251 insertions(+), 9 deletions(-) create mode 100644 cbpi/extension/ConfigUpdate/__init__.py create mode 100644 cbpi/extension/ConfigUpdate/config.yaml diff --git a/cbpi/config/config.json b/cbpi/config/config.json index 486163c..56f98c5 100644 --- a/cbpi/config/config.json +++ b/cbpi/config/config.json @@ -20,6 +20,22 @@ "type": "kettle", "value": "" }, + "AddMashInStep": { + "description": "Add MashIn Step automatically if not defined in recipe", + "name": "AddMashInStep", + "options": [ + { + "label": "Yes", + "value": "Yes" + }, + { + "label": "No", + "value": "No" + } + ], + "type": "select", + "value": "Yes" + }, "RECIPE_CREATION_PATH": { "description": "API path to creation plugin. Default: empty", "name": "RECIPE_CREATION_PATH", diff --git a/cbpi/controller/upload_controller.py b/cbpi/controller/upload_controller.py index 395a6d9..e08f524 100644 --- a/cbpi/controller/upload_controller.py +++ b/cbpi/controller/upload_controller.py @@ -208,6 +208,24 @@ class UploadController: await self.create_step(step_string) for row in c.execute('SELECT Name, Temp, Dauer FROM Rasten WHERE Typ <> 0 AND SudID = ?', (Recipe_ID,)): + if mashin_temp is None and self.addmashin == "Yes": + step_type = self.mashin if self.mashin != "" else "MashInStep" + step_string = { "name": "MashIn", + "props": { + "AutoMode": self.AutoMode, + "Kettle": self.id, + "Sensor": self.kettle.sensor, + "Temp": str(int(row[1])) if self.TEMP_UNIT == "C" else str(round(9.0 / 5.0 * int(row[1]) + 32)), + "Timer": "0", + "Notification": "Target temperature reached. Please add malt." + }, + "status_text": "", + "status": "I", + "type": step_type + } + await self.create_step(step_string) + + step_type = self.mash if self.mash != "" else "MashStep" step_string = { "name": str(row[0]), "props": { @@ -302,10 +320,38 @@ class UploadController: step_timer = str(int(row.get("timer"))) step_temp = str(int(row.get("temp"))) sensor = self.kettle.sensor - if MashIn_Flag == True and row.get("timer") == 0: - step_type = self.mashin if self.mashin != "" else "MashInStep" - Notification = "Target temperature reached. Please add malt." - MashIn_Flag = False + if MashIn_Flag == True: + if row.get("timer") == 0: + step_type = self.mashin if self.mashin != "" else "MashInStep" + Notification = "Target temperature reached. Please add malt." + MashIn_Flag = False + if step_name is None or step_name == "": + step_name = "MashIn" + elif self.addmashin == "Yes": + step_type = self.mashin if self.mashin != "" else "MashInStep" + Notification = "Target temperature reached. Please add malt." + MashIn_Flag = False + step_string = { "name": "MashIn", + "props": { + "AutoMode": self.AutoMode, + "Kettle": self.id, + "Sensor": self.kettle.sensor, + "Temp": step_temp, + "Timer": 0, + "Notification": Notification + }, + "status_text": "", + "status": "I", + "type": step_type + } + await self.create_step(step_string) + + step_type = self.mash if self.mash != "" else "MashStep" + Notification = "" + else: + step_type = self.mash if self.mash != "" else "MashStep" + Notification = "" + else: step_type = self.mash if self.mash != "" else "MashStep" Notification = "" @@ -441,10 +487,11 @@ class UploadController: try: step_name = step['name'] if step_name == "": - step_name = "MashStep" + step_name = "MashStep" except: step_name = "MashStep" + step_timer = str(int(step['stepTime'])) if self.TEMP_UNIT == "C": @@ -453,10 +500,38 @@ class UploadController: step_temp = str(round((9.0 / 5.0 * int(step['stepTemp']) + 32))) sensor = self.kettle.sensor - if MashIn_Flag == True and int(step_timer) == 0: - step_type = self.mashin if self.mashin != "" else "MashInStep" - Notification = "Target temperature reached. Please add malt." - MashIn_Flag = False + if MashIn_Flag == True: + + if int(step_timer) == 0: + step_type = self.mashin if self.mashin != "" else "MashInStep" + Notification = "Target temperature reached. Please add malt." + MashIn_Flag = False + + elif self.addmashin == "Yes": + step_type = self.mashin if self.mashin != "" else "MashInStep" + Notification = "Target temperature reached. Please add malt." + MashIn_Flag = False + step_string = { "name": "MashIn", + "props": { + "AutoMode": self.AutoMode, + "Kettle": self.id, + "Sensor": self.kettle.sensor, + "Temp": step_temp, + "Timer": 0, + "Notification": Notification + }, + "status_text": "", + "status": "I", + "type": step_type + } + await self.create_step(step_string) + + step_type = self.mash if self.mash != "" else "MashStep" + Notification = "" + else: + step_type = self.mash if self.mash != "" else "MashStep" + Notification = "" + else: step_type = self.mash if self.mash != "" else "MashStep" Notification = "" @@ -645,6 +720,9 @@ class UploadController: self.CoolDownTemp = self.cbpi.config.get("steps_cooldown_temp", 25) # get default Kettle from Settings self.id = self.cbpi.config.get('MASH_TUN', None) + # If next parameter is Yes, MashIn Ste will be added before first mash step if not included in recipe + self.addmashin = self.cbpi.config.get('AddMashInStep', "Yes") + try: self.kettle = self.cbpi.kettle.find_by_id(self.id) except: diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py new file mode 100644 index 0000000..1a668ab --- /dev/null +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -0,0 +1,145 @@ +import os, threading, time +from aiohttp import web +import logging +from unittest.mock import MagicMock, patch +import asyncio +import random +from cbpi.api import * +from cbpi.api.config import ConfigType +from cbpi.api.base import CBPiBase + +logger = logging.getLogger(__name__) + +class ConfigUpdate(CBPiExtension): + + def __init__(self,cbpi): + self.cbpi = cbpi + self._task = asyncio.create_task(self.run()) + + + async def run(self): + logging.info("Check Config for required changes") + + # check is default steps are config parameters + + TEMP_UNIT = self.cbpi.config.get("TEMP_UNIT", "C") + default_boil_temp = 99 if TEMP_UNIT == "C" else 212 + default_cool_temp = 20 if TEMP_UNIT == "C" else 68 + boil_temp = self.cbpi.config.get("steps_boil_temp", None) + cooldown_sensor = self.cbpi.config.get("steps_cooldown_sensor", None) + cooldown_temp = self.cbpi.config.get("steps_cooldown_temp", None) + mashin_step = self.cbpi.config.get("steps_mashin", None) + mash_step = self.cbpi.config.get("steps_mash", None) + mashout_step = self.cbpi.config.get("steps_mashout", None) + boil_step = self.cbpi.config.get("steps_boil", None) + cooldown_step = self.cbpi.config.get("steps_cooldown", None) + + if boil_temp is None: + logger.info("INIT Boil Temp Setting") + try: + await self.cbpi.config.add("steps_boil_temp", default_boil_temp, ConfigType.NUMBER, "Default Boil Temperature for Recipe Creation") + except: + logger.warning('Unable to update database') + + if cooldown_sensor is None: + logger.info("INIT Cooldown Sensor Setting") + try: + await self.cbpi.config.add("steps_cooldown_sensor", "", ConfigType.SENSOR, "Alternative Sensor to monitor temperature durring cooldown (if not selected, Kettle Sensor will be used)") + except: + logger.warning('Unable to update database') + + if cooldown_temp is None: + logger.info("INIT Cooldown Temp Setting") + try: + await self.cbpi.config.add("steps_cooldown_temp", default_cool_temp, ConfigType.NUMBER, "Cooldown temp will send notification when this temeprature is reached") + except: + logger.warning('Unable to update database') + + if cooldown_step is None: + logger.info("INIT Cooldown Step Type") + try: + await self.cbpi.config.add("steps_cooldown", "", ConfigType.STEP, "Cooldown step type") + except: + logger.warning('Unable to update database') + + if mashin_step is None: + logger.info("INIT MashIn Step Type") + try: + await self.cbpi.config.add("steps_mashin", "", ConfigType.STEP, "MashIn step type") + except: + logger.warning('Unable to update database') + + if mash_step is None: + logger.info("INIT Mash Step Type") + try: + await self.cbpi.config.add("steps_mash", "", ConfigType.STEP, "Mash step type") + except: + logger.warning('Unable to update database') + + if mashout_step is None: + logger.info("INIT MashOut Step Type") + try: + await self.cbpi.config.add("steps_mashout", "", ConfigType.STEP, "MashOut step type") + except: + logger.warning('Unable to update database') + + if boil_step is None: + logger.info("INIT Boil Step Type") + try: + await self.cbpi.config.add("steps_boil", "", ConfigType.STEP, "Boil step type") + except: + logger.warning('Unable to update database') + + ## Check if AtuoMode for Steps is in config + AutoMode = self.cbpi.config.get("AutoMode", None) + if AutoMode is None: + logger.info("INIT AutoMode") + try: + await self.cbpi.config.add("AutoMode", "Yes", ConfigType.SELECT, "Use AutoMode in steps", + [{"label": "Yes", "value": "Yes"}, + {"label": "No", "value": "No"}]) + except: + logger.warning('Unable to update config') + + ## Check if AddMashInStep for Steps is in config + AddMashIn = self.cbpi.config.get("AddMashInStep", None) + if AddMashIn is None: + logger.info("INIT AddMashInStep") + try: + await self.cbpi.config.add("AddMashInStep", "Yes", ConfigType.SELECT, "Add MashIn Step automatically if not defined in recipe", + [{"label": "Yes", "value": "Yes"}, + {"label": "No", "value": "No"}]) + except: + logger.warning('Unable to update config') + + ## Check if Brewfather UserID is in config + bfuserid = self.cbpi.config.get("brewfather_user_id", None) + if bfuserid is None: + logger.info("INIT Brewfather User ID") + try: + await self.cbpi.config.add("brewfather_user_id", "", ConfigType.STRING, "Brewfather User ID") + except: + logger.warning('Unable to update config') + + ## Check if Brewfather API Key is in config + bfapikey = self.cbpi.config.get("brewfather_api_key", None) + if bfapikey is None: + logger.info("INIT Brewfather API Key") + try: + await self.cbpi.config.add("brewfather_api_key", "", ConfigType.STRING, "Brewfather API Key") + except: + logger.warning('Unable to update config') + + ## Check if Brewfather API Key is in config + RecipeCreationPath = self.cbpi.config.get("RECIPE_CREATION_PATH", None) + if RecipeCreationPath is None: + logger.info("INIT Recipe Creation Path") + try: + await self.cbpi.config.add("RECIPE_CREATION_PATH", "upload", ConfigType.STRING, "API path to creation plugin. Default: upload . CHANGE ONLY IF USING A RECIPE CREATION PLUGIN") + except: + logger.warning('Unable to update config') + + +def setup(cbpi): + cbpi.plugin.register("ConfigUpdate", ConfigUpdate) + pass diff --git a/cbpi/extension/ConfigUpdate/config.yaml b/cbpi/extension/ConfigUpdate/config.yaml new file mode 100644 index 0000000..325f43a --- /dev/null +++ b/cbpi/extension/ConfigUpdate/config.yaml @@ -0,0 +1,3 @@ +name: ConfigUpdate +version: 4 +active: true