Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ crc32fast = "1"
ratatui = { version = "0.30", default-features = false, features = ["crossterm", "underline-color"], optional = true }
crossterm = { version = "0.29", features = ["event-stream"], optional = true }
nucleo-matcher = { version = "0.3", optional = true }
loxone-config = { path = "crates/loxone-config", version = "0.15.0" }
loxone-config = { path = "crates/loxone-config", version = "0.16.0" }
loxone-client = { path = "crates/loxone-client", version = "0.1.3" }
unicode-width = { version = "0.2", optional = true }
unicode-segmentation = { version = "1", optional = true }
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/i299-unit-suffix.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- **`loxl lint`** reports bare durations also on extern port assignments (`x.TimeEnd = 120`), template value parameters and instance arguments, and `--fix` writes their units across the files of a module. The compiled config does not change.
- **Lox Logic** knows the units of the Virtual HTTP Input from Loxone's documentation: `Poll` in seconds, `Timeout` in milliseconds (`Timeout: 4s` compiles to `4000`). Modbus settings have no documented unit and stay bare numbers.
2 changes: 1 addition & 1 deletion crates/loxone-config/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "loxone-config"
version = "0.15.0"
version = "0.16.0"
edition = "2024"
rust-version = "1.88"
description = "The Loxone Config program file as a library — lossless .Loxone model, UUIDs, backup ZIPs, semantic diff — and Lox Logic, a text language compiled into it (compile/decompile/adopt, lockfile, simulated tests)"
Expand Down
2 changes: 1 addition & 1 deletion crates/loxone-config/docs/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ it only when it matches what you intended.
| `` `defaults` … is not a value `` / `takes no label string` | a wire, expression or label in `defaults` | write it on each block; `defaults` takes values only |
| `says which object a block is` | `device:`/`mirrors:` in `defaults` | write it on each block |
| `cannot adopt` + `would set it to` (from `adopt --uuid`) | the adopted block leaves a defaulted port unset | take the port out of the `defaults` statement and bind it on the other blocks, or set the port on the block in Loxone Config |
| `unit-suffix` (from `lint`) | a bare time value on a port documented in seconds or minutes (`Delay: 10800`) | `loxl lint --fix` writes the unit (`Delay: 3h`); the compile output does not change. The language server offers it as a quick fix |
| `unit-suffix` (from `lint`) | a bare time value on a port documented in milliseconds, seconds or minutes (`Delay: 10800`), also in a port assignment or a template parameter | `loxl lint --fix` writes the unit (`Delay: 3h`); the compile output does not change. The language server offers it as a quick fix |
| `default-page` (from `lint`) | a fragment of a directory module without its own `page` line, while others have one | add the `page` line; the language server offers the previous file's page as a quick fix |
| `dead-outputs` (from `lint`) | a block, extern or device whose outputs reach nothing in the compiled config: no wire, not shown in the app as a value, no statistic, no energy flow, no message | wire it, delete it, or keep it with `# lint: allow dead-outputs <reason>` on the line above |
| `tile-dead-command` (from `lint`) | Touch Pure Flex commands for a block type that no block linked to the device has | link a block of that type, empty the tiles (`tile 2()`), or `# lint: allow tile-dead-command` above the extern |
Expand Down
59 changes: 59 additions & 0 deletions crates/loxone-config/docs/decisions/0111-unit-suffix-coverage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# D111: Unit suffixes on extern ports, template parameters and device settings

Status: accepted (2026-11-01), extends [D87](0087-v2-tooling.md)

## Context

The `unit-suffix` lint ([D87](0087-v2-tooling.md)) looked at block and `defaults` parameters only.
Two places with bare time values were left out:

- port assignments on externs (`x.TimeEnd = 120`);
- template value parameters (`zeit_hoch = 75`) and the instances' arguments for them. On the house
config these were the 34 remaining bare durations: every blind instance passes its run times
this way.

