Add min and max brightness parameters for Light dim_relative Action (#6971)

This commit is contained in:
PaoloTK 2024-08-13 23:29:55 +02:00 committed by GitHub
parent 68c56b3e03
commit c5b1a8eb81
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 58 additions and 1 deletions

View file

@ -7,6 +7,8 @@
namespace esphome {
namespace light {
enum class LimitMode { CLAMP, DO_NOTHING };
template<typename... Ts> class ToggleAction : public Action<Ts...> {
public:
explicit ToggleAction(LightState *state) : state_(state) {}
@ -77,7 +79,10 @@ template<typename... Ts> class DimRelativeAction : public Action<Ts...> {
float rel = this->relative_brightness_.value(x...);
float cur;
this->parent_->remote_values.as_brightness(&cur);
float new_brightness = clamp(cur + rel, 0.0f, 1.0f);
if ((limit_mode_ == LimitMode::DO_NOTHING) && ((cur < min_brightness_) || (cur > max_brightness_))) {
return;
}
float new_brightness = clamp(cur + rel, min_brightness_, max_brightness_);
call.set_state(new_brightness != 0.0f);
call.set_brightness(new_brightness);
@ -85,8 +90,18 @@ template<typename... Ts> class DimRelativeAction : public Action<Ts...> {
call.perform();
}
void set_min_max_brightness(float min, float max) {
this->min_brightness_ = min;
this->max_brightness_ = max;
}
void set_limit_mode(LimitMode limit_mode) { this->limit_mode_ = limit_mode; }
protected:
LightState *parent_;
float min_brightness_{0.0};
float max_brightness_{1.0};
LimitMode limit_mode_{LimitMode::CLAMP};
};
template<typename... Ts> class LightIsOnCondition : public Condition<Ts...> {

View file

@ -19,10 +19,15 @@ from esphome.const import (
CONF_WARM_WHITE,
CONF_RANGE_FROM,
CONF_RANGE_TO,
CONF_BRIGHTNESS_LIMITS,
CONF_LIMIT_MODE,
CONF_MIN_BRIGHTNESS,
CONF_MAX_BRIGHTNESS,
)
from .types import (
ColorMode,
COLOR_MODES,
LIMIT_MODES,
DimRelativeAction,
ToggleAction,
LightState,
@ -167,6 +172,15 @@ LIGHT_DIM_RELATIVE_ACTION_SCHEMA = cv.Schema(
cv.Optional(CONF_TRANSITION_LENGTH): cv.templatable(
cv.positive_time_period_milliseconds
),
cv.Optional(CONF_BRIGHTNESS_LIMITS): cv.Schema(
{
cv.Optional(CONF_MIN_BRIGHTNESS, default="0%"): cv.percentage,
cv.Optional(CONF_MAX_BRIGHTNESS, default="100%"): cv.percentage,
cv.Optional(CONF_LIMIT_MODE, default="CLAMP"): cv.enum(
LIMIT_MODES, upper=True, space="_"
),
}
),
}
)
@ -182,6 +196,13 @@ async def light_dim_relative_to_code(config, action_id, template_arg, args):
if CONF_TRANSITION_LENGTH in config:
templ = await cg.templatable(config[CONF_TRANSITION_LENGTH], args, cg.uint32)
cg.add(var.set_transition_length(templ))
if conf := config.get(CONF_BRIGHTNESS_LIMITS):
cg.add(
var.set_min_max_brightness(
conf[CONF_MIN_BRIGHTNESS], conf[CONF_MAX_BRIGHTNESS]
)
)
cg.add(var.set_limit_mode(conf[CONF_LIMIT_MODE]))
return var

View file

@ -26,6 +26,13 @@ COLOR_MODES = {
"RGB_COLD_WARM_WHITE": ColorMode.RGB_COLD_WARM_WHITE,
}
# Limit modes
LimitMode = light_ns.enum("LimitMode", is_class=True)
LIMIT_MODES = {
"CLAMP": LimitMode.CLAMP,
"DO_NOTHING": LimitMode.DO_NOTHING,
}
# Actions
ToggleAction = light_ns.class_("ToggleAction", automation.Action)
LightControlAction = light_ns.class_("LightControlAction", automation.Action)

View file

@ -95,6 +95,7 @@ CONF_BOARD_FLASH_MODE = "board_flash_mode"
CONF_BORDER = "border"
CONF_BRANCH = "branch"
CONF_BRIGHTNESS = "brightness"
CONF_BRIGHTNESS_LIMITS = "brightness_limits"
CONF_BROKER = "broker"
CONF_BSSID = "bssid"
CONF_BUFFER_SIZE = "buffer_size"
@ -429,6 +430,7 @@ CONF_LIGHT = "light"
CONF_LIGHT_ID = "light_id"
CONF_LIGHTNING_ENERGY = "lightning_energy"
CONF_LIGHTNING_THRESHOLD = "lightning_threshold"
CONF_LIMIT_MODE = "limit_mode"
CONF_LINE_THICKNESS = "line_thickness"
CONF_LINE_TYPE = "line_type"
CONF_LOADED_INTEGRATIONS = "loaded_integrations"

View file

@ -15,6 +15,8 @@ esphome:
- light.dim_relative:
id: test_monochromatic_light
relative_brightness: 5%
brightness_limits:
max_brightness: 90%
output:
- platform: gpio

View file

@ -15,6 +15,8 @@ esphome:
- light.dim_relative:
id: test_monochromatic_light
relative_brightness: 5%
brightness_limits:
max_brightness: 90%
output:
- platform: gpio

View file

@ -15,6 +15,8 @@ esphome:
- light.dim_relative:
id: test_monochromatic_light
relative_brightness: 5%
brightness_limits:
max_brightness: 90%
output:
- platform: gpio

View file

@ -15,6 +15,8 @@ esphome:
- light.dim_relative:
id: test_monochromatic_light
relative_brightness: 5%
brightness_limits:
max_brightness: 90%
output:
- platform: gpio

View file

@ -15,6 +15,8 @@ esphome:
- light.dim_relative:
id: test_monochromatic_light
relative_brightness: 5%
brightness_limits:
max_brightness: 90%
output:
- platform: gpio

View file

@ -15,6 +15,8 @@ esphome:
- light.dim_relative:
id: test_monochromatic_light
relative_brightness: 5%
brightness_limits:
max_brightness: 90%
output:
- platform: gpio