Skip to content

Commit 83d82c5

Browse files
authored
Merge pull request #807 from pdxlocations/export-true-defaults
Export missing defaults when set False
2 parents 8a95ce4 + 0261313 commit 83d82c5

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

.vscode/launch.json

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7+
78
{
89
"name": "meshtastic BLE",
910
"type": "debugpy",
@@ -261,7 +262,14 @@
261262
"module": "meshtastic",
262263
"justMyCode": true,
263264
"args": ["--nodes", "--show-fields", "AKA,Pubkey,Role,Role,Role,Latitude,Latitude,deviceMetrics.voltage"]
264-
}
265-
265+
},
266+
{
267+
"name": "meshtastic --export-config",
268+
"type": "debugpy",
269+
"request": "launch",
270+
"module": "meshtastic",
271+
"justMyCode": true,
272+
"args": ["--export-config", "config.json"]
273+
},
266274
]
267275
}

meshtastic/__main__.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,11 +1122,32 @@ def subscribe() -> None:
11221122

11231123
# pub.subscribe(onNode, "meshtastic.node")
11241124

1125+
def set_missing_flags_false(config_dict: dict, true_defaults: set[tuple[str, str]]) -> None:
1126+
"""Ensure that missing default=True keys are present in the config_dict and set to False."""
1127+
for path in true_defaults:
1128+
d = config_dict
1129+
for key in path[:-1]:
1130+
if key not in d or not isinstance(d[key], dict):
1131+
d[key] = {}
1132+
d = d[key]
1133+
if path[-1] not in d:
1134+
d[path[-1]] = False
11251135

11261136
def export_config(interface) -> str:
11271137
"""used in --export-config"""
11281138
configObj = {}
11291139

1140+
# A list of configuration keys that should be set to False if they are missing
1141+
true_defaults = {
1142+
("bluetooth", "enabled"),
1143+
("lora", "sx126xRxBoostedGain"),
1144+
("lora", "txEnabled"),
1145+
("lora", "usePreset"),
1146+
("position", "positionBroadcastSmartEnabled"),
1147+
("security", "serialEnabled"),
1148+
("mqtt", "encryptionEnabled"),
1149+
}
1150+
11301151
owner = interface.getLongName()
11311152
owner_short = interface.getShortName()
11321153
channel_url = interface.localNode.getURL()
@@ -1185,6 +1206,8 @@ def export_config(interface) -> str:
11851206
else:
11861207
configObj["config"] = config
11871208

1209+
set_missing_flags_false(configObj["config"], true_defaults)
1210+
11881211
module_config = MessageToDict(interface.localNode.moduleConfig)
11891212
if module_config:
11901213
# Convert inner keys to correct snake/camelCase

meshtastic/tests/test_main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
onNode,
1919
onReceive,
2020
tunnelMain,
21+
set_missing_flags_false,
2122
)
2223
from meshtastic import mt_config
2324

@@ -1897,6 +1898,41 @@ def test_main_export_config(capsys):
18971898
# mo.assert_called()
18981899

18991900

1901+
@pytest.mark.unit
1902+
def test_set_missing_flags_false():
1903+
"""Test set_missing_flags_false() function"""
1904+
config = {
1905+
"bluetooth": {
1906+
"enabled": True
1907+
},
1908+
"lora": {
1909+
"txEnabled": True
1910+
}
1911+
}
1912+
1913+
false_defaults = {
1914+
("bluetooth", "enabled"),
1915+
("lora", "sx126xRxBoostedGain"),
1916+
("lora", "txEnabled"),
1917+
("lora", "usePreset"),
1918+
("position", "positionBroadcastSmartEnabled"),
1919+
("security", "serialEnabled"),
1920+
("mqtt", "encryptionEnabled"),
1921+
}
1922+
1923+
set_missing_flags_false(config, false_defaults)
1924+
1925+
# Preserved
1926+
assert config["bluetooth"]["enabled"] is True
1927+
assert config["lora"]["txEnabled"] is True
1928+
1929+
# Added
1930+
assert config["lora"]["usePreset"] is False
1931+
assert config["lora"]["sx126xRxBoostedGain"] is False
1932+
assert config["position"]["positionBroadcastSmartEnabled"] is False
1933+
assert config["security"]["serialEnabled"] is False
1934+
assert config["mqtt"]["encryptionEnabled"] is False
1935+
19001936
@pytest.mark.unit
19011937
@pytest.mark.usefixtures("reset_mt_config")
19021938
def test_main_gpio_rd_no_gpio_channel(capsys):

0 commit comments

Comments
 (0)