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 / adaptive-lighting-integration-lifecycle

Adaptive Lighting integration lifecycle

Adaptive Lighting is a Home Assistant integration that turns each configured lighting profile into a group of switch entities and coordinates their shared reaction to light service calls and state changes. Its lifecycle starts with YAML or config-flow input, builds one shared runtime manager, and ends by removing entry-specific entities and shared listeners when the last entry unloads.

Integration initialization

The integration accepts configuration from Home Assistant YAML and from config entries. YAML profiles are sent through the config-entry import flow so both setup paths become managed entries; each entry then receives its own runtime state under the adaptive_lighting domain. The top-level setup registers services for applying settings and marking lights as manually controlled, and it registers the switch entity service for profile setting changes when Home Assistant supports that service. Entry setup installs the configuration-update hook, keeps the hook cleanup callback with the entry, and forwards setup to the switch platform. This makes the integration-level lifecycle responsible for shared coordination while the switch platform owns the profile entities.

See the child pages Integration setup and coordination for the entry setup sequence and its coordination rules, and Integration diagnostics for diagnostic support.

flowchart TD
    A[Home Assistant configuration] --> B{YAML or config flow}
    B --> C[Config entry]
    C --> D[Integration entry setup]
    D --> E[Entry state in hass.data]
    D --> F[Switch platform setup]
    F --> G[Profile switch entities]
    F --> H[Shared runtime manager]

References: custom_components/…/__init__.py• L61-115 custom_components/…/config_flow.py• L29-99 custom_components/…/manifest.json• L2-11

Runtime registration

The integration setup registers the shared runtime services, while the switch platform registers the runtime behavior for an entry. The platform creates the main Adaptive Lighting switch plus controls for sleep mode, color adaptation, and brightness adaptation. The shared services apply settings or mark lights as manually controlled, and the switch entity service updates one profile.

The first active entry creates the shared AdaptiveLightingManager. The manager listens for Home Assistant service and state events for the lights that belong to all profiles. It also tries to intercept light.turn_on and light.toggle calls before Home Assistant runs them. If interception cannot be installed, the manager keeps event-based operation instead of blocking light service calls.

sequenceDiagram
    participant HA as Home Assistant
    participant Entry as Entry setup
    participant Platform as Switch platform
    participant Manager as Shared manager
    participant Light as Light services

    HA->>Entry: Set up config entry
    Entry->>Platform: Forward switch setup
    Platform->>Manager: Create once for active entries
    Platform->>Platform: Create profile and control switches
    Platform->>HA: Register entities and services
    Manager->>Light: Register call interceptors when available
    Light-->>Manager: Service and state events

References: custom_components/…/__init__.py• L63-89 custom_components/…/switch.py• L376-444 custom_components/…/switch.py• L446-551 custom_components/…/switch.py• L1703-1785 custom_components/…/hass_utils.py• L34-108

Integration-level state

Integration state lives in hass.data[DOMAIN] and has two scopes. Each config-entry key holds that entry's cleanup callback and its switch entities. The shared manager sits beside those entry records, so multiple profiles can observe and coordinate the same light service and state activity without creating one manager per profile.

The manager tracks the light set and the short-lived state needed to relate service calls, state changes, manual control, transitions, and active adaptation work. A profile adds its expanded light entities to this shared set when its listeners start. The profile switch state controls whether that profile keeps its own listeners and interval activity active.

References: custom_components/…/__init__.py• L103-115 custom_components/…/const.py• L275-279 custom_components/…/switch.py• L1036-1067 custom_components/…/switch.py• L1703-1758

Active profile lifecycle

Each main profile switch restores its prior Home Assistant state when it is added. An active profile starts its listeners, expands configured light groups, and schedules periodic adaptation. Turning the profile off removes those listeners and resets the manager's tracking for its lights. The child controls represent sleep mode and the separate adaptation choices while remaining part of the same profile device.

When a profile is active, its interval callback refreshes the light settings and may adapt the lights. Turning the profile on also performs an initial update unless the profile is configured to adapt only when lights turn on. This connects entity state to the manager's event coordination without making each profile own a separate event system.

References: custom_components/…/switch.py• L840-893 custom_components/…/switch.py• L1019-1067 custom_components/…/switch.py• L1089-1093 custom_components/…/switch.py• L1158-1197

Entry unload

Unloading an entry first unloads its switch platform and removes its options-update listener. The entry record is deleted only after the platform reports success. When no entry records remain, the integration disables and removes the shared manager, then removes the domain record from Home Assistant. This prevents shared event listeners from surviving the final profile.

The unload path also handles the YAML marker used during import. A YAML entry that no longer matches the current YAML configuration is removed during switch setup rather than becoming an active runtime profile.

References: custom_components/…/__init__.py• L123-142 custom_components/…/config_flow.py• L84-99 custom_components/…/switch.py• L390-400 custom_components/…/switch.py• L1786-1790

Change checklist

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

  • Keep the keys written by integration setup aligned with the lookups made by switch setup and service routing. A missing entry record prevents platform setup or light-to-profile resolution.
  • Keep manager creation and final cleanup symmetric. Shared event listeners and service interceptors must stop when the last config entry unloads.
  • Keep service registration tied to the active integration and switch platform contracts. A change to a service schema or handler must preserve the profile-selection rules used by service calls.
  • Keep YAML import markers consistent with entry setup. Otherwise removed YAML profiles can remain as active config entries.
  • Keep the intercept and multi_light_intercept relationship valid. Multi-light interception depends on the base light-call interception path.

Important failure modes

TriggerConsequenceGuard
A YAML-imported entry no longer has a matching YAML markerThe switch setup removes the config entry instead of creating entitiesKeep YAML import tracking aligned with the current YAML configuration
The light service registry cannot expose the service handler or the target service is not registeredService interception is skipped and the integration uses event-reactive behaviorThe manager catches setup failure and continues without blocking light calls
A profile enables multi_light_intercept without interceptThe profile cannot use multi-light interception as requestedSetup disables multi-light interception and continues with the base setting
A profile enables non-Home Assistant change detection or bare-turn-on-only behavior without takeover controlThe related control behavior would have no valid takeover modeSetup enables takeover control for the profile
The last config entry unloads but shared manager cleanup does not runEvent listeners and service interceptors can remain active after the integration is goneFinal entry unload calls manager disablement and removes the domain state

Key files

FileWhy to read it
custom_components/…/__init__.pyShows the integration entry point, service registration, config import, entry state, platform forwarding, update reload, and final unload lifecycle.
custom_components/…/config_flow.pyShows how user-created, duplicated, options, and YAML-imported config entries enter the lifecycle.
custom_components/…/switch.pyShows the profile entities, runtime service handlers, listener activation, shared manager, and profile unload behavior.
custom_components/…/hass_utils.pyShows the boundary used to add and remove service-call interception around Home Assistant light services.
custom_components/…/const.pyDefines the domain and the shared state keys that connect integration setup with platform setup.
custom_components/…/manifest.jsonStates the Home Assistant integration contract, including config-flow support and the light dependency.