Adaptive Lighting control engine
The Adaptive Lighting control engine turns a saved lighting profile and the current time into safe updates for managed Home Assistant lights. It runs on a schedule, reacts to light and control events, respects manual ownership, and sends only the color or brightness changes that each light can use.
Control scope
Lighting profile
Each main Adaptive Lighting switch represents one lighting profile for a set of lights. The profile supplies the time interval, brightness and color limits, sleep values, transition times, and rules for handling external control. custom_components/…/switch.py loads these values and creates the time-aware SunLightSettings used for each update.
The profile does not directly describe one fixed light state. It describes how the target should change through the day. The Light adaptation calculations page explains how the profile becomes a brightness and color target.
References: custom_components/…/switch.py• L840-985 custom_components/…/const.py• L70-273
Managed lights
The engine keeps the lights from all active profiles in one shared runtime manager. A configured Home Assistant light group can be expanded to its member lights for adaptation, so the manager can track state, manual control, transitions, and running updates per light. Explicit member targets remain individual targets even when group expansion is enabled.
The same light can belong to more than one profile. Service calls that target several lights are therefore resolved against the profiles that own those lights. This shared view lets the engine split work when different lights need different profile rules.
References: custom_components/…/switch.py• L236-289 custom_components/…/switch.py• L638-661 custom_components/…/switch.py• L1098-1119 custom_components/…/switch.py• L1997-2059
Integration lifecycle
Configuration entry lifecycle
The integration turns validated configuration into active switch entities and shared engine state. It registers the public apply, set_manual_control, and change_switch_settings services, imports YAML profiles through Home Assistant config entries, and forwards each entry to the switch platform.
Option changes reload the related configuration entry so the profile and its managed lights use the new settings. When entries are unloaded, the integration removes their listeners and disables the shared manager only after the last entry is gone. This keeps one profile from stopping adaptation for other active profiles.
References: custom_components/…/__init__.py• L46-49 custom_components/…/__init__.py• L61-100 custom_components/…/__init__.py• L103-140
Adaptation lifecycle
Target refresh
The main switch starts its interval listener while it is on. At each interval, the engine evaluates the current time and sleep state, updates the switch attributes, and asks each eligible light to move toward the new target. The periodic listener includes split-command and processing time, and a stable per-profile offset spreads routine updates across profiles. Turning the main switch on forces an initial update; turning it off removes the listeners and stops future adaptations.
The engine also starts an update when a managed light changes from off to on, when sleep mode changes, or when a user invokes the adaptive_lighting.apply service. The initial and sleep transitions can use different durations from the normal interval transition.
This diagram shows how a profile moves from a time event to a Home Assistant light update.
flowchart TD
A[Profile enabled or control event] --> B[Evaluate current time and sleep state]
B --> C[Build target brightness and color]
C --> D[Check light capability and control ownership]
D --> E[Build compatible light.turn_on data]
E --> F{More than one command needed?}
F -->|No| G[Send update]
F -->|Yes| H[Send cancellable command sequence]
H --> G
G --> I[Track state and transition]
References: custom_components/…/switch.py• L1122-1190 custom_components/…/switch.py• L1297-1306 custom_components/…/switch.py• L1535-1641
Light eligibility
Before a target becomes a command, the engine decides which attributes it may change for each light. The color and brightness control switches provide explicit limits. Manual-control records provide another limit. In pause_all mode any manual change pauses both attributes; in pause_changed mode only the changed attributes pause.
The engine normally adapts lights that are on. It skips a light that is still completing a prior transition, and it can skip a light that was just turned off during an interval update. A forced action, such as an explicit apply or a sleep-mode change, bypasses these normal interval filters while still using the light's supported features.
References: custom_components/…/switch.py• L1562-1605 custom_components/…/switch.py• L1607-1636 custom_components/…/switch.py• L2546-2589 custom_components/…/const.py• L209-224
Command application
The engine maps the calculated target to the light's supported control forms. It sends brightness as a Home Assistant brightness value, uses color temperature when supported unless RGB is preferred, and falls back to RGB when the light provides a usable color mode. It includes a transition only when the light supports one.
Some lights need brightness and color in separate light.turn_on calls. The command layer can split one target into a brightness call followed by a color call, divide the transition between them, and wait between calls when configured. For a multi-light intercepted call, the first shared update is applied to the original call and each member receives only its remaining work. The command layer filters redundant values close to send time and cancels an older sequence when a newer update starts for the same light. The Light state and command handling page explains the capability checks, state checks, and service call path in more detail.
References: custom_components/…/switch.py• L1308-1412 custom_components/…/switch.py• L1414-1527 custom_components/…/switch.py• L2252-2333 custom_components/…/adaptation_utils.py• L95-176 custom_components/…/adaptation_utils.py• L268-331
External control
Service interception
When interception is enabled, the shared manager preprocesses light.turn_on and light.toggle calls before Home Assistant runs them. For an eligible light that is turning on, the engine replaces the requested color and brightness with the current Adaptive Lighting target, then lets the normal Home Assistant service handle the command.
Calls can target lights in different profiles or lights that Adaptive Lighting does not manage. The engine groups eligible lights by profile, adapts one group through the intercepted call, sends direct adaptation calls for other eligible groups, and preserves the original service data for skipped lights. Light groups are left for Home Assistant to expand before member lights are handled.
If Home Assistant does not expose the service registry needed for interception, the engine keeps its event listeners and uses event-reactive adaptation instead. Context markers identify the engine's own calls so they do not trigger another adaptation cycle.
References: custom_components/…/switch.py• L1852-1942 custom_components/…/switch.py• L1997-2059 custom_components/…/switch.py• L2096-2249 custom_components/…/hass_utils.py• L34-108
Manual ownership
The engine treats a non-engine color or brightness change as a request for temporary manual ownership when take-over control is enabled. It records the affected attributes and removes them from later targets until the light is reset, turned off and on, or released through the manual-control service.
The engine can also detect changes made outside Home Assistant by comparing refreshed light state with the last target it sent. An optional timer can release manual ownership after a configured period. When ownership is released, an active profile forces a new target for the light.
References: custom_components/…/switch.py• L1643-1723 custom_components/…/switch.py• L2409-2550 custom_components/…/switch.py• L2658-2720 custom_components/…/const.py• L209-224 custom_components/…/const.py• L238-263
Runtime controls
Adaptation switches
Each profile exposes separate controls for the main engine, sleep mode, color adaptation, and brightness adaptation. The main switch controls whether the profile runs. The color and brightness switches narrow the attributes that the engine may send without changing the time-based target itself.
The adaptive_lighting.apply service requests an immediate update for selected lights or for all lights in a selected profile. It can adapt color, brightness, or both, and it can turn on lights that are off before applying the target. The set_manual_control service changes which attributes the engine must leave under user control.
References: custom_components/…/switch.py• L416-485 custom_components/…/switch.py• L1267-1295 custom_components/…/switch.py• L1771-1845 custom_components/…/const.py• L302-324
Sleep mode
Sleep mode changes the target from the normal solar schedule to the configured sleep brightness and sleep color. The color can be a warm color temperature or a fixed RGB value. A sleep-mode change resets manual ownership for the profile's lights when configured to do so, then applies the new target with the sleep transition.
When the profile is configured to adapt until sleep, the sleep values become the night-time end of the gradual change instead of an immediate replacement after sunset. The calculation rules for this behavior belong to Light adaptation calculations.
References: custom_components/…/switch.py• L1725-1746 custom_components/…/switch.py• L1017-1041 custom_components/…/const.py• L229-235 custom_components/…/const.py• L257-263
Change checklist
When these files or sections are changed, remember to consider these:
- Keep profile validation,
SunLightSettingsconstruction, and command payload selection aligned. A setting accepted at setup must still produce a valid target and a valid light command. - Keep the interval, stable stagger, initial, sleep, and split-command timing rules aligned. A change in one timing path can cause overlapping updates or a target calculated for the wrong point in time.
- Keep manual-control flags aligned with the color and brightness switches. A mismatch can resume an attribute that the user still owns or pause an attribute that should adapt.
- Keep context tracking aligned with service interception and state-event handling. A missing marker can cause the engine to adapt its own command again.
- Keep shared-manager teardown aligned with all event listeners, interceptors, timers, and adaptation tasks. The manager must remain active for other profiles and stop only after the last profile is removed.
- Keep config-entry reload and unload paths aligned with service registration and platform setup. A stale entry listener or manager reference can leave an unloaded profile active.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The configured sunrise or sunset events have an invalid order | The engine cannot derive a safe time-based target | Solar event validation rejects the invalid schedule |
| A light turns off during a delayed or split adaptation | A pending update can target an off light or continue after the light no longer needs it | The command loop checks state and cancels or skips superseded work |
| Home Assistant cannot expose the service registry for interception | Turn-on calls do not receive instant target replacement | The manager falls back to service and state event handling |
| A multi-light call spans profiles with incompatible interception settings | The engine skips preprocessing for that call and preserves the original request | Multi-light interception requires the involved profiles to allow it |
| A light is marked as manually controlled for all eligible attributes | Scheduled updates stop for that light | Reset manual control, turn the light off and on, or wait for the configured auto-reset |
| Home Assistant reports stale light state while redundant filtering is enabled | A needed target value can be removed from the outgoing command | Disable redundant-command filtering when recorded state is not reliable |
| An active config entry is unloaded without the last-entry cleanup completing | Shared listeners or manager tasks can remain after the profile is gone | Entry unload removes the entry listener and disables the manager only when no entries remain |
Key files
| File | Why to read it |
|---|---|
| custom_components/…/__init__.py | Connects configuration entries to Home Assistant services, switch setup, option reloads, and shared-manager teardown. |
| custom_components/…/switch.py | Connects profile controls, time refreshes, light eligibility, service interception, and command execution. |
| custom_components/…/color_and_brightness.py | Defines the time-aware brightness, color, and sleep targets consumed by the engine. |
| custom_components/…/adaptation_utils.py | Defines the target payload sequence, redundant-value filtering, split commands, and adaptation timing. |
| custom_components/…/hass_utils.py | Defines the boundary used to wrap and restore Home Assistant light services. |
| custom_components/…/const.py | Defines the profile options and service contracts that shape engine behavior. |
| tests/test_switch.py | Shows the expected end-to-end behavior for interval updates, light events, manual control, and commands. |
| Light adaptation calculations | Child page for the solar model and target value calculations. |
| Light state and command handling | Child page for light capability checks, state changes, manual control, and Home Assistant command handling. |