ESPHome 2026.2.1
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
11static const char *const TAG = "light";
12
13// Helper functions to reduce code size for logging
14static void clamp_and_log_if_invalid(const char *name, float &value, const LogString *param_name, float min = 0.0f,
15 float max = 1.0f) {
16 if (value < min || value > max) {
17 ESP_LOGW(TAG, "'%s': %s value %.2f is out of range [%.1f - %.1f]", name, LOG_STR_ARG(param_name), value, min, max);
18 value = clamp(value, min, max);
19 }
20}
21
22#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
23static void log_feature_not_supported(const char *name, const LogString *feature) {
24 ESP_LOGW(TAG, "'%s': %s not supported", name, LOG_STR_ARG(feature));
25}
26
27static void log_color_mode_not_supported(const char *name, const LogString *feature) {
28 ESP_LOGW(TAG, "'%s': color mode does not support setting %s", name, LOG_STR_ARG(feature));
29}
30
31static void log_invalid_parameter(const char *name, const LogString *message) {
32 ESP_LOGW(TAG, "'%s': %s", name, LOG_STR_ARG(message));
33}
34#else
35#define log_feature_not_supported(name, feature)
36#define log_color_mode_not_supported(name, feature)
37#define log_invalid_parameter(name, message)
38#endif
39
40// Macro to reduce repetitive setter code
41#define IMPLEMENT_LIGHT_CALL_SETTER(name, type, flag) \
42 LightCall &LightCall::set_##name(optional<type>(name)) { \
43 if ((name).has_value()) { \
44 this->name##_ = (name).value(); \
45 } \
46 this->set_flag_(flag, (name).has_value()); \
47 return *this; \
48 } \
49 LightCall &LightCall::set_##name(type name) { \
50 this->name##_ = name; \
51 this->set_flag_(flag); \
52 return *this; \
53 }
54
55// Color mode human-readable strings indexed by ColorModeBitPolicy::to_bit() (0-9)
56// Index 0 is Unknown (for ColorMode::UNKNOWN), also used as fallback for out-of-range
57PROGMEM_STRING_TABLE(ColorModeHumanStrings, "Unknown", "On/Off", "Brightness", "White", "Color temperature",
58 "Cold/warm white", "RGB", "RGBW", "RGB + color temperature", "RGB + cold/warm white");
59
60static const LogString *color_mode_to_human(ColorMode color_mode) {
61 return ColorModeHumanStrings::get_log_str(ColorModeBitPolicy::to_bit(color_mode), 0);
62}
63
64// Helper to log percentage values
65#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
66static void log_percent(const LogString *param, float value) {
67 ESP_LOGD(TAG, " %s: %.0f%%", LOG_STR_ARG(param), value * 100.0f);
68}
69#else
70#define log_percent(param, value)
71#endif
72
74 const char *name = this->parent_->get_name().c_str();
75 LightColorValues v = this->validate_();
76 const bool publish = this->get_publish_();
77
78 if (publish) {
79 ESP_LOGD(TAG, "'%s' Setting:", name);
80
81 // Only print color mode when it's being changed
82 ColorMode current_color_mode = this->parent_->remote_values.get_color_mode();
83 ColorMode target_color_mode = this->has_color_mode() ? this->color_mode_ : current_color_mode;
84 if (target_color_mode != current_color_mode) {
85 ESP_LOGD(TAG, " Color mode: %s", LOG_STR_ARG(color_mode_to_human(v.get_color_mode())));
86 }
87
88 // Only print state when it's being changed
89 bool current_state = this->parent_->remote_values.is_on();
90 bool target_state = this->has_state() ? this->state_ : current_state;
91 if (target_state != current_state) {
92 ESP_LOGD(TAG, " State: %s", ONOFF(v.is_on()));
93 }
94
95 if (this->has_brightness()) {
96 log_percent(LOG_STR("Brightness"), v.get_brightness());
97 }
98
99 if (this->has_color_brightness()) {
100 log_percent(LOG_STR("Color brightness"), v.get_color_brightness());
101 }
102 if (this->has_red() || this->has_green() || this->has_blue()) {
103 ESP_LOGD(TAG, " Red: %.0f%%, Green: %.0f%%, Blue: %.0f%%", v.get_red() * 100.0f, v.get_green() * 100.0f,
104 v.get_blue() * 100.0f);
105 }
106
107 if (this->has_white()) {
108 log_percent(LOG_STR("White"), v.get_white());
109 }
110 if (this->has_color_temperature()) {
111 ESP_LOGD(TAG, " Color temperature: %.1f mireds", v.get_color_temperature());
112 }
113
114 if (this->has_cold_white() || this->has_warm_white()) {
115 ESP_LOGD(TAG, " Cold white: %.0f%%, warm white: %.0f%%", v.get_cold_white() * 100.0f,
116 v.get_warm_white() * 100.0f);
117 }
118 }
119
120 if (this->has_flash_()) {
121 // FLASH
122 if (publish) {
123 ESP_LOGD(TAG, " Flash length: %.1fs", this->flash_length_ / 1e3f);
124 }
125
126 this->parent_->start_flash_(v, this->flash_length_, publish);
127 } else if (this->has_transition_()) {
128 // TRANSITION
129 if (publish) {
130 ESP_LOGD(TAG, " Transition length: %.1fs", this->transition_length_ / 1e3f);
131 }
132
133 // Special case: Transition and effect can be set when turning off
134 if (this->has_effect_()) {
135 if (publish) {
136 ESP_LOGD(TAG, " Effect: 'None'");
137 }
138 this->parent_->stop_effect_();
139 }
140
141 this->parent_->start_transition_(v, this->transition_length_, publish);
142
143 } else if (this->has_effect_()) {
144 // EFFECT
145 StringRef effect_s;
146 if (this->effect_ == 0u) {
147 effect_s = StringRef::from_lit("None");
148 } else {
149 effect_s = this->parent_->effects_[this->effect_ - 1]->get_name();
150 }
151
152 if (publish) {
153 ESP_LOGD(TAG, " Effect: '%.*s'", (int) effect_s.size(), effect_s.c_str());
154 }
155
156 this->parent_->start_effect_(this->effect_);
157
158 // Also set light color values when starting an effect
159 // For example to turn off the light
160 this->parent_->set_immediately_(v, true);
161 } else {
162 // INSTANT CHANGE
163 this->parent_->set_immediately_(v, publish);
164 }
165
167 for (auto *listener : *this->parent_->target_state_reached_listeners_) {
168 listener->on_light_target_state_reached();
169 }
170 }
171 if (publish) {
172 this->parent_->publish_state();
173 }
174 if (this->get_save_()) {
176 }
177}
178
179void LightCall::log_and_clear_unsupported_(FieldFlags flag, const LogString *feature, bool use_color_mode_log) {
180 auto *name = this->parent_->get_name().c_str();
181 if (use_color_mode_log) {
182 log_color_mode_not_supported(name, feature);
183 } else {
184 log_feature_not_supported(name, feature);
185 }
186 this->clear_flag_(flag);
187}
188
190 auto *name = this->parent_->get_name().c_str();
191 auto traits = this->parent_->get_traits();
192
193 // Color mode check
194 if (this->has_color_mode() && !traits.supports_color_mode(this->color_mode_)) {
195 ESP_LOGW(TAG, "'%s' does not support color mode %s", name, LOG_STR_ARG(color_mode_to_human(this->color_mode_)));
197 }
198
199 // Ensure there is always a color mode set
200 if (!this->has_color_mode()) {
201 this->color_mode_ = this->compute_color_mode_();
203 }
204 auto color_mode = this->color_mode_;
205
206 // Transform calls that use non-native parameters for the current mode.
207 this->transform_parameters_();
208
209 // Business logic adjustments before validation
210 // Flag whether an explicit turn off was requested, in which case we'll also stop the effect.
211 bool explicit_turn_off_request = this->has_state() && !this->state_;
212
213 // Turn off when brightness is set to zero, and reset brightness (so that it has nonzero brightness when turned on).
214 if (this->has_brightness() && this->brightness_ == 0.0f) {
215 this->state_ = false;
217 this->brightness_ = 1.0f;
218 }
219
220 // Set color brightness to 100% if currently zero and a color is set.
221 if ((this->has_red() || this->has_green() || this->has_blue()) && !this->has_color_brightness() &&
222 this->parent_->remote_values.get_color_brightness() == 0.0f) {
223 this->color_brightness_ = 1.0f;
225 }
226
227 // Capability validation
228 if (this->has_brightness() && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS))
229 this->log_and_clear_unsupported_(FLAG_HAS_BRIGHTNESS, LOG_STR("brightness"), false);
230
231 // Transition length possible check
232 if (this->has_transition_() && this->transition_length_ != 0 && !(color_mode & ColorCapability::BRIGHTNESS))
233 this->log_and_clear_unsupported_(FLAG_HAS_TRANSITION, LOG_STR("transitions"), false);
234
235 if (this->has_color_brightness() && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB))
236 this->log_and_clear_unsupported_(FLAG_HAS_COLOR_BRIGHTNESS, LOG_STR("RGB brightness"), true);
237
238 // RGB exists check
239 if (((this->has_red() && this->red_ > 0.0f) || (this->has_green() && this->green_ > 0.0f) ||
240 (this->has_blue() && this->blue_ > 0.0f)) &&
241 !(color_mode & ColorCapability::RGB)) {
242 log_color_mode_not_supported(name, LOG_STR("RGB color"));
246 }
247
248 // White value exists check
249 if (this->has_white() && this->white_ > 0.0f &&
250 !(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE))
251 this->log_and_clear_unsupported_(FLAG_HAS_WHITE, LOG_STR("white value"), true);
252
253 // Color temperature exists check
254 if (this->has_color_temperature() &&
256 this->log_and_clear_unsupported_(FLAG_HAS_COLOR_TEMPERATURE, LOG_STR("color temperature"), true);
257
258 // Cold/warm white value exists check
259 if (((this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f)) &&
260 !(color_mode & ColorCapability::COLD_WARM_WHITE)) {
261 log_color_mode_not_supported(name, LOG_STR("cold/warm white value"));
264 }
265
266 // Create color values and validate+apply ranges in one step to eliminate duplicate checks
267 auto v = this->parent_->remote_values;
268 if (this->has_color_mode())
270 if (this->has_state())
271 v.set_state(this->state_);
272
273 // clamp_and_log_if_invalid already clamps in-place, so assign directly
274 // to avoid redundant clamp code from the setter being inlined.
275#define VALIDATE_AND_APPLY(field, name_str, ...) \
276 if (this->has_##field()) { \
277 clamp_and_log_if_invalid(name, this->field##_, LOG_STR(name_str), ##__VA_ARGS__); \
278 v.field##_ = this->field##_; \
279 }
280
281 VALIDATE_AND_APPLY(brightness, "Brightness")
282 VALIDATE_AND_APPLY(color_brightness, "Color brightness")
283 VALIDATE_AND_APPLY(red, "Red")
284 VALIDATE_AND_APPLY(green, "Green")
285 VALIDATE_AND_APPLY(blue, "Blue")
286 VALIDATE_AND_APPLY(white, "White")
287 VALIDATE_AND_APPLY(cold_white, "Cold white")
288 VALIDATE_AND_APPLY(warm_white, "Warm white")
289 VALIDATE_AND_APPLY(color_temperature, "Color temperature", traits.get_min_mireds(), traits.get_max_mireds())
290
291#undef VALIDATE_AND_APPLY
292
293 v.normalize_color();
294
295 // Flash length check
296 if (this->has_flash_() && this->flash_length_ == 0) {
297 log_invalid_parameter(name, LOG_STR("flash length must be >0"));
299 }
300
301 // validate transition length/flash length/effect not used at the same time
302 bool supports_transition = color_mode & ColorCapability::BRIGHTNESS;
303
304 // If effect is already active, remove effect start
305 if (this->has_effect_() && this->effect_ == this->parent_->active_effect_index_) {
307 }
308
309 // validate effect index
310 if (this->has_effect_() && this->effect_ > this->parent_->effects_.size()) {
311 ESP_LOGW(TAG, "'%s': invalid effect index %" PRIu32, name, this->effect_);
313 }
314
315 if (this->has_effect_() && (this->has_transition_() || this->has_flash_())) {
316 log_invalid_parameter(name, LOG_STR("effect cannot be used with transition/flash"));
319 }
320
321 if (this->has_flash_() && this->has_transition_()) {
322 log_invalid_parameter(name, LOG_STR("flash cannot be used with transition"));
324 }
325
326 if (!this->has_transition_() && !this->has_flash_() && (!this->has_effect_() || this->effect_ == 0) &&
327 supports_transition) {
328 // nothing specified and light supports transitions, set default transition length
331 }
332
333 if (this->has_transition_() && this->transition_length_ == 0) {
334 // 0 transition is interpreted as no transition (instant change)
336 }
337
338 if (this->has_transition_() && !supports_transition)
339 this->log_and_clear_unsupported_(FLAG_HAS_TRANSITION, LOG_STR("transitions"), false);
340
341 // If not a flash and turning the light off, then disable the light
342 // Do not use light color values directly, so that effects can set 0% brightness
343 // Reason: When user turns off the light in frontend, the effect should also stop
344 bool target_state = this->has_state() ? this->state_ : v.is_on();
345 if (!this->has_flash_() && !target_state) {
346 if (this->has_effect_()) {
347 log_invalid_parameter(name, LOG_STR("cannot start effect when turning off"));
349 } else if (this->parent_->active_effect_index_ != 0 && explicit_turn_off_request) {
350 // Auto turn off effect
351 this->effect_ = 0;
353 }
354 }
355
356 // Disable saving for flashes
357 if (this->has_flash_())
358 this->clear_flag_(FLAG_SAVE);
359
360 return v;
361}
363 auto traits = this->parent_->get_traits();
364
365 // Allow CWWW modes to be set with a white value and/or color temperature.
366 // This is used in three cases in HA:
367 // - CW/WW lights, which set the "brightness" and "color_temperature"
368 // - RGBWW lights with color_interlock=true, which also sets "brightness" and
369 // "color_temperature" (without color_interlock, CW/WW are set directly)
370 // - Legacy Home Assistant (pre-colormode), which sets "white" and "color_temperature"
371
372 // Cache min/max mireds to avoid repeated calls
373 const float min_mireds = traits.get_min_mireds();
374 const float max_mireds = traits.get_max_mireds();
375
376 if (((this->has_white() && this->white_ > 0.0f) || this->has_color_temperature()) && //
378 !(this->color_mode_ & ColorCapability::WHITE) && //
380 min_mireds > 0.0f && max_mireds > 0.0f) {
381 ESP_LOGD(TAG, "'%s': setting cold/warm white channels using white/color temperature values",
382 this->parent_->get_name().c_str());
383 // Only compute cold_white/warm_white from color_temperature if they're not already explicitly set.
384 // This is important for state restoration, where both color_temperature and cold_white/warm_white
385 // are restored from flash - we want to preserve the saved cold_white/warm_white values.
386 if (this->has_color_temperature() && !this->has_cold_white() && !this->has_warm_white()) {
387 const float color_temp = clamp(this->color_temperature_, min_mireds, max_mireds);
388 const float range = max_mireds - min_mireds;
389 const float ww_fraction = (color_temp - min_mireds) / range;
390 const float cw_fraction = 1.0f - ww_fraction;
391 const float max_cw_ww = std::max(ww_fraction, cw_fraction);
392 const float gamma = this->parent_->get_gamma_correct();
393 this->cold_white_ = gamma_uncorrect(cw_fraction / max_cw_ww, gamma);
394 this->warm_white_ = gamma_uncorrect(ww_fraction / max_cw_ww, gamma);
397 }
398 if (this->has_white()) {
399 this->brightness_ = this->white_;
401 }
402 }
403}
405 auto supported_modes = this->parent_->get_traits().get_supported_color_modes();
406 int supported_count = supported_modes.size();
407
408 // Some lights don't support any color modes (e.g. monochromatic light), leave it at unknown.
409 if (supported_count == 0)
410 return ColorMode::UNKNOWN;
411
412 // In the common case of lights supporting only a single mode, use that one.
413 if (supported_count == 1)
414 return *supported_modes.begin();
415
416 // Don't change if the light is being turned off.
417 ColorMode current_mode = this->parent_->remote_values.get_color_mode();
418 if (this->has_state() && !this->state_)
419 return current_mode;
420
421 // If no color mode is specified, we try to guess the color mode. This is needed for backward compatibility to
422 // pre-colormode clients and automations, but also for the MQTT API, where HA doesn't let us know which color mode
423 // was used for some reason.
424 // Compute intersection of suitable and supported modes using bitwise AND
425 color_mode_bitmask_t intersection = this->get_suitable_color_modes_mask_() & supported_modes.get_mask();
426
427 // Don't change if the current mode is in the intersection (suitable AND supported)
428 if (ColorModeMask::mask_contains(intersection, current_mode)) {
429 ESP_LOGI(TAG, "'%s': color mode not specified; retaining %s", this->parent_->get_name().c_str(),
430 LOG_STR_ARG(color_mode_to_human(current_mode)));
431 return current_mode;
432 }
433
434 // Use the preferred suitable mode.
435 if (intersection != 0) {
437 ESP_LOGI(TAG, "'%s': color mode not specified; using %s", this->parent_->get_name().c_str(),
438 LOG_STR_ARG(color_mode_to_human(mode)));
439 return mode;
440 }
441
442 // There's no supported mode for this call, so warn, use the current more or a mode at random and let validation strip
443 // out whatever we don't support.
444 auto color_mode = current_mode != ColorMode::UNKNOWN ? current_mode : *supported_modes.begin();
445 ESP_LOGW(TAG, "'%s': no suitable color mode supported; defaulting to %s", this->parent_->get_name().c_str(),
446 LOG_STR_ARG(color_mode_to_human(color_mode)));
447 return color_mode;
448}
449// PROGMEM lookup table for get_suitable_color_modes_mask_().
450// Maps 4-bit key (white | ct<<1 | cwww<<2 | rgb<<3) to color mode bitmask.
451// Packed into uint8_t by right-shifting by PACK_SHIFT since the lower bits
452// (UNKNOWN, ON_OFF, BRIGHTNESS) are never present in suitable mode masks.
453static constexpr unsigned PACK_SHIFT = ColorModeBitPolicy::to_bit(ColorMode::WHITE);
454// clang-format off
455static constexpr uint8_t SUITABLE_COLOR_MODES[] PROGMEM = {
456 // [0] none - all modes with brightness
459 ColorMode::COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
460 // [1] white only
463 // [2] ct only
466 // [3] white + ct
468 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
469 // [4] cwww only
470 static_cast<uint8_t>(ColorModeMask({ColorMode::COLD_WARM_WHITE,
471 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
472 0, // [5] white + cwww (conflicting)
473 0, // [6] ct + cwww (conflicting)
474 0, // [7] white + ct + cwww (conflicting)
475 // [8] rgb only
477 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
478 // [9] rgb + white
480 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
481 // [10] rgb + ct
483 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
484 // [11] rgb + white + ct
486 ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
487 // [12] rgb + cwww
488 static_cast<uint8_t>(ColorModeMask({ColorMode::RGB_COLD_WARM_WHITE}).get_mask() >> PACK_SHIFT),
489 0, // [13] rgb + white + cwww (conflicting)
490 0, // [14] rgb + ct + cwww (conflicting)
491 0, // [15] all (conflicting)
492};
493// clang-format on
494
496 bool has_white = this->has_white() && this->white_ > 0.0f;
497 bool has_ct = this->has_color_temperature();
498 bool has_cwww =
499 (this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f);
500 bool has_rgb = (this->has_color_brightness() && this->color_brightness_ > 0.0f) ||
501 (this->has_red() || this->has_green() || this->has_blue());
502
503 // Build key from flags: [rgb][cwww][ct][white]
504 uint8_t key = has_white | (has_ct << 1) | (has_cwww << 2) | (has_rgb << 3);
505 return static_cast<color_mode_bitmask_t>(progmem_read_byte(&SUITABLE_COLOR_MODES[key])) << PACK_SHIFT;
506}
507
508LightCall &LightCall::set_effect(const char *effect, size_t len) {
509 if (len == 4 && strncasecmp(effect, "none", 4) == 0) {
510 this->set_effect(0);
511 return *this;
512 }
513
514 bool found = false;
515 StringRef effect_ref(effect, len);
516 for (uint32_t i = 0; i < this->parent_->effects_.size(); i++) {
517 if (str_equals_case_insensitive(effect_ref, this->parent_->effects_[i]->get_name())) {
518 this->set_effect(i + 1);
519 found = true;
520 break;
521 }
522 }
523 if (!found) {
524 ESP_LOGW(TAG, "'%s': no such effect '%.*s'", this->parent_->get_name().c_str(), (int) len, effect);
525 }
526 return *this;
527}
547 this->set_transition_length(transition_length);
548 return *this;
549}
552 this->set_brightness(brightness);
553 return *this;
554}
556 if (this->parent_->get_traits().supports_color_mode(color_mode))
557 this->set_color_mode(color_mode);
558 return *this;
559}
562 this->set_color_brightness(brightness);
563 return *this;
564}
567 this->set_red(red);
568 return *this;
569}
572 this->set_green(green);
573 return *this;
574}
577 this->set_blue(blue);
578 return *this;
579}
582 this->set_white(white);
583 return *this;
584}
593 this->set_cold_white(cold_white);
594 return *this;
595}
598 this->set_warm_white(warm_white);
599 return *this;
600}
601IMPLEMENT_LIGHT_CALL_SETTER(state, bool, FLAG_HAS_STATE)
602IMPLEMENT_LIGHT_CALL_SETTER(transition_length, uint32_t, FLAG_HAS_TRANSITION)
603IMPLEMENT_LIGHT_CALL_SETTER(flash_length, uint32_t, FLAG_HAS_FLASH)
604IMPLEMENT_LIGHT_CALL_SETTER(brightness, float, FLAG_HAS_BRIGHTNESS)
605IMPLEMENT_LIGHT_CALL_SETTER(color_mode, ColorMode, FLAG_HAS_COLOR_MODE)
606IMPLEMENT_LIGHT_CALL_SETTER(color_brightness, float, FLAG_HAS_COLOR_BRIGHTNESS)
607IMPLEMENT_LIGHT_CALL_SETTER(red, float, FLAG_HAS_RED)
608IMPLEMENT_LIGHT_CALL_SETTER(green, float, FLAG_HAS_GREEN)
609IMPLEMENT_LIGHT_CALL_SETTER(blue, float, FLAG_HAS_BLUE)
610IMPLEMENT_LIGHT_CALL_SETTER(white, float, FLAG_HAS_WHITE)
611IMPLEMENT_LIGHT_CALL_SETTER(color_temperature, float, FLAG_HAS_COLOR_TEMPERATURE)
612IMPLEMENT_LIGHT_CALL_SETTER(cold_white, float, FLAG_HAS_COLD_WHITE)
613IMPLEMENT_LIGHT_CALL_SETTER(warm_white, float, FLAG_HAS_WARM_WHITE)
614LightCall &LightCall::set_effect(optional<std::string> effect) {
615 if (effect.has_value())
616 this->set_effect(*effect);
617 return *this;
618}
620 this->effect_ = effect_number;
622 return *this;
623}
625 if (effect_number.has_value()) {
626 this->effect_ = effect_number.value();
627 }
628 this->set_flag_(FLAG_HAS_EFFECT, effect_number.has_value());
629 return *this;
630}
632 this->set_flag_(FLAG_PUBLISH, publish);
633 return *this;
634}
636 this->set_flag_(FLAG_SAVE, save);
637 return *this;
638}
639LightCall &LightCall::set_rgb(float red, float green, float blue) {
640 this->set_red(red);
641 this->set_green(green);
642 this->set_blue(blue);
643 return *this;
644}
645LightCall &LightCall::set_rgbw(float red, float green, float blue, float white) {
646 this->set_rgb(red, green, blue);
647 this->set_white(white);
648 return *this;
649}
650
651} // namespace esphome::light
BedjetMode mode
BedJet operating mode.
const StringRef & get_name() const
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:26
constexpr const char * c_str() const
Definition string_ref.h:73
constexpr size_type size() const
Definition string_ref.h:74
static constexpr StringRef from_lit(const CharT(&s)[N])
Definition string_ref.h:50
This class represents a requested change in a light state.
Definition light_call.h:21
bool has_color_mode() const
Definition light_call.h:154
bool has_color_temperature() const
Definition light_call.h:151
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:153
bool has_brightness() const
Definition light_call.h:145
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:152
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:146
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.
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.
void clear_flag_(FieldFlags flag)
Definition light_call.h:231
ColorMode get_active_color_mode_()
Get the currently targeted, or active if none set, color mode.
void set_flag_(FieldFlags flag, bool value=true)
Definition light_call.h:222
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.
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.
void transform_parameters_()
Some color modes also can be set using non-native parameters, transform those calls.
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.
FixedVector< LightEffect * > effects_
List of effects for this light.
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 get_gamma_correct() const
void publish_state()
Publish the currently active state to the frontend.
uint32_t active_effect_index_
Value for storing the index of the currently active effect. 0 if no effect is active.
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.
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.
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.
bool supports_color_mode(ColorMode color_mode) const
ColorModeMask get_supported_color_modes() const
bool has_value() const
Definition optional.h:92
value_type const & value() const
Definition optional.h:94
const char * message
Definition component.cpp:38
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
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:147
uint16_t color_mode_bitmask_t
Definition color_mode.h:108
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:49
@ 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.
float gamma_uncorrect(float value, float gamma)
Reverts gamma correction of gamma to value.
Definition helpers.cpp:712
std::string size_t len
Definition helpers.h:692
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:163
uint8_t progmem_read_byte(const uint8_t *addr)
Definition core.cpp:49
static constexpr unsigned to_bit(ColorMode mode)
Definition color_mode.h:131