From 7ad3fd252e7b9dfd1597b8755e54466b623c4de1 Mon Sep 17 00:00:00 2001 From: Alexander Vollkopf <43980694+avollkopf@users.noreply.github.com> Date: Mon, 24 Jan 2022 22:20:27 +0100 Subject: [PATCH 01/11] Update __init__.py --- cbpi/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 626a2d4..a4d1e04 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,2 +1,2 @@ -__version__ = "4.0.1.2" +__version__ = "4.0.1.3" From 8320853399b06c52e41bd3591c27b065ba9a8f02 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Tue, 25 Jan 2022 07:47:40 +0100 Subject: [PATCH 02/11] Minor fixes - onewire is not hardcoded to busmaster 1 - try to catch exception while sending data to WS and WS has closed connection --- cbpi/__init__.py | 3 +-- cbpi/extension/onewire/__init__.py | 2 +- cbpi/websocket.py | 8 ++++++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index a4d1e04..f1ec938 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1,2 +1 @@ -__version__ = "4.0.1.3" - +__version__ = "4.0.1.4" diff --git a/cbpi/extension/onewire/__init__.py b/cbpi/extension/onewire/__init__.py index 6d1e604..0191354 100644 --- a/cbpi/extension/onewire/__init__.py +++ b/cbpi/extension/onewire/__init__.py @@ -41,7 +41,7 @@ class ReadThread (threading.Thread): try: if self.sensor_name is None: return - with open('/sys/bus/w1/devices/w1_bus_master1/%s/w1_slave' % self.sensor_name, 'r') as content_file: + with open('/sys/bus/w1/devices/%s/w1_slave' % self.sensor_name, 'r') as content_file: content = content_file.read() if (content.split('\n')[0].split(' ')[11] == "YES"): temp = float(content.split("=")[-1]) / 1000 # temp in Celcius diff --git a/cbpi/websocket.py b/cbpi/websocket.py index 253567a..fa6bc5c 100644 --- a/cbpi/websocket.py +++ b/cbpi/websocket.py @@ -32,7 +32,11 @@ class CBPiWebSocket: self.logger.debug("broadcast to ws clients. Data: %s" % data) for ws in self._clients: async def send_data(ws, data): - await ws.send_json(data=data, dumps=json_dumps) + try: + await ws.send_json(data=data, dumps=json_dumps) + except Exception as e: + self.logger.error("Error with client %s: %s" % (ws, str(e))) + self.cbpi.app.loop.create_task(send_data(ws, data)) async def websocket_handler(self, request): @@ -85,4 +89,4 @@ class CBPiWebSocket: self.logger.info("Web Socket Close") return ws - \ No newline at end of file + From 50677a2a3edeecb0987325bd9989ed435412fa57 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Tue, 25 Jan 2022 11:33:21 +0100 Subject: [PATCH 03/11] Restore config fix for windows --- cbpi/__init__.py | 2 +- cbpi/cli.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index f1ec938..3d3d1cb 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.4" +__version__ = "4.0.1.5" diff --git a/cbpi/cli.py b/cbpi/cli.py index 2758b2f..9b28fc6 100644 --- a/cbpi/cli.py +++ b/cbpi/cli.py @@ -10,6 +10,7 @@ from cbpi.utils.utils import load_config from zipfile import ZipFile from cbpi.craftbeerpi import CraftBeerPi import os +import platform import pathlib import shutil import yaml @@ -152,14 +153,19 @@ def check_for_setup(): if zip_content == True: print("Found correct content. Starting Restore process") output_path = pathlib.Path(os.path.join(".", 'config')) - owner = output_path.owner() - group = output_path.group() + system = platform.system() + print(system) + if system != "Windows": + owner = output_path.owner() + group = output_path.group() print("Removing old config folder") shutil.rmtree(output_path, ignore_errors=True) print("Extracting zip file to config folder") zip.extractall(output_path) - print("Changing owner and group of config folder recursively to {}:{}".format(owner,group)) - recursive_chown(output_path, owner, group) + zip.close() + if system != "Windows": + print("Changing owner and group of config folder recursively to {}:{}".format(owner,group)) + recursive_chown(output_path, owner, group) print("Removing backup file") os.remove(backupfile) else: From f4b028f26323aa407716913a632a1f225773c0a3 Mon Sep 17 00:00:00 2001 From: Alexander Vollkopf <43980694+avollkopf@users.noreply.github.com> Date: Tue, 25 Jan 2022 21:16:52 +0100 Subject: [PATCH 04/11] Update __init__.py Update version for last merge --- cbpi/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 3d3d1cb..3489099 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.5" +__version__ = "4.0.1.6" From 2de62ca7d596b16070e39b5d4a41c9fc35b1774b Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Fri, 28 Jan 2022 07:56:24 +0100 Subject: [PATCH 05/11] Added Actor for Cooldown step in settings - Default Actor can be defined for cooldown to trigger cooling water on and off. - will be used in automated recipe creation for cooldown step --- cbpi/__init__.py | 2 +- cbpi/controller/upload_controller.py | 5 ++++- cbpi/extension/ConfigUpdate/__init__.py | 8 ++++++++ 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 3489099..dc85dce 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.6" +__version__ = "4.0.1.7" diff --git a/cbpi/controller/upload_controller.py b/cbpi/controller/upload_controller.py index eb41501..c7db2bb 100644 --- a/cbpi/controller/upload_controller.py +++ b/cbpi/controller/upload_controller.py @@ -977,7 +977,8 @@ class UploadController: "Kettle": self.boilid, "Timer": step_timer, "Temp": step_temp, - "Sensor": cooldown_sensor + "Sensor": cooldown_sensor, + "Actor": self.CoolDownActor }, "status_text": "", "status": "I", @@ -1001,6 +1002,7 @@ class UploadController: self.BoilTemp = self.cbpi.config.get("steps_boil_temp", 98) #get default cooldown temp alarm setting self.CoolDownTemp = self.cbpi.config.get("steps_cooldown_temp", 25) + self.CoolDownActor = self.cbpi.config.get("steps_cooldown_actor", None) # get default Kettle from Settings self.id = self.cbpi.config.get('MASH_TUN', None) self.boilid = self.cbpi.config.get('BoilKettle', None) @@ -1030,6 +1032,7 @@ class UploadController: "cooldown": str(self.cooldown), "boiltemp": str(self.BoilTemp), "cooldowntemp": str(self.CoolDownTemp), + "cooldownactor": self.CoolDownActor, "temp_unit": str(self.TEMP_UNIT), "AutoMode": str(self.AutoMode) } diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py index a6714f7..ae4704f 100644 --- a/cbpi/extension/ConfigUpdate/__init__.py +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -28,6 +28,7 @@ class ConfigUpdate(CBPiExtension): 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_actor = self.cbpi.config.get("steps_cooldown_actor", 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) @@ -60,6 +61,13 @@ class ConfigUpdate(CBPiExtension): except: logger.warning('Unable to update database') + if cooldown_actor is None: + logger.info("INIT Cooldown Actor Setting") + try: + await self.cbpi.config.add("steps_cooldown_actor", "", ConfigType.ACTOR, "Actor to trigger cooldown water on and off (default: None)") + except: + logger.warning('Unable to update database') + if cooldown_temp is None: logger.info("INIT Cooldown Temp Setting") try: From f2e9b1bebc43fb401ad06af34443185713052ba4 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Fri, 28 Jan 2022 19:41:58 +0100 Subject: [PATCH 06/11] Update fermentation_controller.py --- cbpi/controller/fermentation_controller.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cbpi/controller/fermentation_controller.py b/cbpi/controller/fermentation_controller.py index 5bb7611..7e2129e 100644 --- a/cbpi/controller/fermentation_controller.py +++ b/cbpi/controller/fermentation_controller.py @@ -199,7 +199,8 @@ class FermentationController: def get_state(self): if self.data == []: - logging.info(self.data) + #logging.info(self.data) + pass return {"data": list(map(lambda x: x.to_dict(), self.data)), "types":self.get_types(), "steptypes":self.get_steptypes()} From a7ddcdd30645c0661649225457f7918a07aa95a0 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Mon, 31 Jan 2022 07:27:01 +0100 Subject: [PATCH 07/11] fix get_actor_state () in CBPiBase --- cbpi/__init__.py | 2 +- cbpi/api/base.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index dc85dce..4fd74a9 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.7" +__version__ = "4.0.1.8" diff --git a/cbpi/api/base.py b/cbpi/api/base.py index e4c0c9a..d698c1e 100644 --- a/cbpi/api/base.py +++ b/cbpi/api/base.py @@ -51,10 +51,10 @@ class CBPiBase(metaclass=ABCMeta): def get_actor_state(self,id): try: actor = self.cbpi.actor.find_by_id(id) - return actor.get("instance").get_state() + return actor.instance.state except: logging.error("Failed to read actor state in step - actor {}".format(id)) - return None + return False async def actor_on(self,id,power=100): @@ -73,4 +73,4 @@ class CBPiBase(metaclass=ABCMeta): try: await self.cbpi.actor.set_power(id,power) except Exception as e: - pass \ No newline at end of file + pass From ed141f87881b33fcc4be950fde761395a7a008d9 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Mon, 31 Jan 2022 17:17:53 +0100 Subject: [PATCH 08/11] Added mqttutil extension - forces mqtt update for kettle and femrenter in specified timeframes even w/o change in payload - required for mqttdevice - only active if mqtt is enabled - can be set to never and is also deactivated when mqtt is enabled --- cbpi/__init__.py | 2 +- cbpi/extension/ConfigUpdate/__init__.py | 14 ++++++++ cbpi/extension/mqtt_util/__init__.py | 44 +++++++++++++++++++++++++ cbpi/extension/mqtt_util/config.yaml | 3 ++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 cbpi/extension/mqtt_util/__init__.py create mode 100644 cbpi/extension/mqtt_util/config.yaml diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 4fd74a9..1f11a12 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.8" +__version__ = "4.0.1.9" diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py index ae4704f..f05145e 100644 --- a/cbpi/extension/ConfigUpdate/__init__.py +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -44,6 +44,7 @@ class ConfigUpdate(CBPiExtension): influxdbname = self.cbpi.config.get("INFLUXDBNAME", None) influxdbuser = self.cbpi.config.get("INFLUXDBUSER", None) influxdbpwd = self.cbpi.config.get("INFLUXDBPWD", None) + mqttupdate = self.cbpi.config.get("MQTTUpdate", None) @@ -252,6 +253,19 @@ class ConfigUpdate(CBPiExtension): except: logger.warning('Unable to update config') + if mqttupdate is None: + logger.info("INIT MQTT update frequency for Kettles and Fermenters") + try: + await self.cbpi.config.add("MQTTUpdate", 60, ConfigType.SELECT, "Forced MQTT Update frequency in s for Kettle and Fermenter (no changes in payload required). Restart required after change", + [{"label": "30", "value": 30}, + {"label": "60", "value": 60}, + {"label": "120", "value": 120}, + {"label": "300", "value": 300}, + {"label": "Never", "value": 0}]) + except: + logger.warning('Unable to update database') + + def setup(cbpi): cbpi.plugin.register("ConfigUpdate", ConfigUpdate) pass diff --git a/cbpi/extension/mqtt_util/__init__.py b/cbpi/extension/mqtt_util/__init__.py new file mode 100644 index 0000000..bc20f04 --- /dev/null +++ b/cbpi/extension/mqtt_util/__init__.py @@ -0,0 +1,44 @@ +import logging +import asyncio +from cbpi.api import * +from cbpi.api.config import ConfigType +from cbpi.api.base import CBPiBase +from cbpi.controller.fermentation_controller import FermentationController +from cbpi.controller.kettle_controller import KettleController + +logger = logging.getLogger(__name__) + +class MQTTUtil(CBPiExtension): + + def __init__(self,cbpi): + self.cbpi = cbpi + self.kettlecontroller : KettleController = cbpi.kettle + self.fermentationcontroller : FermentationController = cbpi.fermenter + self.mqttupdate = int(self.cbpi.config.get("MQTTUpdate", 60)) + if self.mqttupdate != 0: + self._task = asyncio.create_task(self.run()) + logger.info("INIT MQTTUtil") + + async def run(self): + + while True: + try: + self.kettle=self.kettlecontroller.get_state() + for item in self.kettle['data']: + self.cbpi.push_update("cbpi/{}/{}".format("kettleupdate",item['id']), item) + except Exception as e: + logging.error(e) + pass + try: + self.fermenter=self.fermentationcontroller.get_state() + for item in self.fermenter['data']: + self.cbpi.push_update("cbpi/{}/{}".format("fermenterupdate",item['id']), item) + except Exception as e: + logging.error(e) + pass + await asyncio.sleep(self.mqttupdate) + +def setup(cbpi): + if str(cbpi.static_config.get("mqtt", False)).lower() == "true": + cbpi.plugin.register("MQTTUtil", MQTTUtil) + pass diff --git a/cbpi/extension/mqtt_util/config.yaml b/cbpi/extension/mqtt_util/config.yaml new file mode 100644 index 0000000..005124a --- /dev/null +++ b/cbpi/extension/mqtt_util/config.yaml @@ -0,0 +1,3 @@ +name: MQTTUtil +version: 4 +active: true From 0bf2b64c3edfa2a7db319deea0159091185bfdd5 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Thu, 3 Feb 2022 17:00:12 +0100 Subject: [PATCH 09/11] Reduction of duplicated mqtt actor messages --- cbpi/__init__.py | 2 +- cbpi/controller/actor_controller.py | 11 +++++++---- cbpi/controller/satellite_controller.py | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 1f11a12..763e1e5 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.9" +__version__ = "4.0.1.10" diff --git a/cbpi/controller/actor_controller.py b/cbpi/controller/actor_controller.py index edb559d..f31abeb 100644 --- a/cbpi/controller/actor_controller.py +++ b/cbpi/controller/actor_controller.py @@ -9,7 +9,6 @@ class ActorController(BasicController): self.update_key = "actorupdate" async def on(self, id, power=None): -# logging.info("Controller_power: {}".format(power)) try: item = self.find_by_id(id) if power is None: @@ -20,7 +19,8 @@ class ActorController(BasicController): power = 100 if item.instance.state is False: await item.instance.on(power) - await self.push_udpate() + #await self.push_udpate() + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict(), True) else: await self.set_power(id, power) @@ -33,7 +33,8 @@ class ActorController(BasicController): item = self.find_by_id(id) if item.instance.state is True: await item.instance.off() - await self.push_udpate() + #await self.push_udpate() + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to switch on Actor {} {}".format(id, e), True) @@ -43,6 +44,7 @@ class ActorController(BasicController): item = self.find_by_id(id) instance = item.get("instance") await instance.toggle() + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to toggle Actor {} {}".format(id, e)) @@ -58,7 +60,8 @@ class ActorController(BasicController): try: item = self.find_by_id(id) item.power = round(power) - await self.push_udpate() + #await self.push_udpate() + self.cbpi.ws.send(dict(topic=self.update_key, data=list(map(lambda item: item.to_dict(), self.data)))) self.cbpi.push_update("cbpi/actorupdate/{}".format(id), item.to_dict()) except Exception as e: logging.error("Failed to update Actor {} {}".format(id, e)) diff --git a/cbpi/controller/satellite_controller.py b/cbpi/controller/satellite_controller.py index 4629ac8..3fbcd0d 100644 --- a/cbpi/controller/satellite_controller.py +++ b/cbpi/controller/satellite_controller.py @@ -63,7 +63,7 @@ class SatelliteController: if power < 0: power = 0 await self.cbpi.actor.set_power(topic_key[2],power) - await self.cbpi.actor.actor_update(topic_key[2],power) + #await self.cbpi.actor.actor_update(topic_key[2],power) except: self.logger.warning("Failed to set actor power via mqtt. No valid power in message") except: From c1dacd691e8ca5326b72d965e0ce1fc2c4ece158 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Thu, 3 Feb 2022 18:03:53 +0100 Subject: [PATCH 10/11] added filters for mqtt updates that can be triggered via mqtt cbpi/updatekettle cbpi/updateactor cbpi/updatesensor cbpi/updatefermenter --- cbpi/__init__.py | 2 +- cbpi/controller/satellite_controller.py | 47 +++++++++++++++++++++++-- cbpi/craftbeerpi.py | 2 +- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 763e1e5..466d13e 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.10" +__version__ = "4.0.1.11" diff --git a/cbpi/controller/satellite_controller.py b/cbpi/controller/satellite_controller.py index 3fbcd0d..230193e 100644 --- a/cbpi/controller/satellite_controller.py +++ b/cbpi/controller/satellite_controller.py @@ -1,5 +1,4 @@ - import asyncio import json from re import M @@ -8,11 +7,14 @@ from contextlib import AsyncExitStack, asynccontextmanager from cbpi import __version__ import logging - class SatelliteController: def __init__(self, cbpi): self.cbpi = cbpi + self.kettlecontroller = cbpi.kettle + self.fermentercontroller = cbpi.fermenter + self.sensorcontroller = cbpi.sensor + self.actorcontroller = cbpi.actor self.logger = logging.getLogger(__name__) self.host = cbpi.static_config.get("mqtt_host", "localhost") self.port = cbpi.static_config.get("mqtt_port", 1883) @@ -23,6 +25,11 @@ class SatelliteController: ("cbpi/actor/+/on", self._actor_on), ("cbpi/actor/+/off", self._actor_off), ("cbpi/actor/+/power", self._actor_power), + ("cbpi/updateactor", self._actorupdate), + ("cbpi/updatekettle", self._kettleupdate), + ("cbpi/updatesensor", self._sensorupdate), + ("cbpi/updatefermenter", self._fermenterupdate), + ] self.tasks = set() @@ -69,6 +76,42 @@ class SatelliteController: except: self.logger.warning("Failed to set actor power via mqtt") + async def _kettleupdate(self, messages): + async for message in messages: + try: + self.kettle=self.kettlecontroller.get_state() + for item in self.kettle['data']: + self.cbpi.push_update("cbpi/{}/{}".format("kettleupdate",item['id']), item) + except Exception as e: + self.logger.warning("Failed to send kettleupdate via mqtt: {}".format(e)) + + async def _fermenterupdate(self, messages): + async for message in messages: + try: + self.fermenter=self.fermentercontroller.get_state() + for item in self.fermenter['data']: + self.cbpi.push_update("cbpi/{}/{}".format("fermenterupdate",item['id']), item) + except Exception as e: + self.logger.warning("Failed to send fermenterupdate via mqtt: {}".format(e)) + + async def _actorupdate(self, messages): + async for message in messages: + try: + self.actor=self.actorcontroller.get_state() + for item in self.actor['data']: + self.cbpi.push_update("cbpi/{}/{}".format("actorupdate",item['id']), item) + except Exception as e: + self.logger.warning("Failed to send actorupdate via mqtt: {}".format(e)) + + async def _sensorupdate(self, messages): + async for message in messages: + try: + self.sensor=self.sensorcontroller.get_state() + for item in self.sensor['data']: + self.cbpi.push_update("cbpi/{}/{}".format("sensorupdate",item['id']), item) + except Exception as e: + self.logger.warning("Failed to send sensorupdate via mqtt: {}".format(e)) + def subcribe(self, topic, method): task = asyncio.create_task(self._subcribe(topic, method)) return task diff --git a/cbpi/craftbeerpi.py b/cbpi/craftbeerpi.py index 4c2f548..2aec078 100644 --- a/cbpi/craftbeerpi.py +++ b/cbpi/craftbeerpi.py @@ -109,6 +109,7 @@ class CraftBeerPi: self.log = LogController(self) self.system = SystemController(self) self.kettle = KettleController(self) + self.fermenter : FermentationController = FermentationController(self) self.step : StepController = StepController(self) self.recipe : RecipeController = RecipeController(self) self.upload : UploadController = UploadController(self) @@ -117,7 +118,6 @@ class CraftBeerPi: if str(self.static_config.get("mqtt", False)).lower() == "true": self.satellite: SatelliteController = SatelliteController(self) self.dashboard = DashboardController(self) - self.fermenter : FermentationController = FermentationController(self) self.http_step = StepHttpEndpoints(self) self.http_recipe = RecipeHttpEndpoints(self) From 6c1f928933d0306f828a76520ef4541fe0e9a7d1 Mon Sep 17 00:00:00 2001 From: avollkopf <43980694+avollkopf@users.noreply.github.com> Date: Fri, 4 Feb 2022 17:08:10 +0100 Subject: [PATCH 11/11] Update on mqttutil An update on kettle and fermenter is triggered on start of cbpi --- cbpi/__init__.py | 2 +- cbpi/extension/ConfigUpdate/__init__.py | 2 +- cbpi/extension/mqtt_util/__init__.py | 64 ++++++++++++++++++------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/cbpi/__init__.py b/cbpi/__init__.py index 466d13e..ff5e50e 100644 --- a/cbpi/__init__.py +++ b/cbpi/__init__.py @@ -1 +1 @@ -__version__ = "4.0.1.11" +__version__ = "4.0.1.12" diff --git a/cbpi/extension/ConfigUpdate/__init__.py b/cbpi/extension/ConfigUpdate/__init__.py index f05145e..4895dc2 100644 --- a/cbpi/extension/ConfigUpdate/__init__.py +++ b/cbpi/extension/ConfigUpdate/__init__.py @@ -256,7 +256,7 @@ class ConfigUpdate(CBPiExtension): if mqttupdate is None: logger.info("INIT MQTT update frequency for Kettles and Fermenters") try: - await self.cbpi.config.add("MQTTUpdate", 60, ConfigType.SELECT, "Forced MQTT Update frequency in s for Kettle and Fermenter (no changes in payload required). Restart required after change", + await self.cbpi.config.add("MQTTUpdate", 0, ConfigType.SELECT, "Forced MQTT Update frequency in s for Kettle and Fermenter (no changes in payload required). Restart required after change", [{"label": "30", "value": 30}, {"label": "60", "value": 60}, {"label": "120", "value": 120}, diff --git a/cbpi/extension/mqtt_util/__init__.py b/cbpi/extension/mqtt_util/__init__.py index bc20f04..3cce4c4 100644 --- a/cbpi/extension/mqtt_util/__init__.py +++ b/cbpi/extension/mqtt_util/__init__.py @@ -12,32 +12,62 @@ class MQTTUtil(CBPiExtension): def __init__(self,cbpi): self.cbpi = cbpi - self.kettlecontroller : KettleController = cbpi.kettle - self.fermentationcontroller : FermentationController = cbpi.fermenter - self.mqttupdate = int(self.cbpi.config.get("MQTTUpdate", 60)) + self.kettlecontroller = cbpi.kettle + self.fermentationcontroller = cbpi.fermenter + # sensor and actor update is done anyhow during startup +# self.sensorcontroller = cbpi.sensor +# self.actorcontroller = cbpi.actor + + self.mqttupdate = int(self.cbpi.config.get("MQTTUpdate", 0)) if self.mqttupdate != 0: self._task = asyncio.create_task(self.run()) logger.info("INIT MQTTUtil") + else: + self._task = asyncio.create_task(self.push_once()) + + async def push_once(self): + # wait some time to ensure that kettlecontroller is started + await asyncio.sleep(5) + self.push_update() + async def run(self): while True: - try: - self.kettle=self.kettlecontroller.get_state() - for item in self.kettle['data']: - self.cbpi.push_update("cbpi/{}/{}".format("kettleupdate",item['id']), item) - except Exception as e: - logging.error(e) - pass - try: - self.fermenter=self.fermentationcontroller.get_state() - for item in self.fermenter['data']: - self.cbpi.push_update("cbpi/{}/{}".format("fermenterupdate",item['id']), item) - except Exception as e: - logging.error(e) - pass + self.push_update() await asyncio.sleep(self.mqttupdate) + def push_update(self): +# try: +# self.actor=self.actorcontroller.get_state() +# for item in self.actor['data']: +# self.cbpi.push_update("cbpi/{}/{}".format("actorupdate",item['id']), item) +# except Exception as e: +# logging.error(e) +# pass +# try: +# self.sensor=self.sensorcontroller.get_state() +# for item in self.sensor['data']: +# self.cbpi.push_update("cbpi/{}/{}".format("sensorupdate",item['id']), item) +# except Exception as e: +# logging.error(e) +# pass + try: + self.kettle=self.kettlecontroller.get_state() + for item in self.kettle['data']: + self.cbpi.push_update("cbpi/{}/{}".format("kettleupdate",item['id']), item) + except Exception as e: + logging.error(e) + pass + try: + self.fermenter=self.fermentationcontroller.get_state() + for item in self.fermenter['data']: + self.cbpi.push_update("cbpi/{}/{}".format("fermenterupdate",item['id']), item) + except Exception as e: + logging.error(e) + pass + + def setup(cbpi): if str(cbpi.static_config.get("mqtt", False)).lower() == "true": cbpi.plugin.register("MQTTUtil", MQTTUtil)