Update on mqttutil

An update on kettle and fermenter is triggered on start of cbpi
This commit is contained in:
avollkopf 2022-02-04 17:08:10 +01:00
parent c1dacd691e
commit 6c1f928933
3 changed files with 49 additions and 19 deletions

View file

@ -1 +1 @@
__version__ = "4.0.1.11" __version__ = "4.0.1.12"

View file

@ -256,7 +256,7 @@ class ConfigUpdate(CBPiExtension):
if mqttupdate is None: if mqttupdate is None:
logger.info("INIT MQTT update frequency for Kettles and Fermenters") logger.info("INIT MQTT update frequency for Kettles and Fermenters")
try: 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": "30", "value": 30},
{"label": "60", "value": 60}, {"label": "60", "value": 60},
{"label": "120", "value": 120}, {"label": "120", "value": 120},

View file

@ -12,32 +12,62 @@ class MQTTUtil(CBPiExtension):
def __init__(self,cbpi): def __init__(self,cbpi):
self.cbpi = cbpi self.cbpi = cbpi
self.kettlecontroller : KettleController = cbpi.kettle self.kettlecontroller = cbpi.kettle
self.fermentationcontroller : FermentationController = cbpi.fermenter self.fermentationcontroller = cbpi.fermenter
self.mqttupdate = int(self.cbpi.config.get("MQTTUpdate", 60)) # 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: if self.mqttupdate != 0:
self._task = asyncio.create_task(self.run()) self._task = asyncio.create_task(self.run())
logger.info("INIT MQTTUtil") 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): async def run(self):
while True: while True:
try: self.push_update()
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) 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): def setup(cbpi):
if str(cbpi.static_config.get("mqtt", False)).lower() == "true": if str(cbpi.static_config.get("mqtt", False)).lower() == "true":
cbpi.plugin.register("MQTTUtil", MQTTUtil) cbpi.plugin.register("MQTTUtil", MQTTUtil)