diff --git a/meshterm/core/device_config.py b/meshterm/core/device_config.py index 010503f..5f280a2 100644 --- a/meshterm/core/device_config.py +++ b/meshterm/core/device_config.py @@ -53,6 +53,9 @@ class SettingSpec: the connected hardware actually supports. max_length: For ``str`` values, the longest accepted string (protocol field widths, e.g. the flood scope's 31-byte name slot). + decimals: For ``float`` values, the fixed decimal places a value is shown with and + rounded to on parse, so what is applied is what the row showed; ``None`` keeps + the value as given. validate: A last rule for a typed, in-range value, given the snapshot when there is one: it returns the complaint, or ``None``. For what a bound can't state — the PIN's "zero or six digits", relaying only on an allowed frequency. @@ -73,6 +76,7 @@ class SettingSpec: strict_choices: bool = True max_key: str | None = None max_length: int | None = None + decimals: int | None = None validate: Callable[[Any, dict | None], str | None] | None = None @@ -137,6 +141,8 @@ def _parse_typed(spec: SettingSpec, raw: Any, snapshot: dict | None) -> Any: try: if spec.value_type == "float": value: Any = float(text) + if spec.decimals is not None: + value = round(value, spec.decimals) else: # int or enum value = int(text, 0) if isinstance(raw, str) else int(raw) except (ValueError, TypeError) as exc: @@ -199,6 +205,8 @@ def format_value(spec: SettingSpec, value: Any) -> str: return "true" if value else "false" if spec.value_type == "enum" and spec.choices is not None and value in spec.choices: return f"{value} ({spec.choices[value]})" + if spec.value_type == "float" and spec.decimals is not None: + return f"{float(value):.{spec.decimals}f}" return str(value) @@ -694,6 +702,7 @@ DEVICE_SETTINGS: list[SettingSpec] = [ "float", minimum=0.0, maximum=20.0, + decimals=1, getter=_get("rx_delay"), apply=_tuning_apply("rx_delay"), ), diff --git a/meshterm/core/remote_config.py b/meshterm/core/remote_config.py index 4f9f3fb..ec0cc65 100644 --- a/meshterm/core/remote_config.py +++ b/meshterm/core/remote_config.py @@ -328,7 +328,7 @@ REPEATER_SETTINGS: tuple[RemoteSetting, ...] = ( label="TX delay", category="Repeating", kind="float", - decimals=2, + decimals=1, minimum=0, maximum=2, help="Delay factor before rebroadcasting a flood packet", @@ -338,7 +338,7 @@ REPEATER_SETTINGS: tuple[RemoteSetting, ...] = ( label="Direct TX delay", category="Repeating", kind="float", - decimals=2, + decimals=1, minimum=0, maximum=2, help="Delay factor before forwarding a directed packet", @@ -348,7 +348,7 @@ REPEATER_SETTINGS: tuple[RemoteSetting, ...] = ( label="RX delay", category="Repeating", kind="float", - decimals=2, + decimals=1, minimum=0, maximum=20, help="Base delay before acting on a received packet", @@ -748,7 +748,7 @@ def _format_number(spec: RemoteSetting, value: float) -> str: def normalize_value(spec: RemoteSetting, raw: str) -> str: """A validated typed value in the form the cache stores and the firmware is sent. - TX delay entered as ``0.5`` becomes ``0.50``: rounded to the setting's decimals, and + TX delay entered as ``0.33`` becomes ``0.3``: rounded to the setting's decimals, and spelled exactly as a read of the same value would be, so staging what the node already holds unstages rather than queueing a no-op ``set``. """ diff --git a/tests/test_device_config.py b/tests/test_device_config.py index 5685172..e8ed57c 100644 --- a/tests/test_device_config.py +++ b/tests/test_device_config.py @@ -1264,6 +1264,14 @@ def test_tuning_fields_are_floats_with_firmware_ranges() -> None: parse_value(get_spec("airtime_factor"), "1000") +def test_rx_delay_is_rounded_to_one_place() -> None: + """RX delay shows and applies at one decimal, matching the repeater admin's delays.""" + spec = get_spec("rx_delay") + assert parse_value(spec, "0.3333") == 0.3 + assert format_value(spec, 0.123) == "0.1" + assert format_value(spec, 0.0) == "0.0" + + def test_tx_delay_factors_are_not_companion_settings() -> None: """The repeater-only TX delay factors must not appear as (no-op) device settings. diff --git a/tests/test_gallery.py b/tests/test_gallery.py index f41ea2f..1f3df87 100644 --- a/tests/test_gallery.py +++ b/tests/test_gallery.py @@ -823,10 +823,10 @@ def _repeater_admin(cols: int, rows: int) -> Screen: now = utcnow() cache = { "name": CachedValue("Hilltop-Repeater", now), - "txdelay": CachedValue("0.50", now), + "txdelay": CachedValue("0.5", now), "bridge.delay": CachedValue("", now, supported=False), } - title, items = _admin_menu_items(hub, cache, {"txdelay": "1.25"}) + title, items = _admin_menu_items(hub, cache, {"txdelay": "1.5"}) return AdminMenu(title, items, footer_hint="↑↓ move · type to filter · Enter select · Esc back") diff --git a/tests/test_repeater_admin.py b/tests/test_repeater_admin.py index 414f2ff..5ce722c 100644 --- a/tests/test_repeater_admin.py +++ b/tests/test_repeater_admin.py @@ -121,25 +121,25 @@ def test_catalog_carries_the_repeater_only_knobs() -> None: """TX delay and Direct TX delay — the reason this feature exists — are first-class.""" txdelay = get_setting("txdelay") direct = get_setting("direct.txdelay") - assert txdelay is not None and txdelay.set_command("0.50") == "set txdelay 0.50" + assert txdelay is not None and txdelay.set_command("0.5") == "set txdelay 0.5" assert direct is not None and direct.get_command == "get direct.txdelay" assert any(s.key == "af" for s in REPEATER_SETTINGS) assert any(s.key == "advert.interval" for s in REPEATER_SETTINGS) -def test_delays_are_floats_rounded_to_two_places() -> None: - """TX, Direct TX and RX delay read, validate and send as two-decimal floats. +def test_delays_are_floats_rounded_to_one_place() -> None: + """TX, Direct TX and RX delay read, validate and send as one-decimal floats. Regression: they were typed ``int``, so a firmware ``0.5`` showed as ``0`` and typing ``0.5`` back was refused as "not a whole number". """ for key in ("txdelay", "direct.txdelay", "rxdelay"): spec = get_setting(key) - assert spec.kind == "float" and spec.decimals == 2 - assert parse_reply_value(spec, "> 0.5") == "0.50" - assert parse_reply_value(spec, "> 1.2345678") == "1.23" + assert spec.kind == "float" and spec.decimals == 1 + assert parse_reply_value(spec, "> 0.5") == "0.5" + assert parse_reply_value(spec, "> 1.2345678") == "1.2" assert validate_value(spec, "0.5") is True - assert normalize_value(spec, "0.3333") == "0.33" + assert normalize_value(spec, "0.3333") == "0.3" assert "≤ 2" in validate_value(get_setting("txdelay"), "2.5") assert validate_value(get_setting("rxdelay"), "12.5") is True # RX delay runs to 20 @@ -481,7 +481,7 @@ async def test_read_settings_reads_every_setting_asking_each_question_once( assert len(sent) == len(set(sent)) == len(read_plan(REPEATER_SETTINGS)) cache = tui_ctx.remote_store.settings(NODE) assert set(cache) == {s.key for s in REPEATER_SETTINGS} - assert cache["txdelay"].value == "0.50" and cache["direct.txdelay"].value == "0.20" + assert cache["txdelay"].value == "0.5" and cache["direct.txdelay"].value == "0.2" assert (cache["freq"].value, cache["bw"].value, cache["sf"].value) == ("910.525", "62.5", "7") assert cache["path.hash.mode"].value == "0" assert cache["dutycycle"].value == "50.0" @@ -521,7 +521,7 @@ async def test_apply_reads_the_radio_first_then_sends_one_line(tui_ctx, admin_de ctx = tui_ctx session = ctx.ui.session ctx.remote_store.remember_setting(NODE, "af", "1.00") - pending = {"sf": "9", "txdelay": "1.25", "dutycycle": "40.0"} + pending = {"sf": "9", "txdelay": "1.5", "dutycycle": "40.0"} task = asyncio.ensure_future(repeater_admin._apply(ctx, admin_device, NODE, pending)) summary = await _step_until( @@ -539,12 +539,12 @@ async def test_apply_reads_the_radio_first_then_sends_one_line(tui_ctx, admin_de assert admin_device.sent == [ "get radio", "set radio 910.525,62.5,9,5", - "set txdelay 1.25", + "set txdelay 1.5", "set dutycycle 40.0", ] assert applied == 3 and pending == {} cache = ctx.remote_store.settings(NODE) - assert cache["sf"].value == "9" and cache["txdelay"].value == "1.25" + assert cache["sf"].value == "9" and cache["txdelay"].value == "1.5" assert "af" not in cache # the other spelling of the duty cycle is stale now @@ -554,13 +554,13 @@ async def test_apply_reads_the_radio_first_then_sends_one_line(tui_ctx, admin_de def test_value_lane_shows_the_value_without_its_age() -> None: """The value lane is the value: no age stamp, and its own word for each absence.""" cache = { - "txdelay": CachedValue("0.50", utcnow() - timedelta(hours=3)), + "txdelay": CachedValue("0.5", utcnow() - timedelta(hours=3)), "bridge.delay": CachedValue("", utcnow(), supported=False), "guest.password": CachedValue("", utcnow()), "path.hash.mode": CachedValue("1", utcnow()), } lane = repeater_admin._value_text - assert lane(get_setting("txdelay"), cache, {}).plain == "0.50" + assert lane(get_setting("txdelay"), cache, {}).plain == "0.5" assert lane(get_setting("bridge.delay"), cache, {}).plain == "n/a" assert lane(get_setting("guest.password"), cache, {}).plain == "empty" assert lane(get_setting("rxdelay"), cache, {}).plain == "?"