Integration setup and coordination
Adaptive Lighting is a Home Assistant custom component that turns each saved configuration into a group of control entities and coordinates their work against the light domain. Its setup path accepts YAML or UI-created config entries, its platform path creates the entities and shared runtime, and its unload path removes those resources in the reverse order.
Integration boundary
Home Assistant entry points
The integration boundary is defined by custom_components/…/manifest.json and custom_components/…/__init__.py. Home Assistant loads the component after the light domain, then calls a general setup entry for YAML and a config-entry setup entry for each active configuration. The component exposes only the switch platform for its control entities.
The general setup step registers the integration services and does not create switches directly. It starts an import flow for each YAML configuration, which gives YAML entries the same config-entry lifecycle as entries created through the UI. The config-entry setup step stores per-entry coordination data, attaches an options update listener, and forwards setup to the switch platform.
References: custom_components/…/manifest.json• L1-11 custom_components/…/__init__.py• L22-36 custom_components/…/__init__.py• L61-115
Configuration entry points
custom_components/…/config_flow.py provides the UI path, the YAML import path, and the options path. A UI flow creates a named entry and prevents duplicate names. A YAML import records which names came from YAML so later platform setup can distinguish current YAML entries from entries that the user removed from YAML.
Options belong to an existing config entry. UI-created entries receive an editable options form, while YAML-imported entries direct the user back to YAML. A change to an entry causes the integration to reload that entry, so setup and teardown both take part in applying new settings.
References: custom_components/…/config_flow.py• L29-99 custom_components/…/config_flow.py• L101-177 custom_components/…/__init__.py• L62-80
Setup coordination
Setup sequence
The setup sequence connects a configuration source to active Home Assistant entities and then to shared light-domain tracking. The integration first registers its services, then accepts configuration, loads its platform, and creates the shared manager and the entities that use it.
This diagram shows the relationship between service registration, the YAML and UI paths, and the common platform setup path.
flowchart TD
A[Home Assistant loads integration] --> B[Register integration services]
B --> C{Configuration source}
C -->|YAML| D[Import config entry]
C -->|UI| E[Create config entry]
D --> F[Set up config entry]
E --> F
F --> G[Forward setup to switch platform]
G --> H[Reuse or create shared manager]
H --> I[Create main and auxiliary switches]
I --> J[Track configured lights]
References: custom_components/…/__init__.py• L61-115 custom_components/…/config_flow.py• L36-99 custom_components/…/switch.py• L376-551
Platform entities
The switch platform represents one Adaptive Lighting configuration as four related switches: a main adaptation switch plus sleep-mode, color-adaptation, and brightness-adaptation controls. The main switch owns the configuration-specific settings and refers to the three auxiliary switches, while all four share one Home Assistant device identity.
The integration registers its services during general setup. Service calls resolve their target configuration through the active config entries, then use that configuration's main switch and shared manager to apply settings or manual-control changes.
References: custom_components/…/switch.py• L376-444 custom_components/…/switch.py• L446-551 custom_components/…/switch.py• L840-1017 custom_components/…/switch.py• L1624-1675
Shared runtime coordination
Shared manager
The AdaptiveLightingManager is the runtime coordinator for all active configurations in one Home Assistant instance. The first switch-platform setup creates it and stores it under the integration domain; later entries reuse it. This lets configurations that share a light use common records for service calls, state changes, manual control, transitions, and running adaptation tasks.
The manager also keeps the set of lights known to Adaptive Lighting. Each main switch adds its configured lights during setup and expands ordinary Home Assistant light groups into member lights for runtime tracking. The manager therefore provides the shared view needed when a service call covers more than one configuration.
References: custom_components/…/switch.py• L600-618 custom_components/…/switch.py• L1024-1048 custom_components/…/switch.py• L1699-1778
Service interception
When enabled for a configuration, the manager places a preprocessing layer around Home Assistant light.turn_on and light.toggle. The layer checks which active Adaptive Lighting switch controls each target light, adapts eligible turn-on data before the original service runs, and sends separate calls for lights that need different configuration handling. Lights that are not eligible keep the original service data.
The manager keeps context records so its own service calls do not trigger a second adaptation. If Home Assistant does not expose the service registry needed for interception, setup keeps the event listeners and falls back to adapting from service and state events.
References: custom_components/…/hass_utils.py• L34-108 custom_components/…/switch.py• L1703-1785 custom_components/…/switch.py• L1928-2075
Event coordination
The shared manager listens for service-call and state-change events for tracked lights. Service events record turn-on, turn-off, and toggle intent. State events then connect that intent to the light's actual transition, which allows the integration to respond when a light changes from off to on and to stop work when it changes from on to off.
The main switch adds its own interval and auxiliary-switch listeners only while adaptation is on. On each update it asks the manager which attributes remain eligible for adaptation, taking account of the brightness and color controls and any manual control recorded by the manager. This separates shared light history from per-configuration adaptation settings.
References: custom_components/…/switch.py• L1040-1093 custom_components/…/switch.py• L1188-1197 custom_components/…/switch.py• L2277-2321 custom_components/…/switch.py• L2387-2475 custom_components/…/switch.py• L2477-2597
Teardown behavior
Entity teardown
The main switch starts its runtime listeners when Home Assistant adds it. If Home Assistant has not started yet, it waits for the start event. Turning the switch off removes its interval and state listeners and resets the tracked light state; removing the entity uses the same listener cleanup path. The auxiliary switches restore their state but do not own the light-event listeners.
An adaptation that is still running can be cancelled by the manager when a light turns off, when a new adaptation supersedes it, or when the main switch resets that light. This keeps entity removal and ordinary switch-off behavior from leaving active work behind.
References: custom_components/…/switch.py• L1019-1038 custom_components/…/switch.py• L1050-1121 custom_components/…/switch.py• L1158-1186 custom_components/…/switch.py• L2323-2359
Config entry unload
Config-entry unload first asks the switch platform to remove the entry's entities. It then removes the options update listener and deletes the entry's stored coordination data when platform unload succeeds. The shared manager remains available while another config entry is active.
When the last config entry is gone, the integration removes the manager and calls its disable operation. That operation removes the manager's service and event listeners, including any service interceptors. Only after the entry data and manager are cleared does the integration remove its domain-level hass.data record.
References: custom_components/…/__init__.py• L123-142 custom_components/…/switch.py• L1786-1790 custom_components/…/switch.py• L1112-1121
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the platform names aligned across
PLATFORMS,async_forward_entry_setups, andasync_forward_entry_unload. - Keep the service registration point in general setup aligned with the service handlers and schemas used by the switch platform.
- If the manager gains a listener, interceptor, or long-running task, give it a matching removal or cancellation path in
disableor entry teardown. - Keep YAML import tracking and the switch platform's stale-entry check aligned. A change to either side can remove a valid YAML entry or retain one that was deleted.
- Keep config-flow fields, YAML validation, and switch setting validation aligned. A field accepted by one setup path must still produce data that the switch can load.
- Preserve the shared-manager lifetime rule when changing entry data. Removing it per entry can break other active configurations, while retaining it after the last unload can keep event handlers active.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A YAML config entry is no longer marked as present in the current YAML import set | The switch platform removes that config entry during setup | Keep YAML import tracking in async_step_import aligned with the stale-entry check |
| Home Assistant does not allow access to the registered light services | Instant turn-on interception is unavailable; the integration uses event-reactive behavior | The manager catches interceptor setup failure and retains its event listeners |
| A multi-light call spans switches that do not all allow multi-light interception | Adaptive preprocessing is skipped for the call rather than split across incompatible configurations | Enable multi_light_intercept consistently with intercept for the involved switches |
| An active entry is unloaded while another entry still uses the manager | Shared event and service tracking must continue for the remaining entry | The integration disables the manager only after the last config entry unloads |
Key files
| File | Why to read it |
|---|---|
| custom_components/…/manifest.json | Defines the Home Assistant integration boundary and its dependency on the light domain. |
| custom_components/…/__init__.py | Shows service registration, YAML import, config-entry setup, platform forwarding, update reload, and final unload. |
| custom_components/…/config_flow.py | Explains UI creation, YAML import, duplicate names, and options updates. |
| custom_components/…/switch.py | Shows platform entity creation, per-entry services, the shared manager, event coordination, and cleanup. |
| custom_components/…/hass_utils.py | Shows how service interception wraps and later restores Home Assistant service handlers. |
Adaptive Lighting integration lifecycle | Parent page for the wider lifecycle view beyond setup and runtime coordination. |
Integration diagnostics | Related page for diagnosing setup and runtime coordination problems. |