mirror of
				https://github.com/Tediore/battery2mqtt.git
				synced 2025-11-03 20:46:35 +01:00 
			
		
		
		
	Add MQTT client option and other fixes
This commit is contained in:
		@@ -9,6 +9,7 @@ MQTT_HOST = os.getenv('MQTT_HOST')
 | 
				
			|||||||
MQTT_PORT = int(os.getenv('MQTT_PORT', 1883))
 | 
					MQTT_PORT = int(os.getenv('MQTT_PORT', 1883))
 | 
				
			||||||
MQTT_USER = os.getenv('MQTT_USER')
 | 
					MQTT_USER = os.getenv('MQTT_USER')
 | 
				
			||||||
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD')
 | 
					MQTT_PASSWORD = os.getenv('MQTT_PASSWORD')
 | 
				
			||||||
 | 
					MQTT_CLIENT = os.getenv('MQTT_CLIENT', 'battery2mqtt')
 | 
				
			||||||
MQTT_QOS = int(os.getenv('MQTT_QOS', 1))
 | 
					MQTT_QOS = int(os.getenv('MQTT_QOS', 1))
 | 
				
			||||||
MQTT_TOPIC = os.getenv('MQTT_TOPIC', 'server')
 | 
					MQTT_TOPIC = os.getenv('MQTT_TOPIC', 'server')
 | 
				
			||||||
INTERVAL = int(os.getenv('INTERVAL', 60))
 | 
					INTERVAL = int(os.getenv('INTERVAL', 60))
 | 
				
			||||||
@@ -25,14 +26,13 @@ if LOG_LEVEL.lower() not in ['debug', 'info', 'warning', 'error']:
 | 
				
			|||||||
else:
 | 
					else:
 | 
				
			||||||
    logging.basicConfig(level=LOG_LEVEL, format='%(asctime)s %(levelname)s: %(message)s')
 | 
					    logging.basicConfig(level=LOG_LEVEL, format='%(asctime)s %(levelname)s: %(message)s')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
client = mqtt.Client("battery2mqtt")
 | 
					client = mqtt.Client(MQTT_CLIENT)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
monitored_conditions = MONITORED_CONDITIONS.split(',')
 | 
					monitored_conditions = MONITORED_CONDITIONS.split(',')
 | 
				
			||||||
path = "/sys/class/power_supply/"
 | 
					path = "/sys/class/power_supply/"
 | 
				
			||||||
dirs = os.listdir(path)
 | 
					dirs = os.listdir(path)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
payload = {}
 | 
					payload = {}
 | 
				
			||||||
prev_payload = {}
 | 
					 | 
				
			||||||
health_calc = {}
 | 
					health_calc = {}
 | 
				
			||||||
time_remaining = {}
 | 
					time_remaining = {}
 | 
				
			||||||
mqtt_connected = False
 | 
					mqtt_connected = False
 | 
				
			||||||
@@ -126,15 +126,13 @@ def get_info():
 | 
				
			|||||||
            except:
 | 
					            except:
 | 
				
			||||||
                pass
 | 
					                pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    try:
 | 
					        try:
 | 
				
			||||||
        if prev_payload != payload:
 | 
					            if not dir.startswith('AC'):
 | 
				
			||||||
            # only send MQTT payload if information has changed from previous payload
 | 
					                client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), MQTT_QOS, False)
 | 
				
			||||||
            client.publish("battery2mqtt/" + MQTT_TOPIC + '/' + dir, json.dumps(payload), MQTT_QOS, False)
 | 
					                if LOG_LEVEL == 'DEBUG':
 | 
				
			||||||
            prev_payload = payload
 | 
					                    logging.debug('Sending MQTT payload: ' + str(payload))
 | 
				
			||||||
        if LOG_LEVEL == 'DEBUG':
 | 
					        except Exception as e:
 | 
				
			||||||
            logging.debug('Sending MQTT payload: ' + str(payload))
 | 
					            logging.error(f'Message send failed: {e}')
 | 
				
			||||||
    except Exception as e:
 | 
					 | 
				
			||||||
        logging.error(f'Message send failed: {e}')
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
check_conditions()
 | 
					check_conditions()
 | 
				
			||||||
mqtt_connect()
 | 
					mqtt_connect()
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user