mirror of
https://github.com/Tediore/battery2mqtt.git
synced 2025-04-04 15:33:44 +02:00
Only send MQTT payload if info has changed from previous payload
This commit is contained in:
parent
dfecf44afc
commit
d3b2e5e428
@ -28,6 +28,7 @@ path = "/sys/class/power_supply/"
|
||||
dirs = os.listdir(path)
|
||||
|
||||
payload = {}
|
||||
prev_payload = {}
|
||||
health_calc = {}
|
||||
time_remaining = {}
|
||||
mqtt_connected = False
|
||||
@ -62,6 +63,8 @@ def check_conditions():
|
||||
|
||||
def get_info():
|
||||
# Get requested conditions and generate/send MQTT payload
|
||||
global payload
|
||||
global prev_payload
|
||||
for dir in dirs:
|
||||
if AC_ADAPTER:
|
||||
if dir.startswith('AC'):
|
||||
@ -120,7 +123,10 @@ def get_info():
|
||||
pass
|
||||
|
||||
try:
|
||||
client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), MQTT_QOS, False)
|
||||
if prev_payload != payload:
|
||||
# only send MQTT payload if information has changed from previous payload
|
||||
client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), MQTT_QOS, False)
|
||||
prev_payload = payload
|
||||
if LOG_LEVEL == 'DEBUG':
|
||||
logging.debug('Sending MQTT payload: ' + str(payload))
|
||||
except Exception as e:
|
||||
|
Loading…
x
Reference in New Issue
Block a user