mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-14 15:54:04 +02:00
Adds one `number.adaptive_lighting_intensity_<name>` entity per
configuration, alongside the Sleep Mode / Adapt Brightness / Adapt Color
switches on the same device. It is a 0-100% slider that interpolates the
adaptive result towards the switch's floor:
output = floor_value + (adaptive_value - floor_value) * intensity / 100
100 short-circuits and returns the adaptive value untouched, so it is the
default and existing configurations behave exactly as before. 0 returns
the floor. In between the light stays fully adaptive -- the sun still
moves it at every setting, so it is a scaled curve rather than a frozen
level, which is what distinguishes this from simply scaling the
brightness band.
The floor is set by a new per-switch `intensity_floor` option: the
switch's sleep settings (`sleep_brightness`/`sleep_color_temp`) by
default, or its `min_brightness`/`min_color_temp`.
`transition_until_sleep` forces the sleep floor whatever `intensity_floor`
says. With that option on, the adaptive color temperature after sunset
descends below `min_color_temp` towards `sleep_color_temp`, so a
`min_color_temp` floor would sit above the adaptive value and turning the
dial down would make the light cooler.
Documented in the README's features and a new Intensity section, and on
the docs site as advanced/intensity.md.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
144 lines
4.5 KiB
Python
144 lines
4.5 KiB
Python
"""Adaptive Lighting integration in Home-Assistant."""
|
|
|
|
import logging
|
|
from functools import partial
|
|
from typing import Any
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
import voluptuous as vol
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
from homeassistant.const import CONF_SOURCE, Platform
|
|
from homeassistant.core import Event, HomeAssistant
|
|
from homeassistant.helpers import service
|
|
|
|
from .const import (
|
|
_DOMAIN_SCHEMA, # pyright: ignore[reportPrivateUsage]
|
|
ATTR_ADAPTIVE_LIGHTING_MANAGER,
|
|
CONF_NAME,
|
|
DOMAIN,
|
|
SERVICE_APPLY,
|
|
SERVICE_CHANGE_SWITCH_SETTINGS,
|
|
SERVICE_SET_MANUAL_CONTROL,
|
|
SET_MANUAL_CONTROL_SCHEMA,
|
|
UNDO_UPDATE_LISTENER,
|
|
apply_service_schema,
|
|
change_switch_settings_schema,
|
|
)
|
|
from .switch import (
|
|
handle_apply_service,
|
|
handle_change_switch_settings,
|
|
handle_set_manual_control_service,
|
|
)
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
PLATFORMS = ["switch", "number"] # "number" is the intensity dial
|
|
|
|
|
|
def _all_unique_names(value: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
"""Validate that all entities have a unique profile name."""
|
|
hosts = [device[CONF_NAME] for device in value]
|
|
schema = vol.Schema(vol.Unique())
|
|
schema(hosts)
|
|
return value
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
{DOMAIN: vol.All(cv.ensure_list, [_DOMAIN_SCHEMA], _all_unique_names)},
|
|
extra=vol.ALLOW_EXTRA,
|
|
)
|
|
|
|
|
|
async def reload_configuration_yaml(event: Event) -> None:
|
|
"""Reload configuration.yaml."""
|
|
hass: HomeAssistant | None = event.data.get("hass")
|
|
if hass is not None:
|
|
await hass.services.async_call("homeassistant", "check_config", {})
|
|
else:
|
|
_LOGGER.error("HomeAssistant instance not found in event data.")
|
|
|
|
|
|
async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
|
|
"""Import integration from config."""
|
|
hass.services.async_register(
|
|
domain=DOMAIN,
|
|
service=SERVICE_APPLY,
|
|
service_func=partial(handle_apply_service, hass),
|
|
schema=apply_service_schema(),
|
|
)
|
|
|
|
hass.services.async_register(
|
|
domain=DOMAIN,
|
|
service=SERVICE_SET_MANUAL_CONTROL,
|
|
service_func=partial(handle_set_manual_control_service, hass),
|
|
schema=SET_MANUAL_CONTROL_SCHEMA,
|
|
)
|
|
|
|
if register_platform_service := getattr(
|
|
service,
|
|
"async_register_platform_entity_service",
|
|
None,
|
|
):
|
|
register_platform_service(
|
|
hass,
|
|
DOMAIN,
|
|
SERVICE_CHANGE_SWITCH_SETTINGS,
|
|
entity_domain=Platform.SWITCH,
|
|
func=handle_change_switch_settings,
|
|
schema=change_switch_settings_schema(),
|
|
)
|
|
|
|
if DOMAIN in config:
|
|
for entry in config[DOMAIN]:
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN,
|
|
context={CONF_SOURCE: SOURCE_IMPORT},
|
|
data=entry,
|
|
),
|
|
)
|
|
return True
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
|
"""Set up the component."""
|
|
data = hass.data.setdefault(DOMAIN, {})
|
|
|
|
# This will reload any changes the user made to any YAML configurations.
|
|
# Called during 'quick reload' or hass.reload_config_entry
|
|
hass.bus.async_listen("hass.config.entry_updated", reload_configuration_yaml)
|
|
|
|
undo_listener = config_entry.add_update_listener(async_update_options)
|
|
data[config_entry.entry_id] = {UNDO_UPDATE_LISTENER: undo_listener}
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
|
|
|
return True
|
|
|
|
|
|
async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
|
"""Update options."""
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
|
"""Unload a config entry."""
|
|
# Unload every platform: leaving the number entity loaded here would
|
|
# strand it when the entry is removed or reloaded.
|
|
unload_ok = await hass.config_entries.async_unload_platforms(
|
|
config_entry,
|
|
PLATFORMS,
|
|
)
|
|
data = hass.data[DOMAIN]
|
|
data[config_entry.entry_id][UNDO_UPDATE_LISTENER]()
|
|
if unload_ok:
|
|
data.pop(config_entry.entry_id)
|
|
|
|
if len(data) == 1 and ATTR_ADAPTIVE_LIGHTING_MANAGER in data:
|
|
# no more config_entries
|
|
manager = data.pop(ATTR_ADAPTIVE_LIGHTING_MANAGER)
|
|
manager.disable()
|
|
|
|
if not data:
|
|
hass.data.pop(DOMAIN)
|
|
|
|
return unload_ok
|