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-adaptation-calculations

Light adaptation calculations

Light adaptation calculations turn the current solar position and sleep state into a target brightness and color for each light. The target is then expressed in the color formats that Home Assistant and the light support, so the control engine can move lights toward the target as time passes.

Calculation model

Solar position

The solar clock provides the shared time basis for all non-sleep targets. It uses Astral sunrise, sunset, solar noon, and solar midnight events, with optional fixed times, limits, offsets, and a configured time zone. The resulting sun_position ranges from -1 at solar midnight to 1 at solar noon. Sunrise and sunset are 0. At polar latitudes, where the sun may not cross the horizon, the calculator creates bounded synthetic sunrise and sunset events so the adaptation cycle can still run. SunEvents

The calculator finds the events around the requested time before it calculates the position. It also checks that the events keep a valid daily order. Invalid event settings can therefore stop reliable adaptation rather than produce an ambiguous target. SunEvents.sun_position

References: custom_components/…/color_and_brightness.py• L44-256 tests/test_color_and_brightness.py• L78-209

Target time

The requested transition changes which time the calculator evaluates. get_settings adds the transition length to the current UTC time, so the returned target represents the light state expected at the end of that transition. SunLightSettings.get_settings

This keeps a gradual light change aligned with the solar schedule. An adaptation that takes time does not aim at the value for the instant when the command starts. It aims at the value due when the command finishes.

References: custom_components/…/color_and_brightness.py• L461-471

Brightness calculation

Brightness range

Brightness uses a configured minimum and maximum percentage. In normal operation the solar position selects a value within that range. In sleep mode the calculator returns the configured sleep brightness instead. SunLightSettings.brightness_pct

The default mode reaches maximum brightness while the sun is above the horizon. During the dark part of the solar cycle it moves between the minimum and maximum according to the position of the sun. This makes sunset and sunrise the points where the normal night-to-day change meets the daytime maximum. SunLightSettings._brightness_pct_default

References: custom_components/…/color_and_brightness.py• L319-394 const.py• L73-83 const.py• L118-119

Brightness modes

The configured brightness mode controls the shape of the change around sunrise and sunset. default follows the solar-position model. linear uses straight interpolation over the configured dark and light periods. tanh uses the same periods with a smooth curve that approaches the range limits without changing them abruptly. SunLightSettings._brightness_pct_linear SunLightSettings._brightness_pct_tanh

The two period settings have different roles at each event:

  • brightness_mode_time_dark covers the shorter dark-side period before sunrise or after sunset.
  • brightness_mode_time_light covers the longer light-side period after sunrise or before sunset.

Both non-default modes clamp their result to the configured minimum and maximum. The tanh mode uses 5% and 95% of the range at its configured period boundaries, while the linear mode uses the actual range boundaries. The range handling remains valid when a user intentionally configures the endpoints in reverse order. SunLightSettings._brightness_pct_linear SunLightSettings._brightness_pct_tanh clamp

References: custom_components/…/color_and_brightness.py• L327-394 custom_components/…/color_and_brightness.py• L600-610 const.py• L177-197 tests/test_switch.py• L2637-2694

Color calculation

Color temperature

Color temperature uses the configured warm and cold Kelvin limits. During daylight it interpolates from the warm limit at sunrise to the cold limit at solar noon, then back toward the warm limit at sunset. The result is rounded to the nearest 5 Kelvin. SunLightSettings.color_temp_kelvin

The configuration names follow the physical effect rather than numeric order: min_color_temp is the warmest target and max_color_temp is the coldest target. At or below the horizon the normal target stays at the warm limit unless adaptation continues toward sleep settings. const.py

References: custom_components/…/color_and_brightness.py• L396-409 custom_components/…/const.py• L76-83

Sleep color

Sleep mode replaces the time-based color with the configured sleep color temperature or RGB color. The selected form is controlled by sleep_rgb_or_color_temp. SunLightSettings.brightness_and_color const.py

When adaptation continues until sleep, the sleep color becomes the post-sunset end of the color change. With RGB sleep color selected, the calculator blends from the warm color-temperature RGB value toward the sleep RGB color in HSV space. The blend uses the absolute solar position, so it reaches the sleep color at the darkest point. SunLightSettings.brightness_and_color lerp_color_hsv

References: custom_components/…/color_and_brightness.py• L411-459 custom_components/…/color_and_brightness.py• L569-592 custom_components/…/const.py• L121-137

Color representations

The calculator returns one target in several representations so downstream light control can use the form that fits the light. It converts Kelvin to RGB, then derives XY and HS values from RGB. It also includes the legacy mired value and a flag that tells command selection when RGB must be used. SunLightSettings.brightness_and_color

The control engine chooses color temperature when the light supports it, unless RGB is preferred or the current sleep target requires RGB. If the light has a color mode but no usable color-temperature path, the engine sends RGB instead. It clamps Kelvin targets to the light's own supported range before sending them. Switch.prepare_adaptation_data

