diff --git a/.vscode/launch.json b/.vscode/launch.json index 4103a6a5..1df777fd 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,7 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "name": "meshtastic BLE", "type": "debugpy", @@ -261,7 +262,14 @@ "module": "meshtastic", "justMyCode": true, "args": ["--nodes", "--show-fields", "AKA,Pubkey,Role,Role,Role,Latitude,Latitude,deviceMetrics.voltage"] - } - + }, + { + "name": "meshtastic --export-config", + "type": "debugpy", + "request": "launch", + "module": "meshtastic", + "justMyCode": true, + "args": ["--export-config", "config.json"] + }, ] } diff --git a/meshtastic/__main__.py b/meshtastic/__main__.py index 978da0ab..a5d7c85f 100644 --- a/meshtastic/__main__.py +++ b/meshtastic/__main__.py @@ -1122,11 +1122,32 @@ def subscribe() -> None: # pub.subscribe(onNode, "meshtastic.node") +def set_missing_flags_false(config_dict: dict, true_defaults: set[tuple[str, str]]) -> None: + """Ensure that missing default=True keys are present in the config_dict and set to False.""" + for path in true_defaults: + d = config_dict + for key in path[:-1]: + if key not in d or not isinstance(d[key], dict): + d[key] = {} + d = d[key] + if path[-1] not in d: + d[path[-1]] = False def export_config(interface) -> str: """used in --export-config""" configObj = {} + # A list of configuration keys that should be set to False if they are missing + true_defaults = { + ("bluetooth", "enabled"), + ("lora", "sx126xRxBoostedGain"), + ("lora", "txEnabled"), + ("lora", "usePreset"), + ("position", "positionBroadcastSmartEnabled"), + ("security", "serialEnabled"), + ("mqtt", "encryptionEnabled"), + } + owner = interface.getLongName() owner_short = interface.getShortName() channel_url = interface.localNode.getURL() @@ -1185,6 +1206,8 @@ def export_config(interface) -> str: else: configObj["config"] = config + set_missing_flags_false(configObj["config"], true_defaults) + module_config = MessageToDict(interface.localNode.moduleConfig) if module_config: # Convert inner keys to correct snake/camelCase diff --git a/meshtastic/tests/test_main.py b/meshtastic/tests/test_main.py index 811c8064..1e24dc2d 100644 --- a/meshtastic/tests/test_main.py +++ b/meshtastic/tests/test_main.py @@ -18,6 +18,7 @@ onNode, onReceive, tunnelMain, + set_missing_flags_false, ) from meshtastic import mt_config @@ -1897,6 +1898,41 @@ def test_main_export_config(capsys): # mo.assert_called() +@pytest.mark.unit +def test_set_missing_flags_false(): + """Test set_missing_flags_false() function""" + config = { + "bluetooth": { + "enabled": True + }, + "lora": { + "txEnabled": True + } + } + + false_defaults = { + ("bluetooth", "enabled"), + ("lora", "sx126xRxBoostedGain"), + ("lora", "txEnabled"), + ("lora", "usePreset"), + ("position", "positionBroadcastSmartEnabled"), + ("security", "serialEnabled"), + ("mqtt", "encryptionEnabled"), + } + + set_missing_flags_false(config, false_defaults) + + # Preserved + assert config["bluetooth"]["enabled"] is True + assert config["lora"]["txEnabled"] is True + + # Added + assert config["lora"]["usePreset"] is False + assert config["lora"]["sx126xRxBoostedGain"] is False + assert config["position"]["positionBroadcastSmartEnabled"] is False + assert config["security"]["serialEnabled"] is False + assert config["mqtt"]["encryptionEnabled"] is False + @pytest.mark.unit @pytest.mark.usefixtures("reset_mt_config") def test_main_gpio_rd_no_gpio_channel(capsys):