Skip to content

fix(common): 🐛 keep decimals in luxtronik2.write values - #813

Merged
rhammen merged 1 commit into
mainfrom
fix/811-write-service-decimals
Sep 25, 2026
Merged

rhammen merged 1 commit into
mainfrom
fix/811-write-service-decimals

Conversation

@rhammen

@rhammen rhammen commented Sep 25, 2026

Copy link
Copy Markdown
Collaborator

🔍 What this fixes

Resolves #811, reported and diagnosed by @webmaster-lars.

luxtronik2.write ran every value through int() before queuing it:

  • YAML, templates and Developer Tools send a float. -1.5 for ID_Einst_WK_akt (heating curve offset, 0.5 K steps) reached the pump as -1, with no error and no log entry. The post-write confirmation compared the truncated value with the read-back, both -1, so it passed.
  • The UI form sends a string, because value uses a text: selector. "22.5" was written correctly, since the Celsius datatype parses it, but the confirmation then compared the string "22.5" against the numeric read-back 22.5 and raised "did not confirm".

As a result, no decimal value could be written through the service without either silent truncation or an error.

✨ Changes

  • common.py: convert_to_int_if_possible is renamed to normalize_write_value. Every value still ends up as an int on the wire. The rule is:
    • Whole numbers become int. Identity datatypes (Unknown, Seconds, …) queue the value unchanged, and Luxtronik.write() skips anything that isn't an int, which is why the original int() was added in 3c052b5.
    • Fractional numbers stay float, so scaled datatypes (Celsius ×10, …) convert them to the raw int themselves.
    • Numeric strings are parsed the same way.
    • Bools, selection names ("Automatic"), "HH:MM" and nan/inf pass through unchanged.
  • lux_overrides.py: Energy2.to_heatpump now uses round(value * 100) instead of int(value * 100). 0.29 * 100 is 28.999… in binary floating point, so 0.29 was written as 28. The same kind of truncation, found while checking this one.

A fractional value sent to an identity-datatype parameter (e.g. 1.5 to an Unknown register) is no longer half-applied as 1. The library skips it and the confirmation raises an error. There's no scale that could turn it into a valid raw value, so an error is the correct result.

🧪 Tests

  • test_common.py: fractional float, whole-number float and string, numeric string, int, bool and non-finite cases. The old test asserting that "3.14" stays a string encoded the bug and is replaced.
  • test_init.py: a service-level test runs -1.5 and "-1.5" through the real SERVICE_WRITE_SCHEMA and asserts that async_write receives -1.5. The redundant pure-function test there is removed.
  • test_lux_overrides.py: Energy2 rounding for three values that used to truncate.

Full run: 1382 passed, 1 skipped, coverage 100% (unchanged). ruff check, ruff format --check, basedpyright (0 errors) and codespell are clean.

🤖 Generated with Claude Code

- the service ran every value through int(), so -1.5 on the 0.5 K heating
  curve offset reached the pump as -1 with no error, and the post-write
  confirmation compared the truncated value with itself and passed
- numeric strings (the service's text selector in UI mode) were written
  correctly but then failed confirmation against the numeric read-back
- rename convert_to_int_if_possible to normalize_write_value: integral
  numbers become int (identity datatypes need it, Luxtronik.write() skips
  non-ints), fractional numbers stay float for the scaled datatypes to
  convert, bools, selection names and nan/inf pass through unchanged
- Energy2.to_heatpump rounds instead of truncating (0.29 * 100 wrote 28)

Resolves #811

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Code Coverage

Package Line Rate Health
custom_components.luxtronik2 100% ✔
Summary 100% (3961 / 3961) ✔

@rhammen
rhammen merged commit e5a946b into main Sep 25, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

luxtronik2.write silently truncates decimal values (22.5 -> 22) because convert_to_int_if_possible() calls int() on floats

1 participant