From 03e3487d4e396df40cef9e11f9a244175e78c1f2 Mon Sep 17 00:00:00 2001 From: Nathan Gardiner Date: Sun, 2 Feb 2020 16:34:36 +1100 Subject: [PATCH] Moved status update code from check_green_energy to a dedicated background task which only runs after recalculation of amps available --- TWCManager.py | 24 +++++++++++++++--------- lib/TWCManager/TWCMaster.py | 4 ++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/TWCManager.py b/TWCManager.py index 4ca37499..32a1b8e8 100644 --- a/TWCManager.py +++ b/TWCManager.py @@ -159,6 +159,8 @@ def background_tasks_thread(): carapi.car_api_available(task['email'], task['password']) elif(task['cmd'] == 'checkGreenEnergy'): check_green_energy() + elif(task['cmd'] == 'updateStatus'): + update_statuses() # Delete task['cmd'] from backgroundTasksCmds such that # queue_background_task() can queue another task['cmd'] in the future. @@ -188,16 +190,20 @@ def check_green_energy(): master.setGeneration('Powerwall2', powerwall.getGeneration()) master.setGeneration('TED', ted.getGeneration()) - if(config['config']['debugLevel'] >= 1): - print("%s: Solar generating %dW, Consumption %dW, Charger Load %dW" % (time_now(), master.getGeneration(), master.getConsumption(), master.getChargerLoad())) - print(" Limiting car charging to %.2fA - %.2fA = %.2fA." % ((master.getGeneration() / 240), (master.getGenerationOffset() / 240), master.getMaxAmpsToDivideAmongSlaves())) - print(" Charge when above %.0fA (minAmpsPerTWC)." % (config['config']['minAmpsPerTWC'])) +def update_statuses(): - # Update HASS sensors with min/max amp values - master.gethassstatus().setStatus(bytes("config", 'UTF-8'), "min_amps_per_twc", config['config']['minAmpsPerTWC']) - master.getmqttstatus().setStatus(bytes("config", 'UTF-8'), "minAmpsPerTWC", config['config']['minAmpsPerTWC']) - master.gethassstatus().setStatus(bytes("all", 'UTF-8'), "max_amps_for_slaves", master.getMaxAmpsToDivideAmongSlaves()) - master.getmqttstatus().setStatus(bytes("all", 'UTF-8'), "maxAmpsForSlaves", master.getMaxAmpsToDivideAmongSlaves()) + # Print a status update, and update MQTT / HASS values + + if(config['config']['debugLevel'] >= 1): + print("%s: Green energy generates %dW, Consumption %dW, Charger Load %dW" % (time_now(), master.getGeneration(), master.getConsumption(), master.getChargerLoad())) + print(" Limiting car charging to %.2fA - %.2fA = %.2fA." % ((master.getGeneration() / 240), (master.getGenerationOffset() / 240), master.getMaxAmpsToDivideAmongSlaves())) + print(" Charge when above %.0fA (minAmpsPerTWC)." % (config['config']['minAmpsPerTWC'])) + + # Update HASS sensors with min/max amp values + master.gethassstatus().setStatus(bytes("config", 'UTF-8'), "min_amps_per_twc", config['config']['minAmpsPerTWC']) + master.getmqttstatus().setStatus(bytes("config", 'UTF-8'), "minAmpsPerTWC", config['config']['minAmpsPerTWC']) + master.gethassstatus().setStatus(bytes("all", 'UTF-8'), "max_amps_for_slaves", master.getMaxAmpsToDivideAmongSlaves()) + master.getmqttstatus().setStatus(bytes("all", 'UTF-8'), "maxAmpsForSlaves", master.getMaxAmpsToDivideAmongSlaves()) # # End functions diff --git a/lib/TWCManager/TWCMaster.py b/lib/TWCManager/TWCMaster.py index dca3cb8d..742c040f 100644 --- a/lib/TWCManager/TWCMaster.py +++ b/lib/TWCManager/TWCMaster.py @@ -753,6 +753,10 @@ def setMaxAmpsToDivideAmongSlaves(self, amps): self.releaseBackgroundTasksLock() + # Now that we have updated the maxAmpsToDivideAmongSlaves, send update + # to console / MQTT / etc + self.queue_background_task({'cmd':'updateStatus'}) + def setNonScheduledAmpsMax(self, amps): self.settings['nonScheduledAmpsMax'] = amps