ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
light_call.cpp
Go to the documentation of this file.
1#include <cinttypes>
2
3#include "light_call.h"
4#include "light_state.h"
5#include "esphome/core/log.h"
8
9namespace esphome::light {
10
11ESPHOME_LOG_TAG(TAG, "light");
12
13// Cold-path logger; caller handles the clamp so the in-range hot path avoids
14// the spill/reload around the call.
15static void log_value_out_of_range(const char *name, float value, const LogString *param_name, float min, float max) {
16 ESP_LOGW(TAG, "'%s': %s value %.2f is out of range [%.1f - %.1f]", name, LOG_STR_ARG(param_name), value, min, max);
17}
18
19#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
20static void log_feature_not_supported(const char *name, const LogString *feature) {
21 ESP_LOGW(TAG, "'%s': %s not supported", name, LOG_STR_ARG(feature));
22}
23
24static void log_color_mode_not_supported(const char *name, const LogString *feature) {
25 ESP_LOGW(TAG, "'%s': color mode does not support setting %s", name, LOG_STR_ARG(feature));
26}
27
28static void log_invalid_parameter(const char *name, const LogString *message) {
29 ESP_LOGW(TAG, "'%s': %s", name, LOG_STR_ARG(message));
30}
31#else
32#define log_feature_not_supported(name, feature)
33#define log_color_mode_not_supported(name, feature)
34#define log_invalid_parameter(name, message)
35#endif
36
37// Macro to reduce repetitive setter code
38#define IMPLEMENT_LIGHT_CALL_SETTER(name, type, flag) \
39 LightCall &LightCall::set_##name(optional<type>(name)) { \
40 if ((name).has_value()) { \
41 this->name##_ = (name).value(); \
42 } \
43 this->set_flag_(flag, (name).has_value()); \
44 return *this; \
45 } \
46 LightCall &LightCall::set_##name(type name) { \
47 this->name##_ = name; \
48 this->set_flag_(flag); \
49 return *this; \
50 }
51
52// Color mode human-readable strings indexed by ColorModeBitPolicy::to_bit() (0-9)
53// Index 0 is Unknown (for ColorMode::UNKNOWN), also used as fallback for out-of-range
54PROGMEM_STRING_TABLE(ColorModeHumanStrings, "Unknown", "On/Off", "Brightness", "White", "Color temperature",
55 "Cold/warm white", "RGB", "RGBW", "RGB + color temperature", "RGB + cold/warm white");
56
57// Indices 0-7 match FieldFlags bits 0-7; index 8 is color_temperature.
58// PROGMEM_STRING_TABLE is constexpr-init (no RAM guard variable).
59PROGMEM_STRING_TABLE(ValidateFieldNames, "Brightness", "Color brightness", "Red", "Green", "Blue", "White",
60 "Cold white", "Warm white", "Color temperature");
61static constexpr uint8_t VALIDATE_CT_INDEX = 8;
62
63static const LogString *color_mode_to_human(ColorMode color_mode) {
64 return ColorModeHumanStrings::get_log_str(ColorModeBitPolicy::to_bit(color_mode), 0);
65}
66
67// Helper to log percentage values
68#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
69static void log_percent(const LogString *param, float value) {
70 ESP_LOGV(TAG, " %s: %.0f%%", LOG_STR_ARG(param), value * 100.0f);
71}
72#else
73#define log_percent(param, value)
74#endif
75
77 const char *name = LOG_STR_ARG(this->parent_->get_log_name());
78 LightColorValues v = this->validate_();
79 // Any call counts as newer, even one that changes nothing
80 this->parent_->called_ = true;
81 const bool publish = this->get_publish_();
82
83 if (publish) {
84 ESP_LOGV(TAG, "'%s' Setting:", name);
85
86 // Only print color mode when it's being changed
87 ColorMode current_color_mode = this->parent_->remote_values.get_color_mode();
88 ColorMode target_color_mode = this->has_color_mode() ? this->color_mode_ : current_color_mode;
89 if (target_color_mode != current_color_mode) {
90 ESP_LOGV(TAG, " Color mode: %s", LOG_STR_ARG(color_mode_to_human(v.get_color_mode())));
91 }
92
93 // Only print state when it's being changed
94 bool current_state = this->parent_->remote_values.is_on();
95 bool target_state = this->has_state() ? this->state_ : current_state;
96 if (target_state != current_state) {
97 ESP_LOGV(TAG, " State: %s", ONOFF(v.is_on()));
98 }
99
100 if (this->has_brightness()) {
101 log_percent(LOG_STR("Brightness"), v.get_brightness());
102 }
103
104 if (this->has_color_brightness()) {
105 log_percent(LOG_STR("Color brightness"), v.get_color_brightness());
106 }
107 if (this->has_red() || this->has_green() || this->has_blue()) {
108 ESP_LOGV(TAG, " Red: %.0f%%, Green: %.0f%%, Blue: %.0f%%", v.get_red() * 100.0f, v.get_green() * 100.0f,
109 v.get_blue() * 100.0f);
110 }
111
112 if (this->has_white()) {
113 log_percent(LOG_STR("White"), v.get_white());
114 }
115 if (this->has_color_temperature()) {
116 ESP_LOGV(TAG, " Color temperature: %.1f mireds", v.get_color_temperature());
117 }
118
119 if (this->has_cold_white() || this->has_warm_white()) {
120 ESP_LOGV(TAG, " Cold white: %.0f%%, warm white: %.0f%%", v.get_cold_white() * 100.0f,
121 v.get_warm_white() * 100.0f);
122 }
123 }
124
125 if (this->has_flash_()) {
126 // FLASH
127 if (publish) {
128 ESP_LOGV(TAG, " Flash length: %.1fs", this->flash_length_ / 1e3f);
129 }
130
131 this->parent_->start_flash_(v, this->flash_length_, publish);
132 } else if (this->has_transition_()) {
133 // TRANSITION
134 if (publish) {
135 ESP_LOGV(TAG, " Transition length: %.1fs", this->transition_length_ / 1e3f);
136 }
137
138 // Special case: Transition and effect can be set when turning off
139 if (this->has_effect_()) {
140 if (publish) {
141 ESP_LOGV(TAG, " Effect: 'None'");
142 }
143 this->parent_->stop_effect_();
144 }
145
146 this->parent_->start_transition_(v, this->transition_length_, publish);
147
148 } else if (this->has_effect_()) {
149 // EFFECT
150 StringRef effect_s;
151 if (this->effect_ == 0u) {
152 effect_s = EFFECT_NONE_REF;
153 } else {
154 effect_s = this->parent_->effects_[this->effect_ - 1]->get_name();
155 }
156
157 if (publish) {
158 ESP_LOGV(TAG, " Effect: '%.*s'", (int) effect_s.size(), effect_s.c_str());
159 }
160
161 if (this->effect_ != this->parent_->active_effect_index_) {
162 this->parent_->start_effect_(this->effect_);
163
164 // Also set light color values when starting an effect
165 // For example to turn off the light
166 this->parent_->set_immediately_(v, true);
167 }
168 } else {
169 // INSTANT CHANGE
170 this->parent_->set_immediately_(v, publish);
171 }
172
174 for (auto *listener : *this->parent_->target_state_reached_listeners_) {
175 listener->on_light_target_state_reached();
176 }
177 }
178 if (publish) {
179 this->parent_->publish_state();
180 }
181 if (this->get_save_()) {
183 }
184}
185
186void LightCall::log_and_clear_unsupported_(FieldFlags flag, const LogString *feature, bool use_color_mode_log) {
187 auto *name = LOG_STR_ARG(this->parent_->get_log_name());
188 if (use_color_mode_log) {
189 log_color_mode_not_supported(name, feature);
190 } else {
191 log_feature_not_supported(name, feature);
192 }
193 this->clear_flag_(flag);
194}
195
197 auto *name = LOG_STR_ARG(this->parent_->get_log_name());
198 auto traits = this->parent_->get_traits();
199
200 // Snapshot before the adjustments below add flags of their own
201 const bool sets_values = (this->flags_ & VALUE_FLAGS_MASK) != 0;
202 const bool plain_turn_on = this->has_state() && this->state_ && (this->flags_ & ~STATE_ONLY_FLAGS_MASK) == 0;
203
204 // Color mode check
205 if (this->has_color_mode() && !traits.supports_color_mode(this->color_mode_)) {
206 ESP_LOGW(TAG, "'%s' does not support color mode %s", name, LOG_STR_ARG(color_mode_to_human(this->color_mode_)));
208 }
209
210 // Ensure there is always a color mode set
211 if (!this->has_color_mode()) {
212 this->color_mode_ = this->compute_color_mode_(traits);
214 }
215 auto color_mode = this->color_mode_;
216
217 // Transform calls that use non-native parameters for the current mode.
218 this->transform_parameters_(traits);
219
220 // Business logic adjustments before validation
221 // Flag whether an explicit turn off was requested, in which case we'll also stop the effect.
222 bool explicit_turn_off_request = this->has_state() && !this->state_;
223
224 // Treat zero brightness as an implicit turn-off when no state was explicitly requested.
225 if (this->has_brightness() && this->brightness_ == 0.0f && !this->has_state()) {
226 this->state_ = false;
228 }
229
230 // A light without brightness control has no way to represent "on but dark", so zero
231 // brightness -- how effects encode their dark phase -- means the light is off. Clear the
232 // brightness as well, so a zero can't linger in remote_values and leave the light stuck
233 // off: a later turn-on can't heal it, because the capability check below drops any
234 // brightness this mode doesn't support. explicit_turn_off_request was captured above, so
235 // a running effect is not stopped by this.
236 if (this->has_brightness() && this->brightness_ == 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) {
237 this->state_ = false;
240 }
241
242 // Make sure a simple (no specific brightness) turn-on makes the light visible
243 if (this->has_state() && this->state_ && (color_mode & ColorCapability::BRIGHTNESS) && !this->has_brightness() &&
244 this->parent_->remote_values.get_brightness() == 0.0f) {
245 this->brightness_ = 1.0f;
247 }
248
249 // Set color brightness to 100% if currently zero and a color is set.
250 if ((this->has_red() || this->has_green() || this->has_blue()) && !this->has_color_brightness() &&
251 this->parent_->remote_values.get_color_brightness() == 0.0f) {
252 this->color_brightness_ = 1.0f;
254 }
255
256 // Capability validation
257 if (this->has_brightness() && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS))
258 this->log_and_clear_unsupported_(FLAG_HAS_BRIGHTNESS, LOG_STR("brightness"), false);
259
260 // Transition length possible check
261 if (this->has_transition_() && this->transition_length_ != 0 && !(color_mode & ColorCapability::BRIGHTNESS))
262 this->log_and_clear_unsupported_(FLAG_HAS_TRANSITION, LOG_STR("transitions"), false);
263
264 if (this->has_color_brightness() && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB))
265 this->log_and_clear_unsupported_(FLAG_HAS_COLOR_BRIGHTNESS, LOG_STR("RGB brightness"), true);
266
267 // RGB exists check
268 if (((this->has_red() && this->red_ > 0.0f) || (this->has_green() && this->green_ > 0.0f) ||
269 (this->has_blue() && this->blue_ > 0.0f)) &&
270 !(color_mode & ColorCapability::RGB)) {
271 log_color_mode_not_supported(name, LOG_STR("RGB color"));
275 }
276
277 // White value exists check
278 if (this->has_white() && this->white_ > 0.0f &&
279 !(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE))
280 this->log_and_clear_unsupported_(FLAG_HAS_WHITE, LOG_STR("white value"), true);
281
282 // Color temperature exists check
283 if (this->has_color_temperature() &&
285 this->log_and_clear_unsupported_(FLAG_HAS_COLOR_TEMPERATURE, LOG_STR("color temperature"), true);
286
287 // Cold/warm white value exists check
288 if (((this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f)) &&
289 !(color_mode & ColorCapability::COLD_WARM_WHITE)) {
290 log_color_mode_not_supported(name, LOG_STR("cold/warm white value"));
293 }
294
295 // Create color values and validate+apply ranges in one step to eliminate duplicate checks
296 auto v = this->parent_->remote_values;
297 if (this->has_color_mode())
299 if (this->has_state())
300 v.set_state(this->state_);
301
302 // FieldFlags bits 0-7 must match unit_fields_ array indices.
303 static_assert(FLAG_HAS_BRIGHTNESS == 1u << 0 && FLAG_HAS_COLOR_BRIGHTNESS == 1u << 1 && FLAG_HAS_RED == 1u << 2 &&
304 FLAG_HAS_GREEN == 1u << 3 && FLAG_HAS_BLUE == 1u << 4 && FLAG_HAS_WHITE == 1u << 5 &&
305 FLAG_HAS_COLD_WHITE == 1u << 6 && FLAG_HAS_WARM_WHITE == 1u << 7,
306 "FieldFlags bits 0-7 must match unit_fields_ indices");
307
308 // Iterate set bits only (ctz + clear-lowest) — HA can drive perform()
309 // at high frequency so the hot path is O(popcount).
310 unsigned active = this->flags_ & CLAMP_FLAGS_MASK;
311 while (active != 0) {
312 unsigned bit = __builtin_ctz(active);
313 active &= active - 1; // clear lowest set bit
314 float &value = this->unit_fields_[bit];
315 if (float_out_of_unit_range(value)) {
316 log_value_out_of_range(name, value, ValidateFieldNames::get_log_str(bit, 0), 0.0f, 1.0f);
317 value = clamp_unit_float(value);
318 }
319 v.unit_fields_[bit] = value;
320 }
321
322 // color_temperature: runtime range from traits.
323 if (this->has_color_temperature()) {
324 const float ct_min = traits.get_min_mireds();
325 const float ct_max = traits.get_max_mireds();
327 log_value_out_of_range(name, this->color_temperature_, ValidateFieldNames::get_log_str(VALIDATE_CT_INDEX, 0),
328 ct_min, ct_max);
329 this->color_temperature_ = clamp(this->color_temperature_, ct_min, ct_max);
330 }
331 v.color_temperature_ = this->color_temperature_;
332 }
333
334 v.normalize_color();
335
336 // Flash length check
337 if (this->has_flash_() && this->flash_length_ == 0) {
338 log_invalid_parameter(name, LOG_STR("flash length must be >0"));
340 }
341
342 // validate transition length/flash length/effect not used at the same time
343 bool supports_transition = color_mode & ColorCapability::BRIGHTNESS;
344
345#ifdef USE_LIGHT_RESUME_EFFECT
346 // A plain turn-on from off brings back the effect that was running when the light was turned off
347 if (this->parent_->resume_effect_ && plain_turn_on && !this->parent_->remote_values.is_on() &&
348 this->parent_->previous_effect_index_ != 0) {
351 }
352#endif // USE_LIGHT_RESUME_EFFECT
353
354 // A plain turn-on of a lit light keeps a running effect as it is.
355 // Effects' own calls don't publish, so they are not caught here.
356 if (plain_turn_on && this->get_publish_() && this->parent_->remote_values.is_on() &&
357 this->parent_->active_effect_index_ != 0) {
358 this->effect_ = this->parent_->active_effect_index_;
360 }
361
362 // If effect is already active, remove effect start. When a lit light that stays on gets no new values or flash,
363 // keep the flag and drop any transition, which has nothing to fade to; perform() then leaves the running effect
364 // undisturbed. A call that turns the light off keeps no flag, so the turn-off block below stops the effect.
365 // has_brightness() catches the brightness added above to make the turn-on visible.
366 if (this->has_effect_() && this->effect_ == this->parent_->active_effect_index_) {
367 if (sets_values || this->has_brightness() || this->has_flash_() || this->effect_ == 0 ||
368 !this->parent_->remote_values.is_on() || !v.is_on()) {
370 } else {
372 }
373 }
374
375 // validate effect index
376 if (this->has_effect_() && this->effect_ > this->parent_->effects_.size()) {
377 ESP_LOGW(TAG, "'%s': invalid effect index %" PRIu32, name, this->effect_);
379 }
380
381 if (this->has_effect_() && (this->has_transition_() || this->has_flash_())) {
382 log_invalid_parameter(name, LOG_STR("effect cannot be used with transition/flash"));
385 }
386
387 if (this->has_flash_() && this->has_transition_()) {
388 log_invalid_parameter(name, LOG_STR("flash cannot be used with transition"));
390 }
391
392 if (!this->has_transition_() && !this->has_flash_() && (!this->has_effect_() || this->effect_ == 0) &&
393 supports_transition) {
394 // nothing specified and light supports transitions, set default transition length
397 }
398
399 if (this->has_transition_() && this->transition_length_ == 0) {
400 // 0 transition is interpreted as no transition (instant change)
402 }
403
404 if (this->has_transition_() && !supports_transition)
405 this->log_and_clear_unsupported_(FLAG_HAS_TRANSITION, LOG_STR("transitions"), false);
406
407 // If not a flash and turning the light off, then disable the light
408 // Do not use light color values directly, so that effects can set 0% brightness
409 // Reason: When user turns off the light in frontend, the effect should also stop
410 bool target_state = this->has_state() ? this->state_ : v.is_on();
411 if (!this->has_flash_() && !target_state) {
412#ifdef USE_LIGHT_RESUME_EFFECT
413 // Remember what was running, including no effect, when a lit light is explicitly turned off
414 if (this->parent_->resume_effect_ && explicit_turn_off_request && this->parent_->remote_values.is_on())
416#endif // USE_LIGHT_RESUME_EFFECT
417 if (this->has_effect_()) {
418 log_invalid_parameter(name, LOG_STR("cannot start effect when turning off"));
420 } else if (this->parent_->active_effect_index_ != 0 && explicit_turn_off_request) {
421 // Auto turn off effect
422 this->effect_ = 0;
424 }
425 }
426
427 // Disable saving for flashes
428 if (this->has_flash_())
429 this->clear_flag_(FLAG_SAVE);
430
431 return v;
432}
434 // Allow CWWW modes to be set with a white value and/or color temperature.
435 // This is used in three cases in HA:
436 // - CW/WW lights, which set the "brightness" and "color_temperature"
437 // - RGBWW lights with color_interlock=true, which also sets "brightness" and
438 // "color_temperature" (without color_interlock, CW/WW are set directly)
439 // - Legacy Home Assistant (pre-colormode), which sets "white" and "color_temperature"
440
441 // Cache min/max mireds to avoid repeated calls
442 const float min_mireds = traits.get_min_mireds();
443 const float max_mireds = traits.get_max_mireds();
444
445 if (((this->has_white() && this->white_ > 0.0f) || this->has_color_temperature()) && //
447 !(this->color_mode_ & ColorCapability::WHITE) && //
449 min_mireds > 0.0f && max_mireds > 0.0f) {
450 ESP_LOGV(TAG, "'%s': setting cold/warm white channels using white/color temperature values",
451 LOG_STR_ARG(this->parent_->get_log_name()));
452 // Only compute cold_white/warm_white from color_temperature if they're not already explicitly set.
453 // This is important for state restoration, where both color_temperature and cold_white/warm_white
454 // are restored from flash - we want to preserve the saved cold_white/warm_white values.
455 if (this->has_color_temperature() && !this->has_cold_white() && !this->has_warm_white()) {
456 const float color_temp = clamp(this->color_temperature_, min_mireds, max_mireds);
457 const float range = max_mireds - min_mireds;
458 const float ww_fraction = (color_temp - min_mireds) / range;
459 const float cw_fraction = 1.0f - ww_fraction;
460 const float max_cw_ww = std::max(ww_fraction, cw_fraction);
461 this->cold_white_ = this->parent_->gamma_uncorrect_lut(cw_fraction / max_cw_ww);
462 this->warm_white_ = this->parent_->gamma_uncorrect_lut(ww_fraction / max_cw_ww);
465 }
466 if (this->has_white()) {
467 this->brightness_ = this->white_;
469 }
470 }
471}
473 auto supported_modes = traits.get_supported_color_modes();
474 int supported_count = supported_modes.size();
475
476 // Some lights don't support any color modes (e.g. monochromatic light), leave it at unknown.
477 if (supported_count == 0)
478 return ColorMode::UNKNOWN;
479
480 // In the common case of lights supporting only a single mode, use that one.
481 if (supported_count == 1)
482 return *supported_modes.begin();
483
484 // Don't change if the light is being turned off.
485 ColorMode current_mode = this->parent_->remote_values.get_color_mode();
486 if (this->has_state() && !this->state_)
487 return current_mode;
488
489 // If no color mode is specified, we try to guess the color mode. This is needed for backward compatibility to
490 // pre-colormode clients and automations, but also for the MQTT API, where HA doesn't let us know which color mode
491 // was used for some reason.
492 // Compute intersection of suitable and supported modes using bitwise AND
493 color_mode_bitmask_t intersection = this->get_suitable_color_modes_mask_() & supported_modes.get_mask();
494
495 // Don't change if the current mode is in the intersection (suitable AND supported)
496 if (ColorModeMask::mask_contains(intersection, current_mode)) {
497 ESP_LOGV(TAG, "'%s': color mode not specified; retaining %s", LOG_STR_ARG(this->parent_->get_log_name()),
498 LOG_STR_ARG(color_mode_to_human(current_mode)));
499 return current_mode;
500 }
501
502 // Use the preferred suitable mode.
503 if (intersection != 0) {
505 ESP_LOGV(TAG, "'%s': color mode not specified; using %s", LOG_STR_ARG(this->parent_->get_log_name()),
506 LOG_STR_ARG(color_mode_to_human(mode)));
507 return mode;
508 }
509
510 // There's no supported mode for this call, so warn, use the current more or a mode at random and let validation strip
511 // out whatever we don't support.
512 auto color_mode = current_mode != ColorMode::UNKNOWN ? current_mode : *supported_modes.begin();
513 ESP_LOGW(TAG, "'%s': no suitable color mode supported; defaulting to %s", LOG_STR_ARG(this->parent_->get_log_name()),
514 LOG_STR_ARG(color_mode_to_human(color_mode)));
515 return color_mode;
516}
517// PROGMEM lookup table for get_suitable_color_modes_mask_().
518// Maps 4-bit key (white | ct<<1 | cwww<<2 | rgb<<3) to color mode bitmask.
519// Packed into uint8_t by right-shifting by PACK_SHIFT since the lower bits
520// (UNKNOWN, ON_OFF, BRIGHTNESS) are never present in suitable mode masks.
521static constexpr unsigned PACK_SHIFT = ColorModeBitPolicy::to_bit(ColorMode::WHITE);
522// clang-format off
523static constexpr uint8_t SUITABLE_COLOR_MODES[] PROGMEM = {
524 // [0] none - all modes with brightness
527 ColorMode::COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
528 // [1] white only
531 // [2] ct only
534 // [3] white + ct
536 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
537 // [4] cwww only
538 static_cast<uint8_t>(ColorModeMask({ColorMode::COLD_WARM_WHITE,
539 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
540 0, // [5] white + cwww (conflicting)
541 0, // [6] ct + cwww (conflicting)
542 0, // [7] white + ct + cwww (conflicting)
543 // [8] rgb only
545 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
546 // [9] rgb + white
548 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
549 // [10] rgb + ct
551 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
552 // [11] rgb + white + ct
554 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
555 // [12] rgb + cwww
556 static_cast<uint8_t>(ColorModeMask({ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
557 0, // [13] rgb + white + cwww (conflicting)
558 0, // [14] rgb + ct + cwww (conflicting)
559 0, // [15] all (conflicting)
560};
561// clang-format on
562
564 bool has_white = this->has_white() && this->white_ > 0.0f;
565 bool has_ct = this->has_color_temperature();
566 bool has_cwww =
567 (this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f);
568 bool has_rgb = (this->has_color_brightness() && this->color_brightness_ > 0.0f) ||
569 (this->has_red() || this->has_green() || this->has_blue());
570
571 // Build key from flags: [rgb][cwww][ct][white]
572 uint8_t key = has_white | (has_ct << 1) | (has_cwww << 2) | (has_rgb << 3);
573 return static_cast<color_mode_bitmask_t>(progmem_read_byte(&SUITABLE_COLOR_MODES[key])) << PACK_SHIFT;
574}
575
576LightCall &LightCall::set_effect(const char *effect, size_t len) {
577 if (len == 4 && strncasecmp(effect, "none", 4) == 0) {
578 this->set_effect(uint32_t{0});
579 return *this;
580 }
581
582 bool found = false;
583 StringRef effect_ref(effect, len);
584 for (uint32_t i = 0; i < this->parent_->effects_.size(); i++) {
585 if (str_equals_case_insensitive(effect_ref, this->parent_->effects_[i]->get_name())) {
586 this->set_effect(i + 1);
587 found = true;
588 break;
589 }
590 }
591 if (!found) {
592 ESP_LOGW(TAG, "'%s': no such effect '%.*s'", LOG_STR_ARG(this->parent_->get_log_name()), (int) len, effect);
593 }
594 return *this;
595}
615 this->set_transition_length(transition_length);
616 return *this;
617}
620 this->set_brightness(brightness);
621 return *this;
622}
624 if (this->parent_->get_traits().supports_color_mode(color_mode))
625 this->set_color_mode(color_mode);
626 return *this;
627}
630 this->set_color_brightness(brightness);
631 return *this;
632}
635 this->set_red(red);
636 return *this;
637}
640 this->set_green(green);
641 return *this;
642}
645 this->set_blue(blue);
646 return *this;
647}
650 this->set_white(white);
651 return *this;
652}
661 this->set_cold_white(cold_white);
662 return *this;
663}
666 this->set_warm_white(warm_white);
667 return *this;
668}
669IMPLEMENT_LIGHT_CALL_SETTER(state, bool, FLAG_HAS_STATE)
670IMPLEMENT_LIGHT_CALL_SETTER(transition_length, uint32_t, FLAG_HAS_TRANSITION)
671IMPLEMENT_LIGHT_CALL_SETTER(flash_length, uint32_t, FLAG_HAS_FLASH)
672IMPLEMENT_LIGHT_CALL_SETTER(brightness, float, FLAG_HAS_BRIGHTNESS)
673IMPLEMENT_LIGHT_CALL_SETTER(color_mode, ColorMode, FLAG_HAS_COLOR_MODE)
674IMPLEMENT_LIGHT_CALL_SETTER(color_brightness, float, FLAG_HAS_COLOR_BRIGHTNESS)
675IMPLEMENT_LIGHT_CALL_SETTER(red, float, FLAG_HAS_RED)
676IMPLEMENT_LIGHT_CALL_SETTER(green, float, FLAG_HAS_GREEN)
677IMPLEMENT_LIGHT_CALL_SETTER(blue, float, FLAG_HAS_BLUE)
678IMPLEMENT_LIGHT_CALL_SETTER(white, float, FLAG_HAS_WHITE)
679IMPLEMENT_LIGHT_CALL_SETTER(color_temperature, float, FLAG_HAS_COLOR_TEMPERATURE)
680IMPLEMENT_LIGHT_CALL_SETTER(cold_white, float, FLAG_HAS_COLD_WHITE)
681IMPLEMENT_LIGHT_CALL_SETTER(warm_white, float, FLAG_HAS_WARM_WHITE)
682LightCall &LightCall::set_effect(optional<std::string> effect) {
683 if (effect.has_value())
684 this->set_effect(*effect);
685 return *this;
686}
688 this->effect_ = effect_number;
690 return *this;
691}
692LightCall &LightCall::set_effect(optional<uint32_t> effect_number) {
693 if (effect_number.has_value()) {
694 this->effect_ = effect_number.value();
695 }
696 this->set_flag_(FLAG_HAS_EFFECT, effect_number.has_value());
697 return *this;
698}
700 this->set_flag_(FLAG_PUBLISH, publish);
701 return *this;
702}
704 this->set_flag_(FLAG_SAVE, save);
705 return *this;
706}
707LightCall &LightCall::set_rgb(float red, float green, float blue) {
708 this->set_red(red);
709 this->set_green(green);
710 this->set_blue(blue);
711 return *this;
712}
713LightCall &LightCall::set_rgbw(float red, float green, float blue, float white) {
714 this->set_rgb(red, green, blue);
715 this->set_white(white);
716 return *this;
717}
718
719} // namespace esphome::light
BedjetMode mode
BedJet operating mode.
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
static constexpr ColorMode first_value_from_mask(bitmask_t mask)
constexpr bitmask_t get_mask() const
Get the raw bitmask value for optimized operations.
constexpr size_t size() const
Count the number of values in the set.
static constexpr bool mask_contains(bitmask_t mask, ColorMode value)
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
constexpr const char * c_str() const
Definition string_ref.h:78
constexpr size_type size() const
Definition string_ref.h:79
This class represents a requested change in a light state.
Definition light_call.h:23
bool has_color_mode() const
Definition light_call.h:160
bool has_color_temperature() const
Definition light_call.h:157
LightCall & set_color_mode_if_supported(ColorMode color_mode)
Set the color mode of the light, if this mode is supported.
LightCall & set_color_temperature(optional< float > color_temperature)
Set the color temperature of the light in mireds for CWWW or RGBWW lights.
LightCall & set_publish(bool publish)
Set whether this light call should trigger a publish state.
void log_and_clear_unsupported_(FieldFlags flag, const LogString *feature, bool use_color_mode_log)
LightCall & set_color_brightness(optional< float > brightness)
Set the color brightness of the light from 0.0 (no color) to 1.0 (fully on)
bool has_warm_white() const
Definition light_call.h:159
void set_flag_(FieldFlags flag, bool value=true) ESPHOME_ALWAYS_INLINE
Definition light_call.h:232
bool has_brightness() const
Definition light_call.h:151
LightCall & set_rgb(float red, float green, float blue)
Set the RGB color of the light by RGB values.
bool has_cold_white() const
Definition light_call.h:158
void transform_parameters_(const LightTraits &traits)
Some color modes also can be set using non-native parameters, transform those calls.
LightCall & set_transition_length_if_supported(uint32_t transition_length)
Set the transition length property if the light supports transitions.
bool has_color_brightness() const
Definition light_call.h:152
LightCall & set_red_if_supported(float red)
Set the red property if the light supports RGB.
LightCall & set_effect(optional< std::string > effect)
Set the effect of the light by its name.
LightCall & set_color_brightness_if_supported(float brightness)
Set the color brightness property if the light supports RGBW.
LightCall & set_white(optional< float > white)
Set the white value value of the light from 0.0 to 1.0 for RGBW[W] lights.
LightCall & set_green(optional< float > green)
Set the green RGB value of the light from 0.0 to 1.0.
LightCall & set_green_if_supported(float green)
Set the green property if the light supports RGB.
LightCall & set_warm_white(optional< float > warm_white)
Set the warm white value of the light from 0.0 to 1.0.
LightCall & set_rgbw(float red, float green, float blue, float white)
Set the RGBW color of the light by RGB values.
LightCall & set_save(bool save)
Set whether this light call should trigger a save state to recover them at startup....
LightCall & set_color_temperature_if_supported(float color_temperature)
Set the color_temperature property if the light supports color temperature.
void clear_flag_(FieldFlags flag) ESPHOME_ALWAYS_INLINE
Definition light_call.h:241
LightCall & set_blue(optional< float > blue)
Set the blue RGB value of the light from 0.0 to 1.0.
LightCall & set_cold_white(optional< float > cold_white)
Set the cold white value of the light from 0.0 to 1.0.
LightCall & set_red(optional< float > red)
Set the red RGB value of the light from 0.0 to 1.0.
ColorMode get_active_color_mode_()
Get the currently targeted, or active if none set, color mode.
static constexpr uint16_t VALUE_FLAGS_MASK
Definition light_call.h:223
LightCall & set_white_if_supported(float white)
Set the white property if the light supports RGB.
LightCall & set_cold_white_if_supported(float cold_white)
Set the cold white property if the light supports cold white output.
LightCall & set_warm_white_if_supported(float warm_white)
Set the warm white property if the light supports cold white output.
ColorMode compute_color_mode_(const LightTraits &traits)
static constexpr uint16_t CLAMP_FLAGS_MASK
Definition light_call.h:220
LightCall & set_brightness_if_supported(float brightness)
Set the brightness property if the light supports brightness.
LightColorValues validate_()
Validate all properties and return the target light color values.
LightCall & set_brightness(optional< float > brightness)
Set the target brightness of the light from 0.0 (fully off) to 1.0 (fully on)
LightCall & set_blue_if_supported(float blue)
Set the blue property if the light supports RGB.
LightCall & from_light_color_values(const LightColorValues &values)
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
LightCall & set_color_mode(optional< ColorMode > color_mode)
Set the color mode of the light.
color_mode_bitmask_t get_suitable_color_modes_mask_()
Get potential color modes bitmask for this light call.
LightCall & set_transition_length(optional< uint32_t > transition_length)
Set the transition length of this call in milliseconds.
This class represents the color state for a light object.
void set_color_mode(ColorMode color_mode)
Set the color mode of these light color values.
float get_brightness() const
Get the brightness property of these light color values. In range 0.0 to 1.0.
float get_blue() const
Get the blue property of these light color values. In range 0.0 to 1.0.
float get_white() const
Get the white property of these light color values. In range 0.0 to 1.0.
float get_color_temperature() const
Get the color temperature property of these light color values in mired.
float get_cold_white() const
Get the cold white property of these light color values. In range 0.0 to 1.0.
bool is_on() const
Get the binary true/false state of these light color values.
float get_green() const
Get the green property of these light color values. In range 0.0 to 1.0.
float get_warm_white() const
Get the warm white property of these light color values. In range 0.0 to 1.0.
ColorMode get_color_mode() const
Get the color mode of these light color values.
float get_red() const
Get the red property of these light color values. In range 0.0 to 1.0.
float get_color_brightness() const
Get the color brightness property of these light color values. In range 0.0 to 1.0.
bool resume_effect_
Whether a plain turn-on restores the effect that was active when the light was turned off.
void start_effect_(uint32_t effect_index)
Internal method to start an effect with the given index.
void stop_effect_()
Internal method to stop the current effect (if one is active).
LightColorValues remote_values
The remote color values reported to the frontend.
void save_remote_values_()
Internal method to save the current remote_values to the preferences.
void set_immediately_(const LightColorValues &target, bool set_remote_values)
Internal method to set the color values to target immediately (with no transition).
float gamma_uncorrect_lut(float value) const
Reverse gamma correction by binary-searching the forward LUT.
void publish_state()
Publish the currently active state to the frontend.
ConstVector< LightEffect * > effects_
List of effects for this light.
void start_flash_(const LightColorValues &target, uint32_t length, bool set_remote_values)
Internal method to start a flash for the specified amount of time.
uint16_t previous_effect_index_
The effect index that was active when the light was last turned off; shares the active index's word.
void start_transition_(const LightColorValues &target, uint32_t length, bool set_remote_values)
Internal method to start a transition to the target color with the given length.
uint16_t active_effect_index_
1-based index of the active effect, 0 if none; codegen caps effects at MAX_EFFECTS in effects....
uint32_t default_transition_length_
Default transition length for all transitions in ms.
std::unique_ptr< std::vector< LightTargetStateReachedListener * > > target_state_reached_listeners_
Listeners for target state reached.
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
bool supports_color_mode(ColorMode color_mode) const
ColorModeMask get_supported_color_modes() const
const LogString * message
Definition component.cpp:35
const char * key
bool state
Definition fan.h:2
Range range
Definition msa3xx.h:0
IMPLEMENT_LIGHT_CALL_SETTER(state, bool, FLAG_HAS_STATE) IMPLEMENT_LIGHT_CALL_SETTER(transition_length
constexpr StringRef EFFECT_NONE_REF
Definition light_effect.h:8
float clamp_unit_float(float x)
PROGMEM_STRING_TABLE(ColorModeHumanStrings, "Unknown", "On/Off", "Brightness", "White", "Color temperature", "Cold/warm white", "RGB", "RGBW", "RGB + color temperature", "RGB + cold/warm white")
FiniteSetMask< ColorMode, ColorModeBitPolicy > ColorModeMask
Definition color_mode.h:153
ESPHOME_LOG_TAG(TAG, "light.addressable")
constexpr ColorMode COLOR_MODE_LOOKUP[] PROGMEM
Definition color_mode.h:114
bool float_out_of_unit_range(float x)
uint16_t color_mode_bitmask_t
Definition color_mode.h:109
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:50
@ RGB_COLD_WARM_WHITE
RGB color output, and separate cold and warm white outputs.
@ UNKNOWN
No color mode configured (cannot be a supported mode, only active when light is off).
@ RGB_WHITE
RGB color output and a separate white output.
@ RGB_COLOR_TEMPERATURE
RGB color output and a separate white output with controllable color temperature.
@ RGB
RGB color output.
@ COLOR_TEMPERATURE
Controllable color temperature output.
@ WHITE
White output only (use only if the light also has another color mode such as RGB).
@ COLD_WARM_WHITE
Cold and warm white output with individually controllable brightness.
@ BRIGHTNESS
Master brightness of the light can be controlled.
@ RGB
Color can be controlled using RGB format (includes a brightness control for the color).
@ COLOR_TEMPERATURE
Color temperature can be controlled.
@ WHITE
Brightness of white channel can be controlled separately from other channels.
@ COLD_WARM_WHITE
Brightness of cold and warm white output can be controlled.
const void size_t len
Definition hal.h:68
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:210
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
STL namespace.
static constexpr unsigned to_bit(ColorMode mode)
Definition color_mode.h:139