Introducing a new way to review your PRs.

Read the announcement

basnijholt / adaptive-lighting

Adaptive Lighting custom component for Home Assistant

View on GitHub

13 documents · Updated Oct 21, 2026

docs / light-state-and-command-handling

Light state and command handling

Light state and command handling is the boundary between Adaptive Lighting targets and Home Assistant light entities. It reads each light's reported capabilities and state, limits adaptation to safe and allowed attributes, and sends light.turn_on commands in a form the light can use.

Capability inspection

Supported control forms

The integration derives a small capability set from the light state. A light can support brightness, color temperature, color, and transition control in different combinations. Color modes such as RGB, RGBW, RGBWW, XY, or HS also count as brightness-capable because Home Assistant exposes brightness with those modes.

The capability set controls command construction rather than the target calculation. A target can contain several color representations, but the command path selects only the representation that the light reports as usable.

References: custom_components/…/switch.py• L628-667 custom_components/…/color_and_brightness.py• L341-389

Color command selection

Color temperature is the normal color form when the light supports it. The integration uses RGB instead when RGB is preferred, when the current target requires RGB, or when the light has a color mode but no color-temperature capability. Before it sends a color-temperature value, it limits the value to the light's reported Kelvin range.

This selection lets one profile serve lights with different color controls. The Light adaptation calculations page explains how the target representations are produced.

References: custom_components/…/switch.py• L1235-1276 custom_components/…/const.py• L103-107 custom_components/…/color_and_brightness.py• L341-389

State inspection

On and off state

The engine normally adapts only lights that are on. It checks the state before it starts work and checks again before each delayed command. This prevents a scheduled or split update from changing a light after it was turned off.

Forced actions, such as adaptive_lighting.apply or a sleep-mode change, bypass the normal interval filters. They still use the light's capability set, and an apply call turns an off light on only when turn_on_lights is enabled.

References: custom_components/…/switch.py• L447-476 custom_components/…/switch.py• L1414-1519 custom_components/…/switch.py• L1331-1361

Reported attribute changes

When non-Home Assistant changes are enabled, the manager refreshes the light and compares its reported attributes with the last adaptation command. It treats a large brightness change, a large color-temperature change, a large RGB change, or a change of color mode as a meaningful external change. The affected attributes then become manually controlled.

Color comparisons use RGB when possible. If one state reports color temperature or XY color and the other reports no RGB value, the integration converts the values before comparison. This avoids treating equivalent color forms as unrelated state changes.

References: custom_components/…/switch.py• L670-837 custom_components/…/switch.py• L2639-2720 custom_components/…/const.py• L37-48

Adaptation eligibility

Attribute ownership

For each light, the manager combines three decisions: whether brightness adaptation is enabled, whether color adaptation is enabled, and whether another source owns either attribute. The result is a pair of allowed attributes. If no attribute remains allowed, the engine does not create an adaptation command.

Take-over control records external brightness or color changes as manual ownership. In pause_all mode, ownership of one attribute pauses both. In pause_changed mode, only the changed attribute pauses. Manual ownership can be cleared by a reset, by the manual-control service, by an off-to-on cycle, or by an optional timer.

References: custom_components/…/switch.py• L2277-2321 custom_components/…/switch.py• L2217-2275 custom_components/…/const.py• L199-214

Turn-on decisions

An off-to-on state change starts a reactive adaptation for a managed light when its profile is on. The engine links the state change to the recorded light.turn_on call through the Home Assistant context. This distinction prevents a state report from being mistaken for a user turn-on.

When adapt_only_on_bare_turn_on is enabled, a turn-on call that already contains brightness or color marks those attributes as manually controlled. This preserves scene values while the engine can continue with attributes that remain free. An optional adapt_delay gives the light time to settle before the new target is applied.

References: custom_components/…/switch.py• L1521-1579 custom_components/…/switch.py• L2722-2747 custom_components/…/switch.py• L2940-2962 custom_components/…/const.py• L91-100 custom_components/…/const.py• L228-232

Recent turn-off handling

Home Assistant can report a light as on for a short time while a turn-off transition is still in progress. The manager records turn-off and state-change events, waits when needed, and cancels adaptation if the light becomes off. A rapid off-to-on-to-off sequence therefore does not cause the engine to reapply a target to a light that is still shutting down.

References: custom_components/…/switch.py• L2387-2475 custom_components/…/switch.py• L2551-2597 custom_components/…/switch.py• L2787-2938

Command construction

Service payload

The command path combines the selected target with the capability set and builds service data for one light. Brightness percentages become Home Assistant brightness values on the 0–255 scale. A transition is included only when the light supports transitions and the requested duration is greater than zero.

The payload contains brightness, color temperature, or RGB only when that attribute is both allowed and supported. If no relevant attribute remains, the engine stops before it calls Home Assistant.

References: custom_components/…/switch.py• L1199-1301 custom_components/…/const.py• L281-284

State filtering

When redundant-command filtering is enabled, the engine removes target values that equal the light's current Home Assistant attributes. The filter runs when each command is about to be sent, not only when the target is first built. A state change during a delay can therefore change whether a later command is needed.

This saves network traffic but relies on Home Assistant state matching the physical light. The skip_redundant_commands setting should be disabled when the light does not report state reliably.

References: custom_components/…/adaptation_utils.py• L116-173 custom_components/…/adaptation_utils.py• L233-276 custom_components/…/const.py• L246-255

Split command sequence

Some lights do not handle brightness and color in one service call. When separate commands are enabled, the integration sends brightness first and color second. It divides the transition time between the calls and can wait for a configured delay between them.

