Fix NoRetain MQTT setting implementation to properly use config value
This commit is contained in:
parent
040fbd68d8
commit
3d2afbd248
@ -661,9 +661,12 @@ class TasmotaDiscovery:
|
|||||||
changes_needed.append(('MqttPassword', mqtt_fields['Password']))
|
changes_needed.append(('MqttPassword', mqtt_fields['Password']))
|
||||||
self.logger.debug(f"{name}: MQTT Password will be updated")
|
self.logger.debug(f"{name}: MQTT Password will be updated")
|
||||||
|
|
||||||
# Check NoRetain setting
|
# Check NoRetain setting - FIXED: Use the actual value from config with default of False
|
||||||
if mqtt_config.get('NoRetain', True):
|
no_retain = mqtt_config.get('NoRetain', False)
|
||||||
|
if no_retain:
|
||||||
changes_needed.append(('SetOption62', '1')) # 1 = No Retain
|
changes_needed.append(('SetOption62', '1')) # 1 = No Retain
|
||||||
|
else:
|
||||||
|
changes_needed.append(('SetOption62', '0')) # 0 = Use Retain
|
||||||
|
|
||||||
# Apply changes if needed
|
# Apply changes if needed
|
||||||
for setting, value in changes_needed:
|
for setting, value in changes_needed:
|
||||||
@ -750,76 +753,6 @@ class TasmotaDiscovery:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.logger.error(f"Error saving device details: {e}")
|
self.logger.error(f"Error saving device details: {e}")
|
||||||
|
|
||||||
def check_mqtt_settings(ip, name, mqtt_status):
|
|
||||||
"""Check and update MQTT settings if they don't match config"""
|
|
||||||
# Get the base hostname (everything before the dash)
|
|
||||||
hostname_base = name.split('-')[0] if '-' in name else name
|
|
||||||
|
|
||||||
mqtt_fields = {
|
|
||||||
"Host": mqtt_config.get('Host', ''),
|
|
||||||
"Port": mqtt_config.get('Port', 1883),
|
|
||||||
"User": mqtt_config.get('User', ''),
|
|
||||||
"Password": mqtt_config.get('Password', ''),
|
|
||||||
"Topic": hostname_base if mqtt_config.get('Topic') == '%hostname_base%' else mqtt_config.get('Topic', ''),
|
|
||||||
"FullTopic": mqtt_config.get('FullTopic', '%prefix%/%topic%/'),
|
|
||||||
}
|
|
||||||
|
|
||||||
device_mqtt = mqtt_status.get('MqttHost', {})
|
|
||||||
changes_needed = []
|
|
||||||
force_password_update = False
|
|
||||||
|
|
||||||
# Check each MQTT setting
|
|
||||||
if device_mqtt.get('Host') != mqtt_fields['Host']:
|
|
||||||
changes_needed.append(('MqttHost', mqtt_fields['Host']))
|
|
||||||
self.logger.debug(f"{name}: MQTT Host mismatch - Device: {device_mqtt.get('Host')}, Config: {mqtt_fields['Host']}")
|
|
||||||
force_password_update = True
|
|
||||||
|
|
||||||
if device_mqtt.get('Port') != mqtt_fields['Port']:
|
|
||||||
changes_needed.append(('MqttPort', mqtt_fields['Port']))
|
|
||||||
self.logger.debug(f"{name}: MQTT Port mismatch - Device: {device_mqtt.get('Port')}, Config: {mqtt_fields['Port']}")
|
|
||||||
force_password_update = True
|
|
||||||
|
|
||||||
if device_mqtt.get('User') != mqtt_fields['User']:
|
|
||||||
changes_needed.append(('MqttUser', mqtt_fields['User']))
|
|
||||||
self.logger.debug(f"{name}: MQTT User mismatch - Device: {device_mqtt.get('User')}, Config: {mqtt_fields['User']}")
|
|
||||||
force_password_update = True
|
|
||||||
|
|
||||||
if device_mqtt.get('Topic') != mqtt_fields['Topic']:
|
|
||||||
changes_needed.append(('Topic', mqtt_fields['Topic']))
|
|
||||||
self.logger.debug(f"{name}: MQTT Topic mismatch - Device: {device_mqtt.get('Topic')}, Config: {mqtt_fields['Topic']}")
|
|
||||||
force_password_update = True
|
|
||||||
|
|
||||||
if device_mqtt.get('FullTopic') != mqtt_fields['FullTopic']:
|
|
||||||
changes_needed.append(('FullTopic', mqtt_fields['FullTopic']))
|
|
||||||
self.logger.debug(f"{name}: MQTT FullTopic mismatch - Device: {device_mqtt.get('FullTopic')}, Config: {mqtt_fields['FullTopic']}")
|
|
||||||
force_password_update = True
|
|
||||||
|
|
||||||
# Add password update if any MQTT setting changed or user was updated
|
|
||||||
if force_password_update:
|
|
||||||
changes_needed.append(('MqttPassword', mqtt_fields['Password']))
|
|
||||||
self.logger.debug(f"{name}: MQTT Password will be updated")
|
|
||||||
|
|
||||||
# Check NoRetain setting
|
|
||||||
if mqtt_config.get('NoRetain', True):
|
|
||||||
changes_needed.append(('SetOption62', '1')) # 1 = No Retain
|
|
||||||
|
|
||||||
# Apply changes if needed
|
|
||||||
for setting, value in changes_needed:
|
|
||||||
try:
|
|
||||||
url = f"http://{ip}/cm?cmnd={setting}%20{value}"
|
|
||||||
response = requests.get(url, timeout=5)
|
|
||||||
if response.status_code == 200:
|
|
||||||
if setting != 'MqttPassword':
|
|
||||||
self.logger.debug(f"{name}: Updated {setting} to {value}")
|
|
||||||
else:
|
|
||||||
self.logger.debug(f"{name}: Updated MQTT Password")
|
|
||||||
else:
|
|
||||||
self.logger.error(f"{name}: Failed to update {setting}")
|
|
||||||
except requests.exceptions.RequestException as e:
|
|
||||||
self.logger.error(f"{name}: Error updating {setting}: {str(e)}")
|
|
||||||
|
|
||||||
return len(changes_needed) > 0
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='Tasmota Device Manager')
|
parser = argparse.ArgumentParser(description='Tasmota Device Manager')
|
||||||
parser.add_argument('--config', default='network_configuration.json',
|
parser.add_argument('--config', default='network_configuration.json',
|
||||||
|
|||||||
@ -27,6 +27,22 @@
|
|||||||
"Password": "mgeppert",
|
"Password": "mgeppert",
|
||||||
"Topic": "%hostname_base%",
|
"Topic": "%hostname_base%",
|
||||||
"FullTopic": "%prefix%/%topic%/",
|
"FullTopic": "%prefix%/%topic%/",
|
||||||
"NoRetain": false
|
"NoRetain": false,
|
||||||
|
"console": {
|
||||||
|
"SwitchRetain": "Off",
|
||||||
|
"ButtonRetain": "On",
|
||||||
|
"ButtonRetain": "Off",
|
||||||
|
"PowerOnState": "3",
|
||||||
|
"PowerRetain": "On",
|
||||||
|
"SetOption1": "0",
|
||||||
|
"SetOption3": "1",
|
||||||
|
"SetOption13": "0",
|
||||||
|
"SetOption19": "0",
|
||||||
|
"SetOption32": "8",
|
||||||
|
"SetOption53": "1",
|
||||||
|
"SetOption73": "1",
|
||||||
|
"rule1": "on button1#state=10 do power0 toggle endon",
|
||||||
|
"rule1": "1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user