The settings of virtual and Modbus devices (`Poll: 10`, `Timeout: 4000`, `MaxTime: 300000`) had no
documented unit at all. The TechDoc scrape behind the port facts ([D52](0052-port-facts.md))
covers function blocks, not the device tree.

## Decision

**Extern assignments take the extern's declared type.** That is the type the compile scales a
suffix with, so the lint uses the same rule as on a block. A bulk line `(a, b).Port = 300` changes
only as a whole, when every member gets the same spelling: the line stays one line.

**A template value parameter takes a suffix when its uses agree.** The fix collects every use of
the parameter in the body: a block parameter (`Time: zeit`) or a port assignment on an object
parameter or extern (`j.TimeEnd = zeit`). The default and each instance's argument get a suffix
only when all uses give the same spelling, and only when the body names the parameter nowhere
else. A title, an expression or a port without a unit would read a suffixed value differently,
so such a parameter keeps its numbers. An instance argument that names no parameter goes to the
primary block, and the lint uses that block's port.

**`lint --fix` sees the whole module.** In a directory module, templates and externs often live
in another file than the instances and assignments that use them. Each file is fixed with the
whole module as context.

**Device settings get units from the knowledge base, never by guess.** A hand-written table
(`src/settings_meta.rs`, documented in loxone-format.md) holds the unit and range of a setting
where a Loxone page states it:

- `VirtualHttpIn` `Poll` in seconds and `Timeout` in milliseconds, with their ranges (reference
`kb-virtual-http-input`). The corpus agrees: every `Timeout` is the mint default 4000, every
`Poll` at least the documented minimum 10.
- No Loxone page gives the unit of the Modbus server's `Timeout` or the Modbus sensors'
`ModbusPollingCycle`, `MinTime` and `MaxTime` (reference `kb-modbus-tcp` names them without a
unit). They stay bare numbers until a document states one.

**Milliseconds are a port unit.** A time suffix scales exactly into milliseconds there (`4s` →
`4000`); decompile, adopt and the lint write `4s` for `4000`. Before, a suffix on such a port
scaled into seconds, the default for an undocumented port, so `Timeout: 4s` compiled to `4`. No
config in the repository or the house config wrote one.

## Rejected

