max on time for LED in V1.3

This commit is contained in:
root
2025-08-22 09:02:26 +12:00
parent ab1240e720
commit 7aa8badcae

View File

@@ -125,70 +125,74 @@ packages:
esphome:
name: "${device_name}"
friendly_name: "${friendly_name}"
comment: "${description_comment}" # Appears on the esphome page in HA
comment: "${description_comment}"
area: "${device_area}"
on_boot:
priority: -200
then:
# Keep the HA dropdown in sync with the stored mode
- lambda: |-
ESP_LOGI("boot", "Last reset reason: %s", ESP.getResetReason().c_str());
// Keep the HA dropdown in sync with the stored mode
switch (id(restart_mode)) {
case 0: id(restart_action).publish_state("Fade up to full"); break;
case 1: id(restart_action).publish_state("Restore Brightness"); break;
case 2: default: id(restart_action).publish_state("Remain Off"); break;
}
# Mode 0: Fade up to full (respect min/max & ramp time)
# Mode 0: Fade up to full (obeys fade settings; no on/off flicker)
- if:
condition:
lambda: 'return id(restart_mode) == 0;'
then:
- lambda: 'id(ramp_switch_target_on) = true;'
- script.execute: ramp_on_script
# Mode 1: Restore Brightness quickly
# Mode 1: Restore Brightness using the light's normal defaults
# (uses default_transition_length and on_turn_on handlers)
- if:
condition:
lambda: 'return id(restart_mode) == 1;'
then:
- lambda: |-
// Clamp the remembered brightness to valid bounds
float target = id(last_brightness_pct);
if (target < 0.0f) target = 0.0f;
if (target > 100.0f) target = 100.0f;
float minp = (float) id(min_brightness_pct);
float maxp = (float) id(max_brightness_pct);
if (target > 0.0f) {
if (target < minp) target = minp;
// Gently clamp to min/max caps to avoid an immediate post-on_state correction.
const float minp = (float) id(min_brightness_pct);
const float maxp = (float) id(max_brightness_pct);
if (target > 0.0f && target < minp) target = minp;
if (target > maxp) target = maxp;
}
id(suppress_slider_sync) = true;
if (target <= 0.0f) {
id(ramp_switch_target_on) = false;
auto call = id(mosfet_leds).make_call();
call.set_state(false);
call.set_transition_length(0);
call.perform();
id(ramp_switch_target_on) = false;
} else {
id(ramp_switch_target_on) = true;
auto call = id(mosfet_leds).make_call();
call.set_state(true);
call.set_brightness(target / 100.0f);
call.set_transition_length(150);
// No transition_length here: use light.default_transition_length.
call.perform();
id(ramp_switch_target_on) = true;
}
- delay: 300ms
- lambda: 'id(suppress_slider_sync) = false;'
# Mode 2: Remain Off
# Mode 2: Remain Off (no blip; stays off)
- if:
condition:
lambda: 'return id(restart_mode) == 2;'
then:
- script.stop: ramp_on_script
- script.stop: ramp_off_script
- lambda: 'id(ramp_switch_target_on) = false;'
- light.turn_off:
id: mosfet_leds
transition_length: 0s
- lambda: 'id(ramp_switch_target_on) = false;'
platformio_options:
build_unflags:
- -flto
@@ -199,7 +203,6 @@ esphome:
- -fdata-sections
- -DNDEBUG
##########################################################################################
# ESP PLATFORM AND FRAMEWORK
# https://esphome.io/components/esp8266.html
@@ -746,17 +749,19 @@ script:
- script.stop: ramp_off_script
- script.stop: led_flash_down
- script.execute: led_flash_up
# Ensure we start at at least the floor without a visible "pop".
- if:
condition:
lambda: |-
const auto &cv = id(mosfet_leds).current_values;
const float floor = id(min_brightness_pct) / 100.0f;
return (!cv.is_on()) || (cv.get_brightness() < floor);
return (!cv.is_on()) || (cv.get_brightness() + 0.0005f < floor);
then:
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(min_brightness_pct) / 100.0f;'
transition_length: 0s
transition_length: 80ms
# Ramp from current (>= floor) to cap over a fraction of ramp_up_ms.
- light.turn_on:
id: mosfet_leds
brightness: !lambda 'return id(max_brightness_pct) / 100.0f;'
@@ -776,6 +781,7 @@ script:
- script.stop: led_flash_up
- output.turn_off: green_led_out
# Script: ramp down from current level to floor, then cleanly cut to OFF
- id: ramp_off_script
mode: restart