Skip to content

Store capability value before firing "changed" flow triggers - #306

Merged
aivus merged 1 commit into
masterfrom
fix/305-capability-value-before-flow-trigger
Nov 21, 2026
Merged

aivus merged 1 commit into
masterfrom
fix/305-capability-value-before-flow-trigger

Conversation

@aivus

@aivus aivus commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Refs #305

Problem

Every capability listener (fan speed, turbo, 8°C heating, lights, X-Fan, vertical/horizontal swing, quiet, health, power save, sleep, fresh air) fired its <capability> changed flow trigger right after sending the command to the HVAC, before the new value was stored:

  • Action cards call triggerCapabilityListener(), which only runs the listener; app.js stores the value after it resolves.
  • Changes from the Homey UI are stored by Homey only after the listener resolves.

So a flow started by e.g. "Power save mode changed" that checks "Power save mode is On" read the previous value and took the wrong branch, which matches the symptom in #305. The device-polling path already stored the value first and wasn't affected.

Change

  • New _setCapabilityValueAndTrigger() helper in device.js: await setCapabilityValue(), then fire the trigger. All 12 listeners use it.
  • A rejected trigger is now logged via this.error instead of becoming an unhandled rejection.
  • Tests: mocks updated for setCapabilityValue/promise-returning triggers; added regression tests for the ordering and for a rejecting trigger.

Notes

  • Using Refs rather than Fixes: the reporter says Health mode works although it had the same ordering. That's likely down to the race between the trigger and the value write, but it should be confirmed by the reporter on a test build. Mode restrictions on the unit (e.g. power save only in Cool) may also play a part.
  • The action cards in app.js still call setCapabilityValue() after the listener; that's now a redundant but harmless second write, left as-is to keep this change focused.

Testing

  • npm test (eslint + Jest): 119 passed. Not yet tested on a real device.

🤖 Generated with Claude Code

Capability listeners fired their "<capability> changed" flow trigger
right after sending the command to the HVAC, before the new value was
stored. Homey only stores the value after the listener resolves (and
triggerCapabilityListener() from our action cards does not store it at
all), so flows started by the trigger evaluated conditions such as
"Power save mode is On" against the previous value and took the wrong
branch.

Store the value first, then fire the trigger, via a shared helper. A
rejected trigger is now logged instead of becoming an unhandled
rejection.

Refs #305

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aivus
aivus enabled auto-merge (squash) November 21, 2026 18:17
@aivus
aivus merged commit 4181be2 into master Nov 21, 2026
7 checks passed
@aivus
aivus deleted the fix/305-capability-value-before-flow-trigger branch November 21, 2026 18:18
aivus added a commit that referenced this pull request Nov 21, 2026
Resolved package-lock.json by taking master's and re-running npm install, so
the tree carries master's dependency bumps (homey 4.6.3, jest 30.5.2) plus
mqtt. package.json merged cleanly.

Mirrors #306 into the cloud driver: its capability listeners fired the
"<capability> changed" trigger before the new value was stored, so flows
started by the trigger read the previous value. The cloud device.js is a
deliberate copy of the local capability bridge (see CLAUDE.md), so the fix has
to be applied to both -- it gains the same _setCapabilityValueAndTrigger helper
and two tests covering the ordering and a rejecting trigger.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.

1 participant