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