Skip to content

Take the thermostat preset from the attribute cache on startup - #1043

Draft
zigpy-review-bot wants to merge 1 commit into
devfrom
zigpy-bot/audit/thermostat-preset-from-attribute-cache
Draft

zigpy-review-bot wants to merge 1 commit into
devfrom
zigpy-bot/audit/thermostat-preset-from-attribute-cache

Conversation

@zigpy-review-bot

Copy link
Copy Markdown
Collaborator

Summary

Severity: medium (the audit's estimate)

After a restart, the Moes, Beca and ZONNSMART thermostat entities show the preset none whatever preset the TRV is in, until the TRV reports operation_preset again. The preset was only taken from attribute events, and zigpy's restore of the attribute cache on startup fires none. While the entity wrongly says none, selecting none writes nothing (the TRV stays in boost or away), and in away the target temperature is read from and written to the occupied setpoint instead of the unoccupied one.

This takes the initial preset from the cached operation_preset when the entity is created.

Changes

  • Thermostat gets an _operation_presets table (operation_preset value to preset), empty by default. When it isn't empty and the cluster has an operation_preset attribute, __init__ sets the preset from the cached value; handle_attribute_updated sets it from operation_preset events.
  • MoesThermostat, BecaThermostat and ZONNSMARTThermostat define their table with the same values as before and drop their own handle_attribute_updated overrides, which held the same mapping as if chains. Values outside the table keep the current preset, as before.

Testing

  • test_preset_from_attribute_cache (Moes, Beca and ZONNSMART) caches operation_preset before the entity is built, checks the preset, and checks that selecting none writes the device's none value. It fails at dev 2da1a5b (the preset reads none for all three) and passes with the fix.
  • test_preset_without_operation_preset_attribute: a thermostat that matches MoesThermostat by manufacturer but has no quirk (no operation_preset) still gets its climate entity, with the preset none.
  • tests/test_climate.py: 92 passed. Full suite: 1785 passed. pre-commit (codespell, ruff, ruff format, mypy) passed.

Opened by the scheduled code audit; not requested by a maintainer.

Fixes #1042

The Moes, Beca and ZONNSMART thermostat entities only learned their preset from an operation_preset attribute event and started at "none". After a restart, zigpy restores operation_preset from its database before the entity is built, but no event fires, so the entity showed "none" until the device reported the attribute again. While in away, the target temperature was read from and written to the occupied setpoint, and selecting "none" wrote nothing, so the device stayed in its preset.

Map operation_preset values to presets in one table per class, used both for attribute events and to set the initial preset from the cache.
@zigpy-review-bot zigpy-review-bot added bot: code audit Found by the bot's scheduled code audit bot: unrequested PR PR: opened by the bot's issue triage, not requested by a maintainer bugfix This PR fixes a bug bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) labels Nov 21, 2026
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.49%. Comparing base (2da1a5b) to head (864b39b).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1043      +/-   ##
==========================================
+ Coverage   97.48%   97.49%   +0.01%     
==========================================
  Files          61       61              
  Lines       10870    10835      -35     
==========================================
- Hits        10597    10564      -33     
+ Misses        273      271       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@zigpy-review-bot zigpy-review-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No findings in this PR's own code. One open item concerns how it fits with two other open PRs. (This is a COMMENT on the bot's own PR; the would-be verdict is ready.)

Open items

  • The quirks-side replacement has the same startup gap. #996 removes MoesThermostat and ZONNSMARTThermostat in favour of the quirk entities from zigpy/zha-device-handlers#5624. That PR's OperationPresetThermostat sets _preset only in handle_attribute_updated (from _value_presets), so once #996 lands, these TRVs will show none after a restart again. zigpy/zha-device-handlers#5624 needs the same startup read. It subclasses ZHA's Thermostat, so it could name its table _operation_presets and inherit the read added here, once the quirks require a ZHA release that has it. Otherwise it can read the cache in its own __init__. That follow-up belongs to zigpy/zha-device-handlers#5624, not to this PR.
  • Overlap with #906. That PR deletes BecaThermostat because its presets don't work on the _TZE200_b6wax7g0 TRV, and this PR changes that class. Whichever merges second drops or rebases the Beca part. Which way Beca goes is the maintainers' call.

The tables carry the same values as the event mapping they replace, so this needs no device test beyond what that mapping already assumed. Taking the PR out of draft is a maintainer's call.

Verified (7 checks)
  • The three tables match the removed if chains value for value (on ZONNSMART, 2 and 3 both map to holiday and 4 to frost protect). On events, a value outside a table leaves the preset unchanged, as before. At startup it gives none.
  • zigpy 2.4.1's appdb restores the attribute cache with _attr_cache.set_value(...) directly and fires no event, which is the cause the body names.
  • Cluster.get("operation_preset") returns None for an uncached or unsupported attribute, so the preset then starts at none. The quirks type operation_preset as an enum8, whose members hash as their int value (checked against the installed zha-quirks 2.4.1), so the int-keyed tables match both the restored value and event values.
  • No other Thermostat subclass overrides handle_attribute_updated. The away reset in _handle_attribute_updated still runs after the table update, as before.
  • test_preset_from_attribute_cache fails for all three thermostats with origin/dev's climate module (the preset reads none) and passes at the head.
  • At 864b39b: tests/test_climate.py 92 passed, full suite 1785 passed, mypy clean, no pyproject.toml change. CI is green.
  • Copilot CLI second opinion (static, adversarial brief): no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot: code audit Found by the bot's scheduled code audit bot: unrequested PR PR: opened by the bot's issue triage, not requested by a maintainer bot-watch: fix zigpy-review-bot follows this thread and also pushes fixes (review + fix) bugfix This PR fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Tuya TRV thermostat preset shows none after every restart, and selecting none then does nothing

1 participant