mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-11 14:24:03 +02:00
* Register service actions in async_setup for Bronze tier compliance - Move 'apply' and 'set_manual_control' service registration from async_setup_entry to async_setup. - Move service handlers to module-level functions in switch.py. - Update apply_service_schema to support dynamic defaults for transition duration. - Clean up related unused imports and fix Python 3.10 syntax compatibility. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Clean up and add tests * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix lint errors * Automated update of generated docs * Re-add types * Make transition not required again * fix: validate global service targets * fix: document optional apply transition * fix: derive service docs from schema markers * docs: clarify service target options * fix: preserve entity service target handling --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Bas Nijholt <bas@nijho.lt>
142 lines
4.3 KiB
Python
142 lines
4.3 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"]
|
|
|
|
|
|
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_ok = await hass.config_entries.async_forward_entry_unload(
|
|
config_entry,
|
|
"switch",
|
|
)
|
|
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
|