Skip to content

Add Adaptive Lighting controller - #589

Open
xDhii wants to merge 9 commits into
ikalchev:devfrom
xDhii:adaptive-lighting-controller
Open

xDhii wants to merge 9 commits into
ikalchev:devfrom
xDhii:adaptive-lighting-controller

Conversation

@xDhii

@xDhii xDhii commented Dec 2, 2026

Copy link
Copy Markdown

Second of two PRs — the controller side. Depends on #588 (the codec); this branch is based on it.

What it adds

  • AdaptiveLightingController: wires the transition codec to a Lightbulb service. It parses TransitionControl writes, evaluates the curve on a loop.call_later timer (clock injectable), and reports colour temperature plus the interpolated brightness factor through an on_apply_temperature callback. It notifies ActiveTransitionCount on arm/disarm, clamps mireds to the characteristic range, rejects curves with fewer than two points, and ignores malformed read requests instead of failing the write.
  • Persistence: the active transition is stored via driver.accessory_state()/async_persist() and restored on start; restore() drops expired or unreadable schedules, and stop() preserves them for the next accessory.
  • update_now() and discard_applied() let a consumer re-evaluate after external state changes and force a rewrite of an unchanged value.
  • The thread/loop contract is documented: characteristic setters run on the driver loop, restore() and async_persist() must be called there.
  • The accessories/adaptive_lightbulb.py example is rewritten on top of the controller.

Tests

62 tests in tests/test_adaptive_lighting.py covering writes, interpolation timing, persistence round-trips, restore after restart, malformed payloads and the public API. The full suite and ruff check/ruff format pass locally.

Where it comes from

Same source as #588: ported from my Home Assistant HomeKit bridge change (home-assistant/core#182265). The glue built on this library has been running in my production Home Assistant since December 1st without errors.

I used AI assistance (Devin and Claude) to port and test this.

devin-ai-integration Bot and others added 9 commits December 1, 2026 14:33
First step towards a reusable AdaptiveLightingController (issue ikalchev#495):
decode the TransitionControl TLV8 schedule the Home app writes, expose
the parsed curve and interpolate the current colour temperature and
brightness factor at any moment, and serialise the state so a consumer
can persist it. No controller or HomeKit service wiring yet.

- pyhap.tlv.decode_ordered: TLV8 decode preserving record order,
  duplicates and empty records (used as list delimiters), rejoining
  255-byte chunks
- pyhap.adaptive_lighting: variable-width uint codec, TLV helpers,
  CurveEntry/ActiveTransition model, parse_transition_control /
  parse_transition_update with injectable clock, interpolate /
  interpolate_point with hold-duration and brightness-factor
  interpolation, ActiveTransition.to_dict/from_dict
- tests cover the real Home app payload, curve end, hold windows,
  brightness factor smoothness, malformed writes and state roundtrip

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parse_transition_update now looks up the configuration record by its
TAG_CFG_IID tag instead of assuming it is the first record, so updates
carrying extra records still parse. decode_ordered documents that
rejoining same-tag chunks is a heuristic bounded by the 255-byte
length rule and its inherent ambiguity.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Wires a Lightbulb service to the Adaptive Lighting protocol: obtains
or creates the ActiveTransitionCount, TransitionControl and
SupportedTransitionConfiguration characteristics, advertises the
supported transition configuration with the real iids, decodes the
schedules the Home app writes (arming, renewing and disabling via the
configuration marker), answers read-current-transition requests with
the stored schedule and reports elapsed time through an injectable
clock. The active transition is kept on the controller for a follow-up
that will evaluate and apply it; no timers, persistence or accessory
writes are included yet.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Evaluate the armed curve on the consumer's event loop: the controller
applies the interpolated colour temperature, shifted by the brightness
adjustment factor and clamped to the characteristic range, through an
on_apply_temperature callback once per update interval, deduplicating
unchanged mireds. Disabling, reaching the end of the curve or stop()
cancel the timer, and a client write to ColorTemperature disarms the
schedule while still reaching the consumer's own setter. Persistence
remains with the consumer.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
The controller now saves the active transition into the accessory state
on arm and clears it on disable, and restore() re-arms a still valid
schedule after a restart, discarding expired or unreadable payloads.
The adaptive_lightbulb example is rewritten on top of
AdaptiveLightingController instead of the stale external tlv8 package,
and the docs cover the new persistence hook.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Reject malformed or incomplete transition schedules by raising from the
setter so the driver fails the write instead of acknowledging it, keep
the running transition untouched and skip undecodable read requests
instead of failing the whole write. Replace the iid asserts with
ValueError and clamp the update interval to the asyncio timer limit.

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
ActiveTransitionCount now notifies subscribers on arm (1) and disable
(0), matching HAP-NodeJS. The notify_threshold stays consumer-owned:
the controller reports every applied point and the consumer paces the
characteristic notifications, as shown by the updated example which
also notifies exactly once per applied point.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
A brightness of 0 is a real value and now clamps to min_multiplier
instead of falling back to max_multiplier. The class docstring and
state docs spell out the driver-loop contract: setter callbacks,
restore() and async_persist all run on driver.loop, which must be the
loop passed to the controller, and restore() stays a no-op after stop().

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Consumers can re-evaluate the curve on demand when the light state changed outside the schedule, and force the next evaluation to rewrite the colour by discarding the deduplicated value.

Generated with [Devin](https://devin.ai)

Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 03:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

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.

2 participants