Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 parsesTransitionControlwrites, evaluates the curve on aloop.call_latertimer (clock injectable), and reports colour temperature plus the interpolated brightness factor through anon_apply_temperaturecallback. It notifiesActiveTransitionCounton 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.driver.accessory_state()/async_persist()and restored on start;restore()drops expired or unreadable schedules, andstop()preserves them for the next accessory.update_now()anddiscard_applied()let a consumer re-evaluate after external state changes and force a rewrite of an unchanged value.restore()andasync_persist()must be called there.accessories/adaptive_lightbulb.pyexample is rewritten on top of the controller.Tests
62 tests in
tests/test_adaptive_lighting.pycovering writes, interpolation timing, persistence round-trips, restore after restart, malformed payloads and the public API. The full suite andruff check/ruff formatpass 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.