References: custom_components/…/color_and_brightness.py• L411-459 custom_components/…/switch.py• L670-688 custom_components/…/switch.py• L1235-1276 custom_components/…/const.py• L103-107

Adaptation command data

Target payload

The control engine combines the calculated brightness and color with the light's supported features. 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. Switch.prepare_adaptation_data

The engine can adapt brightness, color, or both. Manual control state and the adaptation switches decide which of those attributes remain eligible. If no eligible target attribute exists, no adaptation call is created. See Light state and command handling for the wider command and manual-control flow.

References: custom_components/…/switch.py• L1199-1233 custom_components/…/switch.py• L1241-1287 custom_components/…/const.py• L281-284

Split and filtering

The adaptation utilities turn one target payload into the service-call sequence required by the light. When separate commands are enabled, brightness and color become separate calls in brightness-first order. The transition duration is divided between those calls, and an optional delay is added between them. _split_service_call_data prepare_adaptation_data

State filtering removes target attributes that already match the light's known state. Brightness uses a small numeric tolerance, while color temperature is compared in mired space so normal device quantization does not create endless repeat commands. The filter runs as each call is about to be used, so a state change during a delay can prevent a now-redundant call. This reduces needless commands but depends on Home Assistant state matching the physical light. _create_service_call_data_iterator _remove_redundant_attributes

When a shared turn-on has already applied brightness or color, preparation removes that attribute from the pending calls while keeping the original split timing. This prevents a second command from undoing the effect of the first command. prepare_adaptation_data

References: custom_components/…/adaptation_utils.py• L95-209 custom_components/…/adaptation_utils.py• L268-331 tests/test_adaptation_utils.py• L33-343

Adaptation timing

The control engine repeats the calculation at its adaptation interval and sends the target through a transition. Separate brightness and color calls can run as one cancellable sequence. A newer adaptation cancels the older sequence for the same light, which prevents old targets from continuing after the schedule or light state changes. Switch._async_update_at_interval_action Switch.execute_cancellable_adaptation_calls

The command loop checks the light state again before each call unless the adaptation is forced. Turning a light off can therefore stop a pending target, while a turn-on path can deliberately apply the target even when the normal state check would reject it. Switch._execute_adaptation_calls

References: custom_components/…/switch.py• L1188-1197 custom_components/…/switch.py• L1331-1405 custom_components/…/const.py• L70-71 custom_components/…/const.py• L216-237

The calculations sit between the schedule-driven control engine and the state-aware command path:

Change checklist

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

  • Keep the meaning of the warm and cold Kelvin limits aligned between configuration validation, target calculation, and per-light Kelvin clamping.
  • Keep the brightness percentage-to-Home-Assistant brightness conversion aligned with any change to the target range or brightness service contract.
  • Keep sleep RGB interpolation and the RGB preference rules aligned so the selected color form matches the light feature checks.
  • Keep transition duration distribution aligned between target-time evaluation and split service calls. A mismatch can make a target arrive at the wrong point in the solar schedule.
  • Keep the attribute flags aligned with split payloads, already-applied attributes, and manual-control decisions. A mismatch can leave one adaptation task active after its attribute is paused or applied.
  • Keep state-filter tolerances aligned with the precision used by Home Assistant and the light integration. A mismatch can cause repeated commands or skip a needed change.

Important failure modes

TriggerConsequenceGuard
Sunrise or sunset settings make the four solar events occur in an invalid orderThe solar position cannot provide a safe target and adaptation is rejectedSunEvents validates event order before returning the position
A light's configured Kelvin target is outside the light's supported rangeThe light may reject the command or apply a different colorThe command path clamps Kelvin to the light's reported limits
Home Assistant state does not match the physical light while redundant-command filtering is enabledA needed update can be skippedDisable redundant-command filtering when recorded state is not reliable
State precision differs from the target precision and the filtering tolerance is changed incorrectlyThe system can repeat commands or treat a needed change as already appliedBrightness uses a small tolerance and color temperature uses a one-mired comparison
A light is turned off during a delayed or split adaptationPending target calls are cancelled or skipped instead of changing the off lightThe command loop checks state before each call and tracks cancellable tasks
RGB sleep color is selected for a light without a color modeThe sleep color cannot be sent as RGBColor selection checks the light's supported features before building the payload

Key files

FileWhy to read it
custom_components/…/color_and_brightness.pyDefines the solar position model and all brightness, color-temperature, RGB, and output conversions.
custom_components/…/adaptation_utils.pyDefines how calculated targets become filtered and, when needed, split service-call data.
custom_components/…/switch.pyConnects calculated settings to per-light feature support, transitions, and cancellable adaptation.
custom_components/…/const.pyDefines the configuration terms and limits that shape the calculations.
tests/test_color_and_brightness.pyShows the expected solar event and solar position behavior across locations and time zones.
tests/test_adaptation_utils.pyShows the contracts for splitting, state filtering, timing, and controlled attributes.
tests/test_switch.pyShows the expected brightness modes and day, night, and sleep targets through the control path.