mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-11 22:34:04 +02:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
133057b953
commit
1eccd1eae8
6 changed files with 42 additions and 24 deletions
|
|
@ -2,12 +2,12 @@
|
|||
|
||||
import logging
|
||||
from typing import Any, Optional
|
||||
import voluptuous as vol
|
||||
|
||||
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
|
||||
from homeassistant.core import HomeAssistant, Event
|
||||
from homeassistant.core import Event, HomeAssistant
|
||||
|
||||
from .const import (
|
||||
_DOMAIN_SCHEMA,
|
||||
|
|
@ -35,14 +35,16 @@ CONFIG_SCHEMA = vol.Schema(
|
|||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
async def reload_configuration_yaml(event: Event) -> None:
|
||||
"""Reload configuration.yaml."""
|
||||
hass: Optional[HomeAssistant] = event.data["hass"] if "hass" in event.data else None
|
||||
hass: HomeAssistant | None = event.data["hass"] if "hass" in event.data else None
|
||||
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."""
|
||||
if DOMAIN in config:
|
||||
|
|
|
|||
|
|
@ -376,7 +376,7 @@ class SunLightSettings:
|
|||
def get_settings(
|
||||
self,
|
||||
is_sleep: bool,
|
||||
transition: float | int | None,
|
||||
transition: float | None,
|
||||
) -> dict[str, float | int | tuple[float, float] | tuple[float, float, float]]:
|
||||
"""Get all light settings.
|
||||
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
"""Handle configuration by YAML file."""
|
||||
if user_input is None:
|
||||
return self.async_abort(reason="no_data")
|
||||
|
||||
|
||||
await self.async_set_unique_id(user_input[CONF_NAME])
|
||||
# Keep a list of switches that are configured via YAML
|
||||
data = self.hass.data.setdefault(DOMAIN, {})
|
||||
|
|
@ -61,7 +61,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
|
||||
@staticmethod
|
||||
@callback
|
||||
def async_get_options_flow(config_entry: config_entries.ConfigEntry) -> "OptionsFlowHandler":
|
||||
def async_get_options_flow(
|
||||
config_entry: config_entries.ConfigEntry,
|
||||
) -> "OptionsFlowHandler":
|
||||
"""Get the options flow for this handler."""
|
||||
if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 12):
|
||||
# https://github.com/home-assistant/core/pull/129651
|
||||
|
|
@ -125,8 +127,11 @@ class OptionsFlowHandler(config_entries.OptionsFlow):
|
|||
)
|
||||
all_lights.append(configured_light)
|
||||
all_lights_with_names[configured_light] = configured_light
|
||||
|
||||
light_options = {entity_id: f"{name} ({entity_id})" for entity_id, name in all_lights_with_names.items()}
|
||||
|
||||
light_options = {
|
||||
entity_id: f"{name} ({entity_id})"
|
||||
for entity_id, name in all_lights_with_names.items()
|
||||
}
|
||||
to_replace = {CONF_LIGHTS: cv.multi_select(light_options)}
|
||||
|
||||
options_schema = {}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
"""Constants for the Adaptive Lighting integration."""
|
||||
|
||||
from typing import Any, List, Tuple, Optional
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import voluptuous as vol
|
||||
|
|
@ -293,11 +293,13 @@ DOCS_APPLY = {
|
|||
CONF_LIGHTS: "A light (or list of lights) to apply the settings to. 💡",
|
||||
}
|
||||
|
||||
|
||||
def int_between(min_int: int, max_int: int) -> vol.All:
|
||||
"""Return an integer between 'min_int' and 'max_int'."""
|
||||
return vol.All(vol.Coerce(int), vol.Range(min=min_int, max=max_int))
|
||||
|
||||
VALIDATION_TUPLES: List[Tuple[str, Any, Any]] = [
|
||||
|
||||
VALIDATION_TUPLES: list[tuple[str, Any, Any]] = [
|
||||
(CONF_LIGHTS, DEFAULT_LIGHTS, cv.entity_ids), # type: ignore
|
||||
(CONF_INTERVAL, DEFAULT_INTERVAL, cv.positive_int),
|
||||
(CONF_TRANSITION, DEFAULT_TRANSITION, VALID_TRANSITION),
|
||||
|
|
@ -370,6 +372,7 @@ VALIDATION_TUPLES: List[Tuple[str, Any, Any]] = [
|
|||
(CONF_INCLUDE_CONFIG_IN_ATTRIBUTES, DEFAULT_INCLUDE_CONFIG_IN_ATTRIBUTES, bool),
|
||||
]
|
||||
|
||||
|
||||
def timedelta_as_int(value: timedelta) -> float:
|
||||
"""Convert a `datetime.timedelta` object to an integer.
|
||||
|
||||
|
|
@ -402,7 +405,8 @@ def maybe_coerce(key: str, validation: Any) -> vol.All | Any:
|
|||
return vol.All(validation, vol.Coerce(coerce))
|
||||
return validation
|
||||
|
||||
def replace_none_str(value: Any, replace_with: Optional[Any] = None) -> Any:
|
||||
|
||||
def replace_none_str(value: Any, replace_with: Any | None = None) -> Any:
|
||||
"""Replace "None" -> replace_with."""
|
||||
return value if value != NONE_STR else replace_with
|
||||
|
||||
|
|
@ -440,8 +444,8 @@ def apply_service_schema(initial_transition: int = 1) -> vol.Schema:
|
|||
|
||||
SET_MANUAL_CONTROL_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Optional(CONF_ENTITY_ID): cv.entity_ids, # type: ignore
|
||||
vol.Optional(CONF_LIGHTS, default=[]): cv.entity_ids, # type: ignore
|
||||
vol.Optional(CONF_ENTITY_ID): cv.entity_ids, # type: ignore
|
||||
vol.Optional(CONF_LIGHTS, default=[]): cv.entity_ids, # type: ignore
|
||||
vol.Optional(CONF_MANUAL_CONTROL, default=True): cv.boolean,
|
||||
},
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
"""Helper functions for the Adaptive Lighting custom components."""
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import Any, Dict
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
import base64
|
||||
import math
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
def clamp(value: float, minimum: float, maximum: float) -> float:
|
||||
|
|
@ -86,10 +87,11 @@ def color_difference_redmean(
|
|||
blue_term = (2 + (255 - r_hat) / 256) * delta_b**2
|
||||
return math.sqrt(red_term + green_term + blue_term)
|
||||
|
||||
|
||||
def get_friendly_name(hass: HomeAssistant, entity_id: str) -> str:
|
||||
"""Retrieve the friendly name of an entity."""
|
||||
state = hass.states.get(entity_id)
|
||||
if state and hasattr(state, "attributes"):
|
||||
attributes: Dict[str, Any] = dict(getattr(state, "attributes", {}))
|
||||
attributes: dict[str, Any] = dict(getattr(state, "attributes", {}))
|
||||
return attributes.get("friendly_name", entity_id)
|
||||
return entity_id
|
||||
return entity_id
|
||||
|
|
|
|||
|
|
@ -229,12 +229,11 @@ def _switches_with_lights(
|
|||
hass: HomeAssistant,
|
||||
lights: list[str],
|
||||
expand_light_groups: bool = True,
|
||||
) -> list["AdaptiveSwitch"]:
|
||||
) -> list[AdaptiveSwitch]:
|
||||
"""Get all switches that control at least one of the lights passed."""
|
||||
config_entries = hass.config_entries.async_entries(DOMAIN)
|
||||
data = hass.data[DOMAIN]
|
||||
from typing import List
|
||||
switches: List["AdaptiveSwitch"] = []
|
||||
switches: list[AdaptiveSwitch] = []
|
||||
all_check_lights = (
|
||||
_expand_light_groups(hass, lights) if expand_light_groups else set(lights)
|
||||
)
|
||||
|
|
@ -623,6 +622,7 @@ def _is_light_group(state: State) -> bool:
|
|||
False,
|
||||
)
|
||||
|
||||
|
||||
def _supported_features(hass: HomeAssistant, light: str) -> set[str]:
|
||||
state = hass.states.get(light)
|
||||
assert state is not None
|
||||
|
|
@ -634,7 +634,7 @@ def _supported_features(hass: HomeAssistant, light: str) -> set[str]:
|
|||
if supported_features & LightEntityFeature.TRANSITION:
|
||||
supported.add("transition")
|
||||
|
||||
supported_color_modes = state.attributes.get(ATTR_SUPPORTED_COLOR_MODES, set()) # type: ignore
|
||||
supported_color_modes = state.attributes.get(ATTR_SUPPORTED_COLOR_MODES, set()) # type: ignore
|
||||
color_modes = {
|
||||
ColorMode.RGB,
|
||||
ColorMode.RGBW,
|
||||
|
|
@ -659,6 +659,7 @@ def _supported_features(hass: HomeAssistant, light: str) -> set[str]:
|
|||
|
||||
return supported
|
||||
|
||||
|
||||
# All comparisons should be done with RGB since
|
||||
# converting anything to color temp is inaccurate.
|
||||
def _convert_attributes(attributes: dict[str, Any]) -> dict[str, Any]:
|
||||
|
|
@ -1131,7 +1132,9 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
self._remove_listeners()
|
||||
self.manager.reset(*self.lights)
|
||||
|
||||
async def _async_update_at_interval_action(self, now: Any = None) -> None: # noqa: ARG002
|
||||
async def _async_update_at_interval_action(
|
||||
self, now: Any = None
|
||||
) -> None: # noqa: ARG002
|
||||
"""Update the attributes and maybe adapt the lights."""
|
||||
await self._update_attrs_and_maybe_adapt_lights(
|
||||
context=self.create_context("interval"),
|
||||
|
|
@ -2114,7 +2117,9 @@ class AdaptiveLightingManager:
|
|||
|
||||
self._handle_timer(light, self.transition_timers, last_transition, reset)
|
||||
|
||||
def set_auto_reset_manual_control_times(self, lights: list[str], time: float) -> None:
|
||||
def set_auto_reset_manual_control_times(
|
||||
self, lights: list[str], time: float
|
||||
) -> None:
|
||||
"""Set the time after which the lights are automatically reset."""
|
||||
if time == 0:
|
||||
return
|
||||
|
|
@ -2671,7 +2676,7 @@ class AdaptiveLightingManager:
|
|||
|
||||
|
||||
class _AsyncSingleShotTimer:
|
||||
def __init__(self, delay: float, callback: "Callable[[], None | Any]") -> None:
|
||||
def __init__(self, delay: float, callback: Callable[[], None | Any]) -> None:
|
||||
"""Initialize the timer."""
|
||||
self.delay = delay
|
||||
self.callback = callback
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue