Skip to content

Export missing defaults when set False #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"]
},
]
}
23 changes: 23 additions & 0 deletions meshtastic/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions meshtastic/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
onNode,
onReceive,
tunnelMain,
set_missing_flags_false,
)
from meshtastic import mt_config

Expand Down Expand Up @@ -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):
Expand Down
Loading