2020-10-17 16:54:15 +02:00
|
|
|
"""Adaptive Lighting integration in Home-Assistant."""
|
2024-02-05 14:13:13 -08:00
|
|
|
|
2020-09-19 11:35:28 +02:00
|
|
|
import logging
|
2026-09-06 05:04:18 -06:00
|
|
|
from functools import partial
|
2022-04-17 19:51:44 -07:00
|
|
|
from typing import Any
|
2020-09-23 13:11:50 +02:00
|
|
|
|
2023-07-23 14:24:22 -07:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
import voluptuous as vol
|
2020-09-20 18:38:01 +02:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2026-09-06 05:04:18 -06:00
|
|
|
from homeassistant.const import CONF_SOURCE, Platform
|
2025-11-27 21:01:05 +01:00
|
|
|
from homeassistant.core import Event, HomeAssistant
|
2026-09-06 05:04:18 -06:00
|
|
|
from homeassistant.helpers import service
|
2020-09-19 11:10:54 +02:00
|
|
|
|
2020-09-30 00:42:27 +02:00
|
|
|
from .const import (
|
2025-12-12 22:37:42 +01:00
|
|
|
_DOMAIN_SCHEMA, # pyright: ignore[reportPrivateUsage]
|
2023-07-22 17:57:49 -07:00
|
|
|
ATTR_ADAPTIVE_LIGHTING_MANAGER,
|
2020-09-30 00:42:27 +02:00
|
|
|
CONF_NAME,
|
|
|
|
|
DOMAIN,
|
2026-09-06 05:04:18 -06:00
|
|
|
SERVICE_APPLY,
|
|
|
|
|
SERVICE_CHANGE_SWITCH_SETTINGS,
|
|
|
|
|
SERVICE_SET_MANUAL_CONTROL,
|
|
|
|
|
SET_MANUAL_CONTROL_SCHEMA,
|
2020-09-30 00:42:27 +02:00
|
|
|
UNDO_UPDATE_LISTENER,
|
2026-09-06 05:04:18 -06:00
|
|
|
apply_service_schema,
|
|
|
|
|
change_switch_settings_schema,
|
|
|
|
|
)
|
|
|
|
|
from .switch import (
|
|
|
|
|
handle_apply_service,
|
|
|
|
|
handle_change_switch_settings,
|
|
|
|
|
handle_set_manual_control_service,
|
2020-09-30 00:42:27 +02:00
|
|
|
)
|
2020-09-19 11:10:54 +02:00
|
|
|
|
2020-09-20 18:38:01 +02:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2020-09-20 18:33:26 +02:00
|
|
|
|
|
|
|
|
PLATFORMS = ["switch"]
|
2020-09-19 11:10:54 +02:00
|
|
|
|
2020-09-23 13:11:50 +02:00
|
|
|
|
2025-11-27 21:01:05 +01:00
|
|
|
def _all_unique_names(value: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
2020-10-05 23:41:57 +02:00
|
|
|
"""Validate that all entities have a unique profile name."""
|
2020-09-23 18:20:25 +02:00
|
|
|
hosts = [device[CONF_NAME] for device in value]
|
|
|
|
|
schema = vol.Schema(vol.Unique())
|
|
|
|
|
schema(hosts)
|
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
2020-09-24 20:41:24 +02:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
2020-09-26 13:13:44 +02:00
|
|
|
{DOMAIN: vol.All(cv.ensure_list, [_DOMAIN_SCHEMA], _all_unique_names)},
|
2020-09-24 20:41:24 +02:00
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
|
)
|
2020-09-23 18:20:25 +02:00
|
|
|
|
|
|
|
|
|
2025-11-27 21:01:05 +01:00
|
|
|
async def reload_configuration_yaml(event: Event) -> None:
|
2023-04-26 21:10:52 -05:00
|
|
|
"""Reload configuration.yaml."""
|
2025-11-27 21:01:05 +01:00
|
|
|
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.")
|
2023-04-26 21:10:52 -05:00
|
|
|
|
|
|
|
|
|
2025-11-27 21:01:05 +01:00
|
|
|
async def async_setup(hass: HomeAssistant, config: dict[str, Any]) -> bool:
|
2020-09-19 11:10:54 +02:00
|
|
|
"""Import integration from config."""
|
2026-09-06 05:04:18 -06:00
|
|
|
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(),
|
|
|
|
|
)
|
|
|
|
|
|
2020-09-19 11:10:54 +02:00
|
|
|
if DOMAIN in config:
|
2020-09-20 18:33:26 +02:00
|
|
|
for entry in config[DOMAIN]:
|
|
|
|
|
hass.async_create_task(
|
|
|
|
|
hass.config_entries.flow.async_init(
|
2023-07-23 14:24:22 -07:00
|
|
|
DOMAIN,
|
|
|
|
|
context={CONF_SOURCE: SOURCE_IMPORT},
|
|
|
|
|
data=entry,
|
|
|
|
|
),
|
2020-09-19 11:10:54 +02:00
|
|
|
)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
2025-11-27 21:01:05 +01:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2020-09-19 11:10:54 +02:00
|
|
|
"""Set up the component."""
|
2020-09-28 23:39:55 +02:00
|
|
|
data = hass.data.setdefault(DOMAIN, {})
|
2020-09-19 11:10:54 +02:00
|
|
|
|
2023-04-26 21:10:52 -05:00
|
|
|
# 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)
|
|
|
|
|
|
2020-09-20 18:33:26 +02:00
|
|
|
undo_listener = config_entry.add_update_listener(async_update_options)
|
2020-09-28 23:39:55 +02:00
|
|
|
data[config_entry.entry_id] = {UNDO_UPDATE_LISTENER: undo_listener}
|
2025-01-01 14:19:31 -08:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2020-09-19 11:10:54 +02:00
|
|
|
|
|
|
|
|
return True
|
2020-09-20 18:33:26 +02:00
|
|
|
|
|
|
|
|
|
2025-11-27 21:01:05 +01:00
|
|
|
async def async_update_options(hass: HomeAssistant, config_entry: ConfigEntry) -> None:
|
2020-09-20 18:33:26 +02:00
|
|
|
"""Update options."""
|
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|
|
|
|
|
|
|
|
|
|
|
2025-11-27 07:35:09 -10:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
2020-09-20 18:33:26 +02:00
|
|
|
"""Unload a config entry."""
|
2020-09-29 13:13:08 +02:00
|
|
|
unload_ok = await hass.config_entries.async_forward_entry_unload(
|
2023-07-23 14:24:22 -07:00
|
|
|
config_entry,
|
|
|
|
|
"switch",
|
2020-09-20 18:33:26 +02:00
|
|
|
)
|
2020-09-28 23:39:55 +02:00
|
|
|
data = hass.data[DOMAIN]
|
|
|
|
|
data[config_entry.entry_id][UNDO_UPDATE_LISTENER]()
|
2020-10-17 15:43:24 +02:00
|
|
|
if unload_ok:
|
|
|
|
|
data.pop(config_entry.entry_id)
|
|
|
|
|
|
2023-07-22 17:57:49 -07:00
|
|
|
if len(data) == 1 and ATTR_ADAPTIVE_LIGHTING_MANAGER in data:
|
2020-10-17 15:43:24 +02:00
|
|
|
# no more config_entries
|
2023-07-22 17:57:49 -07:00
|
|
|
manager = data.pop(ATTR_ADAPTIVE_LIGHTING_MANAGER)
|
|
|
|
|
manager.disable()
|
2020-09-20 18:33:26 +02:00
|
|
|
|
2020-10-17 15:43:24 +02:00
|
|
|
if not data:
|
|
|
|
|
hass.data.pop(DOMAIN)
|
2020-09-20 18:33:26 +02:00
|
|
|
|
2020-09-20 18:38:01 +02:00
|
|
|
return unload_ok
|