Each sequence is cancellable. A newer target cancels an older sequence for the same light, and the state check before each call stops work when the light is no longer on. The manager records the last sent service data so later state checks can identify changes made outside Home Assistant.

References: custom_components/…/adaptation_utils.py• L86-113 custom_components/…/adaptation_utils.py• L144-191 custom_components/…/switch.py• L1312-1405 custom_components/…/const.py• L109-115 custom_components/…/const.py• L234-237

Service command handling

Adaptive service calls

The adaptive_lighting.apply service selects a profile and a set of lights, then requests a forced adaptation for each selected light. It can request brightness, color, or both. It normally leaves off lights alone, unless turn_on_lights is true.

The resulting calls use the same capability checks, state filtering, and cancellable command path as scheduled adaptation. This keeps an explicit apply action compatible with the light instead of treating it as a separate command system.

References: custom_components/…/switch.py• L446-476 custom_components/…/const.py• L289-315 docs/services.md• L9-54

Turn-on interception

When interception is enabled, the shared manager preprocesses light.turn_on and light.toggle calls before Home Assistant runs them. The service boundary makes a writable copy of the call data, lets the manager add adapted values, restores the data as read-only, and then runs Home Assistant's original service handler. If preprocessing fails, the error is logged and the original handler still runs so the light service remains available.

The interceptor does not reprocess the integration's own commands. Context markers also identify proactive adaptations and prevent their resulting state events from starting a second reactive adaptation. If Home Assistant does not expose the service registry or the target service is not registered, the manager keeps event-based handling as a fallback. The wrapper can also be removed by restoring the original service handler when the manager is disabled.

References: custom_components/…/hass_utils.py• L20-30 custom_components/…/hass_utils.py• L33-107 custom_components/…/switch.py• L1747-1785 custom_components/…/switch.py• L1928-1970 custom_components/…/switch.py• L2078-2136 custom_components/…/const.py• L257-272

Multi-light requests

For a request that targets several entities, the manager resolves direct and indirect entity references without expanding groups, then separates lights by the profile that controls them. It adapts eligible lights through one intercepted group, sends direct adaptation calls for other eligible groups, and preserves the original service data for skipped lights.

Light groups are not adapted as a single entity by the interceptor. Home Assistant expands them and the member-light calls are handled afterward. If a multi-light request spans profiles that do not all allow multi-light interception, the manager skips interception for the full request to preserve a predictable original call.

References: custom_components/…/hass_utils.py• L20-30 custom_components/…/switch.py• L1833-1926 custom_components/…/switch.py• L1979-2075 custom_components/…/switch.py• L600-625

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep Home Assistant color-mode names and feature flags aligned with the capability set. A mismatch can select a service field that the light does not support.
  • Keep the target output names aligned with payload selection and state comparison. A new color form must have a clear command path and a comparison path.
  • Keep manual-control flags aligned with service-call attribute detection and the pause_all or pause_changed rules. Otherwise external changes can be overwritten.
  • Keep context markers aligned across interception, state changes, and proactive adaptation. A missing marker can create a feedback loop.
  • Keep the service interceptor's writable-copy and original-handler contract aligned with Home Assistant's service registry. A wrapper must not leave a service replaced after the manager is disabled.
  • Keep split-command timing aligned with transition tracking and cancellation. A later command must not outlive a newer target or a turn-off action.
  • Keep multi-light target resolution and interception rules aligned with profile ownership and group expansion. A partial rewrite can duplicate commands or change unmanaged lights.

Important failure modes

TriggerConsequenceGuard
A light reports a color mode that the capability mapping does not recognizeAdaptive color or brightness control can be omitted or sent in an unusable formDerive the supported control set from Home Assistant's current feature and color-mode fields
Home Assistant reports stale state while redundant filtering is enabledA needed brightness or color value can be removed from the commandDisable skip_redundant_commands when state reports do not track the physical light
A light turns off during a transition or split sequenceA pending command can change a light that no longer needs adaptationCheck state before each command and cancel older sequences
A scene turns on a light with brightness or color while bare-turn-on mode is enabledAdaptive Lighting can overwrite the scene valueRecord the supplied attributes as manual control
An interceptor changes service data but does not preserve the original service contractHome Assistant can reject the call or the light service can stop working after interceptionPass modified data to the original handler, keep the service schema, and restore the handler on removal
A multi-light call spans profiles with incompatible interception settingsThe call may be adapted inconsistently if only part of it is rewrittenSkip interception for the complete request unless all targeted profiles allow it
The service registry cannot be wrappedTurn-on calls do not receive proactive target valuesFall back to event-reactive adaptation

Key files

FileWhy to read it
custom_components/…/switch.pyConnects capability checks, state events, eligibility decisions, service interception, and command execution.
custom_components/…/adaptation_utils.pyDefines attribute detection, redundant filtering, split commands, and command timing.
custom_components/…/hass_utils.pyResolves service targets and defines the boundary used to wrap, process, and restore Home Assistant services.
custom_components/…/const.pyDefines the settings that change ownership, interception, filtering, and split-command behavior.
custom_components/…/color_and_brightness.pyDefines the target color and brightness forms consumed by command selection.
tests/test_adaptation_utils.pyShows the contracts for attribute detection, state filtering, split calls, and command timing.
tests/test_switch.pyShows the expected behavior for manual control, state changes, interception, groups, and applied light settings.
Adaptive Lighting control engineParent page for when the engine requests updates and how profiles coordinate them.
Light adaptation calculationsRelated page for the time-based targets that this command path sends.