mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-28 21:04:19 +02:00
Merge branch 'basnijholt:master' into master
This commit is contained in:
commit
ab32139b07
6 changed files with 113 additions and 69 deletions
|
|
@ -1,5 +1,7 @@
|
|||
"""Constants for the Adaptive Lighting integration."""
|
||||
|
||||
from homeassistant.components.light import VALID_TRANSITION
|
||||
from homeassistant.helpers import selector
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
import voluptuous as vol
|
||||
|
||||
|
|
@ -30,6 +32,11 @@ CONF_SEPARATE_TURN_ON_COMMANDS, DEFAULT_SEPARATE_TURN_ON_COMMANDS = (
|
|||
)
|
||||
CONF_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS = "sleep_brightness", 1
|
||||
CONF_SLEEP_COLOR_TEMP, DEFAULT_SLEEP_COLOR_TEMP = "sleep_color_temp", 1000
|
||||
CONF_SLEEP_RGB_COLOR, DEFAULT_SLEEP_RGB_COLOR = "sleep_rgb_color", [255, 56, 0]
|
||||
CONF_SLEEP_RGB_OR_COLOR_TEMP, DEFAULT_SLEEP_RGB_OR_COLOR_TEMP = (
|
||||
"sleep_rgb_or_color_temp",
|
||||
"color_temp",
|
||||
)
|
||||
CONF_SUNRISE_OFFSET, DEFAULT_SUNRISE_OFFSET = "sunrise_offset", 0
|
||||
CONF_SUNRISE_TIME = "sunrise_time"
|
||||
CONF_SUNSET_OFFSET, DEFAULT_SUNSET_OFFSET = "sunset_offset", 0
|
||||
|
|
@ -72,7 +79,23 @@ VALIDATION_TUPLES = [
|
|||
(CONF_MIN_COLOR_TEMP, DEFAULT_MIN_COLOR_TEMP, int_between(1000, 10000)),
|
||||
(CONF_MAX_COLOR_TEMP, DEFAULT_MAX_COLOR_TEMP, int_between(1000, 10000)),
|
||||
(CONF_SLEEP_BRIGHTNESS, DEFAULT_SLEEP_BRIGHTNESS, int_between(1, 100)),
|
||||
(
|
||||
CONF_SLEEP_RGB_OR_COLOR_TEMP,
|
||||
DEFAULT_SLEEP_RGB_OR_COLOR_TEMP,
|
||||
selector.SelectSelector(
|
||||
selector.SelectSelectorConfig(
|
||||
options=["color_temp", "rgb_color"],
|
||||
multiple=False,
|
||||
mode=selector.SelectSelectorMode.DROPDOWN,
|
||||
),
|
||||
),
|
||||
),
|
||||
(CONF_SLEEP_COLOR_TEMP, DEFAULT_SLEEP_COLOR_TEMP, int_between(1000, 10000)),
|
||||
(
|
||||
CONF_SLEEP_RGB_COLOR,
|
||||
DEFAULT_SLEEP_RGB_COLOR,
|
||||
selector.ColorRGBSelector(selector.ColorRGBSelectorConfig()),
|
||||
),
|
||||
(CONF_SUNRISE_TIME, NONE_STR, str),
|
||||
(CONF_SUNRISE_OFFSET, DEFAULT_SUNRISE_OFFSET, int),
|
||||
(CONF_SUNSET_TIME, NONE_STR, str),
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
"config_flow": true,
|
||||
"dependencies": [],
|
||||
"codeowners": ["@basnijholt", "@RubenKelevra"],
|
||||
"version": "1.0.20",
|
||||
"version": "1.1.0",
|
||||
"requirements": [],
|
||||
"iot_class": "calculated"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,26 +20,28 @@
|
|||
"description": "All settings for a Adaptive Lighting component. The option names correspond with the YAML settings. No options are shown if you have this entry defined in YAML.",
|
||||
"data": {
|
||||
"lights": "lights",
|
||||
"initial_transition": "initial_transition, when lights go 'off' to 'on'",
|
||||
"sleep_transition": "sleep_transition, when 'sleep_state' changes",
|
||||
"interval": "interval, time between switch updates in seconds",
|
||||
"max_brightness": "max_brightness, in %",
|
||||
"max_color_temp": "max_color_temp, in Kelvin",
|
||||
"min_brightness": "min_brightness, in %",
|
||||
"min_color_temp": "min_color_temp, in Kelvin",
|
||||
"only_once": "only_once, only adapt the lights when turning them on",
|
||||
"prefer_rgb_color": "prefer_rgb_color, use 'rgb_color' over 'color_temp' when possible",
|
||||
"separate_turn_on_commands": "separate_turn_on_commands, for each attribute (color, brightness, etc.) in 'light.turn_on', required for some lights.",
|
||||
"sleep_brightness": "sleep_brightness, in %",
|
||||
"sleep_color_temp": "sleep_color_temp, in Kelvin",
|
||||
"sunrise_offset": "sunrise_offset, in +/- seconds",
|
||||
"sunrise_time": "sunrise_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunrise time at your location)",
|
||||
"sunset_offset": "sunset_offset, in +/- seconds",
|
||||
"sunset_time": "sunset_time, in 'HH:MM:SS' format (if 'None', it uses the actual sunset time at your location)",
|
||||
"take_over_control": "take_over_control, if anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.",
|
||||
"detect_non_ha_changes": "detect_non_ha_changes, detects all >5% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)",
|
||||
"transition": "transition, in seconds",
|
||||
"adapt_delay": "Wait time between light turn on, and Adaptive Lights applying changes to the light state. May avoid flickering."
|
||||
"initial_transition": "initial_transition: When lights turn 'off' to 'on'. (seconds)",
|
||||
"sleep_transition": "sleep_transition: When 'sleep_state' changes. (seconds)",
|
||||
"interval": "interval: Time between switch updates. (seconds)",
|
||||
"max_brightness": "max_brightness: Highest brightness of lights during a cycle. (%)",
|
||||
"max_color_temp": "max_color_temp: Coldest hue of the color temperature cycle. (Kelvin)",
|
||||
"min_brightness": "min_brightness: Lowest brightness of lights during a cycle. (%)",
|
||||
"min_color_temp": "min_color_temp, Warmest hue of the color temperature cycle. (Kelvin)",
|
||||
"only_once": "only_once: Only adapt the lights when turning them on.",
|
||||
"prefer_rgb_color": "prefer_rgb_color: Use 'rgb_color' rather than 'color_temp' when possible.",
|
||||
"separate_turn_on_commands": "separate_turn_on_commands: Separate the commands for each attribute (color, brightness, etc.) in 'light.turn_on' (required for some lights).",
|
||||
"sleep_brightness": "sleep_brightness, Brightness setting for Sleep Mode. (%)",
|
||||
"sleep_rgb_or_color_temp": "sleep_rgb_or_color_temp, use 'rgb_color' or 'color_temp'",
|
||||
"sleep_rgb_color": "sleep_rgb_color, in RGB",
|
||||
"sleep_color_temp": "sleep_color_temp: Color temperature setting for Sleep Mode. (Kelvin)",
|
||||
"sunrise_offset": "sunrise_offset: How long before(-) or after(+) to define the sunrise point of the cycle (+/- seconds)",
|
||||
"sunrise_time": "sunrise_time: Manual override of the sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)",
|
||||
"sunset_offset": "sunset_offset: How long before(-) or after(+) to define the sunset point of the cycle (+/- seconds)",
|
||||
"sunset_time": "sunset_time: Manual override of the sunset time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)",
|
||||
"take_over_control": "take_over_control: If anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.",
|
||||
"detect_non_ha_changes": "detect_non_ha_changes: detects all >10% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)",
|
||||
"transition": "Transition time when applying a change to the lights (seconds)",
|
||||
"adapt_delay": "adapt_delay: wait time between light turn on (seconds), and Adaptive Lights applying changes to the light state. May avoid flickering."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ from datetime import timedelta
|
|||
import functools
|
||||
import logging
|
||||
import math
|
||||
from typing import Any
|
||||
from typing import Any, Literal
|
||||
|
||||
import astral
|
||||
from homeassistant.components.light import (
|
||||
|
|
@ -113,6 +113,8 @@ from .const import (
|
|||
CONF_SEPARATE_TURN_ON_COMMANDS,
|
||||
CONF_SLEEP_BRIGHTNESS,
|
||||
CONF_SLEEP_COLOR_TEMP,
|
||||
CONF_SLEEP_RGB_COLOR,
|
||||
CONF_SLEEP_RGB_OR_COLOR_TEMP,
|
||||
CONF_SLEEP_TRANSITION,
|
||||
CONF_SUNRISE_OFFSET,
|
||||
CONF_SUNRISE_TIME,
|
||||
|
|
@ -587,6 +589,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
min_color_temp=data[CONF_MIN_COLOR_TEMP],
|
||||
sleep_brightness=data[CONF_SLEEP_BRIGHTNESS],
|
||||
sleep_color_temp=data[CONF_SLEEP_COLOR_TEMP],
|
||||
sleep_rgb_color=data[CONF_SLEEP_RGB_COLOR],
|
||||
sleep_rgb_or_color_temp=data[CONF_SLEEP_RGB_OR_COLOR_TEMP],
|
||||
sunrise_offset=data[CONF_SUNRISE_OFFSET],
|
||||
sunrise_time=data[CONF_SUNRISE_TIME],
|
||||
sunset_offset=data[CONF_SUNSET_OFFSET],
|
||||
|
|
@ -802,17 +806,24 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
|||
brightness = round(255 * self._settings["brightness_pct"] / 100)
|
||||
service_data[ATTR_BRIGHTNESS] = brightness
|
||||
|
||||
sleep_rgb = (
|
||||
self.sleep_mode_switch.is_on
|
||||
and self._sun_light_settings.sleep_rgb_or_color_temp == "rgb_color"
|
||||
)
|
||||
if (
|
||||
"color_temp" in features
|
||||
and adapt_color
|
||||
and not (prefer_rgb_color and "color" in features)
|
||||
and not (sleep_rgb and "color" in features)
|
||||
):
|
||||
_LOGGER.debug("%s: Setting color_temp of light %s", self._name, light)
|
||||
attributes = self.hass.states.get(light).attributes
|
||||
min_mireds, max_mireds = attributes["min_mireds"], attributes["max_mireds"]
|
||||
color_temp_mired = self._settings["color_temp_mired"]
|
||||
color_temp_mired = max(min(color_temp_mired, max_mireds), min_mireds)
|
||||
service_data[ATTR_COLOR_TEMP] = color_temp_mired
|
||||
elif "color" in features and adapt_color:
|
||||
_LOGGER.debug("%s: Setting rgb_color of light %s", self._name, light)
|
||||
service_data[ATTR_RGB_COLOR] = self._settings["rgb_color"]
|
||||
|
||||
context = context or self.create_context("adapt_lights")
|
||||
|
|
@ -1073,7 +1084,9 @@ class SunLightSettings:
|
|||
min_brightness: int
|
||||
min_color_temp: int
|
||||
sleep_brightness: int
|
||||
sleep_rgb_or_color_temp: Literal["color_temp", "rgb_color"]
|
||||
sleep_color_temp: int
|
||||
sleep_rgb_color: tuple[int, int, int]
|
||||
sunrise_offset: datetime.timedelta | None
|
||||
sunrise_time: datetime.time | None
|
||||
sunset_offset: datetime.timedelta | None
|
||||
|
|
@ -1192,10 +1205,8 @@ class SunLightSettings:
|
|||
percent = 1 + percent
|
||||
return (delta_brightness * percent) + self.min_brightness
|
||||
|
||||
def calc_color_temp_kelvin(self, percent: float, is_sleep: bool) -> float:
|
||||
def calc_color_temp_kelvin(self, percent: float) -> float:
|
||||
"""Calculate the color temperature in Kelvin."""
|
||||
if is_sleep:
|
||||
return self.sleep_color_temp
|
||||
if percent > 0:
|
||||
delta = self.max_color_temp - self.min_color_temp
|
||||
return (delta * percent) + self.min_color_temp
|
||||
|
|
@ -1214,11 +1225,15 @@ class SunLightSettings:
|
|||
else self.calc_percent(0)
|
||||
)
|
||||
brightness_pct = self.calc_brightness_pct(percent, is_sleep)
|
||||
color_temp_kelvin = self.calc_color_temp_kelvin(percent, is_sleep)
|
||||
if is_sleep:
|
||||
color_temp_kelvin = self.sleep_color_temp
|
||||
rgb_color: tuple[float, float, float] = self.sleep_rgb_color
|
||||
else:
|
||||
color_temp_kelvin = self.calc_color_temp_kelvin(percent)
|
||||
rgb_color: tuple[float, float, float] = color_temperature_to_rgb(
|
||||
color_temp_kelvin
|
||||
)
|
||||
color_temp_mired: float = color_temperature_kelvin_to_mired(color_temp_kelvin)
|
||||
rgb_color: tuple[float, float, float] = color_temperature_to_rgb(
|
||||
color_temp_kelvin
|
||||
)
|
||||
xy_color: tuple[float, float] = color_RGB_to_xy(*rgb_color)
|
||||
hs_color: tuple[float, float] = color_xy_to_hs(*xy_color)
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -32,6 +32,8 @@
|
|||
"prefer_rgb_color": "prefer_rgb_color: Use 'rgb_color' rather than 'color_temp' when possible.",
|
||||
"separate_turn_on_commands": "separate_turn_on_commands: Separate the commands for each attribute (color, brightness, etc.) in 'light.turn_on' (required for some lights).",
|
||||
"sleep_brightness": "sleep_brightness, Brightness setting for Sleep Mode. (%)",
|
||||
"sleep_rgb_or_color_temp": "sleep_rgb_or_color_temp, use 'rgb_color' or 'color_temp'",
|
||||
"sleep_rgb_color": "sleep_rgb_color, in RGB",
|
||||
"sleep_color_temp": "sleep_color_temp: Color temperature setting for Sleep Mode. (Kelvin)",
|
||||
"sunrise_offset": "sunrise_offset: How long before(-) or after(+) to define the sunrise point of the cycle (+/- seconds)",
|
||||
"sunrise_time": "sunrise_time: Manual override of the sunrise time, if 'None', it uses the actual sunrise time at your location (HH:MM:SS)",
|
||||
|
|
@ -40,7 +42,7 @@
|
|||
"take_over_control": "take_over_control: If anything but Adaptive Lighting calls 'light.turn_on' when a light is already on, stop adapting that light until it (or the switch) toggles off -> on.",
|
||||
"detect_non_ha_changes": "detect_non_ha_changes: detects all >10% changes made to the lights (also outside of HA), requires 'take_over_control' to be enabled (calls 'homeassistant.update_entity' every 'interval'!)",
|
||||
"transition": "Transition time when applying a change to the lights (seconds)",
|
||||
"adapt_delay": "Wait time between light turn on, and Adaptive Lights applying changes to the light state. May avoid flickering."
|
||||
"adapt_delay": "adapt_delay: wait time between light turn on (seconds), and Adaptive Lights applying changes to the light state. May avoid flickering."
|
||||
}
|
||||
}
|
||||
},
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue