Keep the ambient colour when the light turns on

The MCU resets the colour to Cycle on every turn-on. Remember the chosen colour
across restarts and send it again just after the MCU reports the light on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-09 01:26:02 +01:00
1 parent 06b259cb1b
commit bcc6b3a42b
3 files changed
+70 -12

No files matched your search

+68 -11
View File
@@ -22,7 +22,7 @@ external_components:
- source:
type: git
url: https://mangavalk.com/mangavalk/esphome-wake-up-light-ii.git
ref: v1.0.4
ref: v1.0.5
components: [uart]
refresh: 1d
@@ -136,6 +136,23 @@ globals:
restore_value: no
initial_value: '0'
# Ambient colour (DP132). The MCU resets it to Cycle (0) whenever the ambient
# light turns on, so the chosen colour is kept here and sent again.
- id: ambient_color_saved
type: uint8_t
restore_value: yes
initial_value: '0'
- id: ambient_color_now
type: uint8_t
restore_value: no
initial_value: '0'
- id: ambient_on_ms
type: uint32_t
restore_value: no
initial_value: '0'
# When the MCU last sent its once-a-minute clock report.
- id: last_mcu_report
type: uint32_t
@@ -301,6 +318,24 @@ tuya:
id(alarm_raw_valid) = true;
id(publish_alarm_summaries).execute();
# DP129: ambient light. On every turn-on the MCU resets DP132 to Cycle.
- sensor_datapoint: 129
datapoint_type: bool
then:
- lambda: |-
if (!x) return;
id(ambient_on_ms) = millis();
id(restore_ambient_color).execute();
# DP132: ambient colour. A change reported while the light has been on for a
# moment came from the user (lamp buttons or here), so remember it.
- sensor_datapoint: 132
datapoint_type: enum
then:
- lambda: |-
id(ambient_color_now) = x;
if (millis() - id(ambient_on_ms) > 1500) id(ambient_color_saved) = x;
# DP128: sunset settings, 10 bytes, per Tuya's data model:
# [0] switch (unused) [1] mode: 0=sound, 1=sound+light, 2=radio, 3=radio+light
# [2] sound 1..4 [3] sound volume 1..16 [4] sound duration 10..120
@@ -400,6 +435,15 @@ script:
call.set_transition_length(0);
call.perform();
- id: restore_ambient_color
mode: restart
then:
- delay: 400ms
- lambda: |-
if (id(ambient_color_saved) == 0) return;
ESP_LOGI("ambient", "Restoring ambient colour %u", (unsigned) id(ambient_color_saved));
id(tuya_mcu).set_enum_datapoint_value(132, id(ambient_color_saved));
- id: radio_step
mode: queued
parameters:
@@ -682,19 +726,32 @@ select:
sorting_group_id: group_snooze
sorting_weight: 3
- platform: tuya
- platform: template
id: ambient_color
name: "Ambient Color"
enum_datapoint: 132
options:
0: "Cycle"
1: "Red"
2: "Orange"
3: "Yellow"
4: "Green"
5: "Cyan"
6: "Blue"
7: "Purple"
- "Cycle"
- "Red"
- "Orange"
- "Yellow"
- "Green"
- "Cyan"
- "Blue"
- "Purple"
update_interval: 1s
lambda: |-
static const char *const names[8] = {"Cycle", "Red", "Orange", "Yellow", "Green", "Cyan", "Blue", "Purple"};
if (id(ambient_color_now) > 7) return {};
return std::string(names[id(ambient_color_now)]);
set_action:
- lambda: |-
static const char *const names[8] = {"Cycle", "Red", "Orange", "Yellow", "Green", "Cyan", "Blue", "Purple"};
for (uint8_t i = 0; i < 8; i++) {
if (x == names[i]) {
id(ambient_color_saved) = i;
id(tuya_mcu).set_enum_datapoint_value(132, i);
}
}
web_server:
sorting_group_id: group_light
sorting_weight: 3