From 1eccd1eae8c8dccce87c467e0809d5ab0ca4a70a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 22 Aug 2025 13:25:37 +0000 Subject: [PATCH] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../adaptive_lighting/__init__.py | 8 +++++--- .../adaptive_lighting/color_and_brightness.py | 2 +- .../adaptive_lighting/config_flow.py | 13 +++++++++---- custom_components/adaptive_lighting/const.py | 14 +++++++++----- .../adaptive_lighting/helpers.py | 10 ++++++---- custom_components/adaptive_lighting/switch.py | 19 ++++++++++++------- 6 files changed, 42 insertions(+), 24 deletions(-) diff --git a/custom_components/adaptive_lighting/__init__.py b/custom_components/adaptive_lighting/__init__.py index a0082c5b..9bcf295c 100644 --- a/custom_components/adaptive_lighting/__init__.py +++ b/custom_components/adaptive_lighting/__init__.py @@ -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: diff --git a/custom_components/adaptive_lighting/color_and_brightness.py b/custom_components/adaptive_lighting/color_and_brightness.py index 75fe372c..47caea03 100644 --- a/custom_components/adaptive_lighting/color_and_brightness.py +++ b/custom_components/adaptive_lighting/color_and_brightness.py @@ -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. diff --git a/custom_components/adaptive_lighting/config_flow.py b/custom_components/adaptive_lighting/config_flow.py index 3e05a401..d1799888 100644 --- a/custom_components/adaptive_lighting/config_flow.py +++ b/custom_components/adaptive_lighting/config_flow.py @@ -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 = {} diff --git a/custom_components/adaptive_lighting/const.py b/custom_components/adaptive_lighting/const.py index 27a22fc8..3493fb87 100644 --- a/custom_components/adaptive_lighting/const.py +++ b/custom_components/adaptive_lighting/const.py @@ -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, }, ) diff --git a/custom_components/adaptive_lighting/helpers.py b/custom_components/adaptive_lighting/helpers.py index 56a660c2..67261128 100644 --- a/custom_components/adaptive_lighting/helpers.py +++ b/custom_components/adaptive_lighting/helpers.py @@ -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 \ No newline at end of file + return entity_id diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 2cfa565a..353be57d 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -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