- **Reading the unit off the value range.** `MaxTime: 300000` looks like milliseconds, but
`MinTime` is 1000 on one Modbus sensor type and 10 on the other. A guessed unit would compile a
suffix to the wrong value.
- **Suffixing a parameter by its first use.** A parameter used on a seconds port and a minutes port
would compile differently from its bare number.
1 change: 1 addition & 0 deletions crates/loxone-config/docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,5 @@ decision, so `grep -r D42` finds it. A new decision takes the next number and a
| D89 | [What dead-outputs counts as used, and `# lint: allow`](decisions/0089-lint-dead-outputs.md) | accepted |
| D91 | [`off` is no attribute, except on a virtual input](decisions/0091-off-is-no-attribute.md) | accepted |
| D101 | [Hardware declarations](decisions/0101-hardware-declarations.md) | accepted |
| D111 | [Unit suffixes on extern ports, template parameters and device settings](decisions/0111-unit-suffix-coverage.md) | accepted, extends [D87](decisions/0087-v2-tooling.md) |
| D112 | [0 is no attribute on a number a save leaves out](decisions/0112-zero-is-no-attribute.md) | accepted |
14 changes: 11 additions & 3 deletions crates/loxone-config/docs/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,25 @@ Notes:
`1.5h`, `2700K`, `70%`. `loxl types <Type>` shows each port's documented unit.
- Time units scale exactly into the port's unit. That is seconds by default (`1.5h` compiles to
`Def="5400"`, byte-identical to `5400`), minutes on a minutes port (`AutoShadeTime: 2h` compiles
to `120`). A value that is not an exact decimal number of minutes (`10s`) is an error there.
to `120`), milliseconds on a milliseconds port (`VirtualHttpIn`'s `Timeout: 4s` compiles to
`4000`). A value that is not an exact decimal number of minutes (`10s`) is an error there.
- `%` and `K` (color temperature) have factor 1. They are accepted only on a port documented in
that unit, or on an undocumented one. A time suffix on a `%`, `K` or `°` port is an error.
- A suffixed `let` scales per use, into each port's unit.
- The suffix is part of the canonical spelling: `1.5h` stays `1.5h` through `fmt`.
- An unknown suffix is a parse error. A quoted `"40s"` stays a string.
- Decompile and adopt write the suffix on a port documented in seconds or minutes: the largest of
- Decompile and adopt write the suffix on a port documented in milliseconds, seconds or minutes:
the largest of
`h`, `min`, `s`, `ms` that gives a whole number (`10800` → `3h`, `0.5` → `500ms`, `58` → `58s`),
and only when it compiles back to the same digits. Zero and the other ports stay bare.
- `loxl lint` reports a bare number that this rule would write with a suffix (`unit-suffix`:
`Delay: 10800` → `Delay: 3h`), on blocks, in `defaults` and in template bodies.
`Delay: 10800` → `Delay: 3h`), on blocks, in `defaults`, on extern port assignments
(`x.Time = 300` → `x.Time = 5min`) and in template bodies. A template value parameter
(`zeit = 75`) and an instance's argument for it count when every use of the parameter in the
body is a port of the same unit; an argument the instance forwards to its primary block counts
by that block's port. A bulk line `(a, b).Port = 300` changes only as a whole.
- A setting with no unit in Loxone's documentation stays a bare number: the Modbus server's
`Timeout`, the Modbus sensors' `ModbusPollingCycle`, `MinTime` and `MaxTime`.
`loxl lint --fix` writes the suffixes; the compile output stays byte-identical, and in a project
the fix is refused if it would not.
- **Reserved words** can never be a name: the statement keywords `let`, `extern`, `removed`,
Expand Down
23 changes: 23 additions & 0 deletions crates/loxone-config/docs/loxone-format.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,29 @@ Lox Logic writes a bound 0 as no attribute ([D112](decisions/0112-zero-is-no-att
calendar entries or the physical inputs and outputs; `MinVal` is absent on 112 of 171 `VirtualIn`s,
`ModbusDataType` on 149 of 339 Modbus sensors); oracle sessions 37, 61 and 62 (Loxone Config 17.2.8.28).

### Units of the settings

The unit a setting's attribute is in, where a Loxone knowledge-base page states it. The table is
`src/settings_meta.rs`; value suffixes scale into these units.

| Object | Attribute | Property | Unit | Range | Source |
|---|---|---|---|---|---|
| `VirtualHttpIn` | `Poll` | Polling cycle | s | 10…604800 | `kb-virtual-http-input`, *Properties* |
| `VirtualHttpIn` | `Timeout` | Timeout | ms | 10…8000 | `kb-virtual-http-input`, *Properties* |
| `VirtualHttpIn` | `Allowedtimeouts` | Number of permitted timeouts | none | 0…100 | `kb-virtual-http-input`, *Properties* |

The attributes match the properties by name. The corpus agrees: all 19 `VirtualHttpIn`s have
`Timeout="4000"` (the mint default), and every `Poll` is 10 or more (10 to 3600).

No Loxone page states the unit of these settings, so they stay bare numbers:

- `ModbusServer` `Timeout`. Reference `kb-modbus-tcp` (German; there is no English page) names the
server's timeout and the sensors' polling cycle, without units. The Modbus Air and Modbus
Extension pages document only RS-485 timing ("Timeout: 1000ms" in Auto mode), not these
attributes.
- `ModbusASensor` and `ModbusSensor` `ModbusPollingCycle`, `MinTime`, `MaxTime`.
- `VirtualUdpInCmd` `MinTime`.

## Settings of physical inputs and outputs

Inputs and outputs of extensions, Tree and Air devices and 1-Wire sensors carry their settings as attributes:
Expand Down
19 changes: 12 additions & 7 deletions crates/loxone-config/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use loxone_config::logic::{
CompileOptions, DecompileOptions, DecompileScope, Item, LintFinding, LintKind, Module,
TestPlan, TestResult, adopt, adopt_one, adopt_pages, apply_rekeys, compile, decompile,
decompile_pages, finish_lint, fix_unit_suffixes, lint_access, lint_dead_outputs, lint_source,
lint_tiles, lock_rekeys, lox_specs, plan_tests, read_results, rename_slug, run_tests, slugify,
trace_tests, valid_slug, validate_ports,
decompile_pages, finish_lint, fix_unit_suffixes_in, lint_access, lint_dead_outputs,
lint_source, lint_tiles, lock_rekeys, lox_specs, plan_tests, read_results, rename_slug,
run_tests, slugify, trace_tests, valid_slug, validate_ports,
};
use loxone_config::uuid::parse_serial;
use loxone_config::{Lockfile, LoxoneDoc, Project};
Expand Down Expand Up @@ -121,7 +121,8 @@ Needs a lox.toml project. Also rewrites the project's out file.
summary: "Report unused and dead parts of a module",
details: r#"On any module, reports unused externs and constants, templates nothing uses, values
outside a port's documented range and bare time values that read better with their unit
(`unit-suffix`: `Delay: 10800` is `Delay: 3h`). In a directory module it reports a file whose
(`unit-suffix`: `Delay: 10800` is `Delay: 3h`; also extern port assignments and template
parameters). In a directory module it reports a file whose
blocks come before its first `page` statement while other files name their page (those blocks go
to the default page).
In a lox.toml project it also reports:
Expand Down Expand Up @@ -1437,11 +1438,15 @@ fn cmd_lint(args: &[&str]) -> Result<ExitCode, AnyError> {
if fix {
let mut fixed = Vec::new();
let mut count = 0;
let mut parsed = Vec::new();
for f in &files {
let src = std::fs::read_to_string(f).map_err(|e| format!("{}: {e}", f.display()))?;
let mut m =
Module::parse_fragment(&src).map_err(|e| format!("{}: {e}", f.display()))?;
let n = fix_unit_suffixes(&mut m).len();
parsed.push(Module::parse_fragment(&src).map_err(|e| format!("{}: {e}", f.display()))?);
}
// Externs and templates may live in another file of the module.
let whole = Module::from_fragments(parsed.clone());
for (f, mut m) in files.iter().zip(parsed) {
let n = fix_unit_suffixes_in(&mut m, &whole).len();
if n > 0 {
count += n;
fixed.push((f.clone(), m.to_text()));
Expand Down
3 changes: 3 additions & 0 deletions crates/loxone-config/src/connectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ pub fn file_params(block_type: &str) -> &'static [&'static str] {
/// time suffixes scale into seconds, `%`/`K` are annotations.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PortUnit {
Milliseconds,
Seconds,
Minutes,
Percent,
Expand All @@ -1383,6 +1384,7 @@ pub enum PortUnit {
impl PortUnit {
pub fn describe(self) -> String {
match self {
PortUnit::Milliseconds => "milliseconds".into(),
PortUnit::Seconds => "seconds".into(),
PortUnit::Minutes => "minutes".into(),
PortUnit::Percent => "percent".into(),
Expand All @@ -1396,6 +1398,7 @@ impl PortUnit {
pub fn port_unit(block_type: &str, port: &str) -> Option<PortUnit> {
let unit = crate::port_meta::port_meta(block_type, port)?.unit?;
Some(match unit {
"ms" => PortUnit::Milliseconds,
"s" => PortUnit::Seconds,
"min" => PortUnit::Minutes,
"%" => PortUnit::Percent,
Expand Down
1 change: 1 addition & 0 deletions crates/loxone-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ pub mod project;
pub mod render;
pub(crate) mod scan;
pub(crate) mod settings;
pub(crate) mod settings_meta;
pub mod sim;
pub mod uuid;
pub mod xml;
Expand Down
4 changes: 3 additions & 1 deletion crates/loxone-config/src/logic/adopt_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ pub(super) fn adopt_device(
{
Value::Bool(v == "true") // `on`/`off`
} else {
value_of(&v)
// A duration reads in its unit (`4s`, not `4000`).
crate::logic::ast::duration_value(&o.block_type, name, &v)
.unwrap_or_else(|| value_of(&v))
},
),
comment: None,
Expand Down
29 changes: 28 additions & 1 deletion crates/loxone-config/src/logic/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,15 @@ impl fmt::Display for Unit {
}
}

/// The duration `raw` (a `Def=` value) of a port documented in seconds or
/// The duration `raw` (a `Def=` value) of a port documented in milliseconds, seconds or
/// minutes, spelled with the largest time suffix that writes it as a whole
/// number (`300` s → `5min`, `0.3` s → `300ms`, `7200` s → `2h`). `None`
/// for a port of any other unit or none documented, for zero, and where no
/// suffix reproduces `raw` exactly: the value then stays a plain number.
pub(crate) fn duration_value(block_type: &str, port: &str, raw: &str) -> Option<Value> {
let pu = port_unit(block_type, port)?;
let per_ms: u128 = match pu {
PortUnit::Milliseconds => 1,
PortUnit::Seconds => 1000,
PortUnit::Minutes => 60_000,
_ => return None,
Expand Down Expand Up @@ -292,6 +293,9 @@ pub(crate) fn scale_for_port(
match port_unit {
None => scale_by_unit(number, unit).ok_or_else(overflow),
Some(PortUnit::Seconds) if is_time => scale_by_unit(number, unit).ok_or_else(overflow),
Some(PortUnit::Milliseconds) if is_time => scale_by_unit(number, unit)
.and_then(|seconds| scale_decimal(&seconds, 1000, 0))
.ok_or_else(overflow),
Some(PortUnit::Minutes) if is_time => {
let seconds = scale_by_unit(number, unit).ok_or_else(overflow)?;
divide_exact(&seconds, 60).ok_or_else(|| {
Expand Down Expand Up @@ -368,6 +372,11 @@ fn gcd(mut a: u128, mut b: u128) -> u128 {
/// `250`÷1000 → `0.25`). `None` when the digits overflow the exact range.
pub(crate) fn scale_by_unit(number: &str, unit: Unit) -> Option<String> {
let (mul, shift) = unit.factor();
scale_decimal(number, mul, shift)
}

/// `number · mul / 10^shift` as an exact decimal literal.
fn scale_decimal(number: &str, mul: u128, shift: u32) -> Option<String> {
let (neg, rest) = match number.strip_prefix('-') {
Some(r) => (true, r),
None => (false, number),
Expand Down Expand Up @@ -2879,6 +2888,24 @@ mod tests {
assert_eq!(scale_for_port("-30", Unit::Min, minutes).unwrap(), "-30");
let err = scale_for_port("10", Unit::S, minutes).unwrap_err();
assert!(err.contains("exact number of minutes"), "{err}");
let ms = Some(PortUnit::Milliseconds);
assert_eq!(scale_for_port("4", Unit::S, ms).unwrap(), "4000");
assert_eq!(scale_for_port("250", Unit::Ms, ms).unwrap(), "250");
assert_eq!(scale_for_port("1.5", Unit::Min, ms).unwrap(), "90000");
assert_eq!(
duration_value("VirtualHttpIn", "Timeout", "4000"),
Some(Value::Unit {
number: "4".into(),
unit: Unit::S
})
);
assert_eq!(
duration_value("VirtualHttpIn", "Timeout", "250"),
Some(Value::Unit {
number: "250".into(),
unit: Unit::Ms
})
);

assert_eq!(
scale_for_port("70", Unit::Pct, Some(PortUnit::Percent)).unwrap(),
Expand Down
Loading
Loading