ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
light_call.cpp
Go to the documentation of this file.
1#include <cinttypes>
2#include "light_call.h"
3#include "light_state.h"
4#include "esphome/core/log.h"
6
7namespace esphome::light {
8
9static const char *const TAG = "light";
10
11// Helper functions to reduce code size for logging
12static void clamp_and_log_if_invalid(const char *name, float &value, const LogString *param_name, float min = 0.0f,
13 float max = 1.0f) {
14 if (value < min || value > max) {
15 ESP_LOGW(TAG, "'%s': %s value %.2f is out of range [%.1f - %.1f]", name, LOG_STR_ARG(param_name), value, min, max);
16 value = clamp(value, min, max);
17 }
18}
19
20#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
21static void log_feature_not_supported(const char *name, const LogString *feature) {
22 ESP_LOGW(TAG, "'%s': %s not supported", name, LOG_STR_ARG(feature));
23}
24
25static void log_color_mode_not_supported(const char *name, const LogString *feature) {
26 ESP_LOGW(TAG, "'%s': color mode does not support setting %s", name, LOG_STR_ARG(feature));
27}
28
29static void log_invalid_parameter(const char *name, const LogString *message) {
30 ESP_LOGW(TAG, "'%s': %s", name, LOG_STR_ARG(message));
31}
32#else
33#define log_feature_not_supported(name, feature)
34#define log_color_mode_not_supported(name, feature)
35#define log_invalid_parameter(name, message)
36#endif
37
38// Macro to reduce repetitive setter code
39#define IMPLEMENT_LIGHT_CALL_SETTER(name, type, flag) \
40 LightCall &LightCall::set_##name(optional<type>(name)) { \
41 if ((name).has_value()) { \
42 this->name##_ = (name).value(); \
43 } \
44 this->set_flag_(flag, (name).has_value()); \
45 return *this; \
46 } \
47 LightCall &LightCall::set_##name(type name) { \
48 this->name##_ = name; \
49 this->set_flag_(flag); \
50 return *this; \
51 }
52
53static const LogString *color_mode_to_human(ColorMode color_mode) {
54 if (color_mode == ColorMode::ON_OFF)
55 return LOG_STR("On/Off");
56 if (color_mode == ColorMode::BRIGHTNESS)
57 return LOG_STR("Brightness");
58 if (color_mode == ColorMode::WHITE)
59 return LOG_STR("White");
60 if (color_mode == ColorMode::COLOR_TEMPERATURE)
61 return LOG_STR("Color temperature");
62 if (color_mode == ColorMode::COLD_WARM_WHITE)
63 return LOG_STR("Cold/warm white");
64 if (color_mode == ColorMode::RGB)
65 return LOG_STR("RGB");
66 if (color_mode == ColorMode::RGB_WHITE)
67 return LOG_STR("RGBW");
68 if (color_mode == ColorMode::RGB_COLD_WARM_WHITE)
69 return LOG_STR("RGB + cold/warm white");
70 if (color_mode == ColorMode::RGB_COLOR_TEMPERATURE)
71 return LOG_STR("RGB + color temperature");
72 return LOG_STR("Unknown");
73}
74
75// Helper to log percentage values
76#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
77static void log_percent(const LogString *param, float value) {
78 ESP_LOGD(TAG, " %s: %.0f%%", LOG_STR_ARG(param), value * 100.0f);
79}
80#else
81#define log_percent(param, value)
82#endif
83
85 const char *name = this->parent_->get_name().c_str();
86 LightColorValues v = this->validate_();
87 const bool publish = this->get_publish_();
88
89 if (publish) {
90 ESP_LOGD(TAG, "'%s' Setting:", name);
91
92 // Only print color mode when it's being changed
93 ColorMode current_color_mode = this->parent_->remote_values.get_color_mode();
94 ColorMode target_color_mode = this->has_color_mode() ? this->color_mode_ : current_color_mode;
95 if (target_color_mode != current_color_mode) {
96 ESP_LOGD(TAG, " Color mode: %s", LOG_STR_ARG(color_mode_to_human(v.get_color_mode())));
97 }
98
99 // Only print state when it's being changed
100 bool current_state = this->parent_->remote_values.is_on();
101 bool target_state = this->has_state() ? this->state_ : current_state;
102 if (target_state != current_state) {
103 ESP_LOGD(TAG, " State: %s", ONOFF(v.is_on()));
104 }
105
106 if (this->has_brightness()) {
107 log_percent(LOG_STR("Brightness"), v.get_brightness());
108 }
109
110 if (this->has_color_brightness()) {
111 log_percent(LOG_STR("Color brightness"), v.get_color_brightness());
112 }
113 if (this->has_red() || this->has_green() || this->has_blue()) {
114 ESP_LOGD(TAG, " Red: %.0f%%, Green: %.0f%%, Blue: %.0f%%", v.get_red() * 100.0f, v.get_green() * 100.0f,
115 v.get_blue() * 100.0f);
116 }
117
118 if (this->has_white()) {
119 log_percent(LOG_STR("White"), v.get_white());
120 }
121 if (this->has_color_temperature()) {
122 ESP_LOGD(TAG, " Color temperature: %.1f mireds", v.get_color_temperature());
123 }
124
125 if (this->has_cold_white() || this->has_warm_white()) {
126 ESP_LOGD(TAG, " Cold white: %.0f%%, warm white: %.0f%%", v.get_cold_white() * 100.0f,
127 v.get_warm_white() * 100.0f);
128 }
129 }
130
131 if (this->has_flash_()) {
132 // FLASH
133 if (publish) {
134 ESP_LOGD(TAG, " Flash length: %.1fs", this->flash_length_ / 1e3f);
135 }
136
137 this->parent_->start_flash_(v, this->flash_length_, publish);
138 } else if (this->has_transition_()) {
139 // TRANSITION
140 if (publish) {
141 ESP_LOGD(TAG, " Transition length: %.1fs", this->transition_length_ / 1e3f);
142 }
143
144 // Special case: Transition and effect can be set when turning off
145 if (this->has_effect_()) {
146 if (publish) {
147 ESP_LOGD(TAG, " Effect: 'None'");
148 }
149 this->parent_->stop_effect_();
150 }
151
152 this->parent_->start_transition_(v, this->transition_length_, publish);
153
154 } else if (this->has_effect_()) {
155 // EFFECT
156 const char *effect_s;
157 if (this->effect_ == 0u) {
158 effect_s = "None";
159 } else {
160 effect_s = this->parent_->effects_[this->effect_ - 1]->get_name();
161 }
162
163 if (publish) {
164 ESP_LOGD(TAG, " Effect: '%s'", effect_s);
165 }
166
167 this->parent_->start_effect_(this->effect_);
168
169 // Also set light color values when starting an effect
170 // For example to turn off the light
171 this->parent_->set_immediately_(v, true);
172 } else {
173 // INSTANT CHANGE
174 this->parent_->set_immediately_(v, publish);
175 }
176
178 for (auto *listener : *this->parent_->target_state_reached_listeners_) {
179 listener->on_light_target_state_reached();
180 }
181 }
182 if (publish) {
183 this->parent_->publish_state();
184 }
185 if (this->get_save_()) {
187 }
188}
189
190void LightCall::log_and_clear_unsupported_(FieldFlags flag, const LogString *feature, bool use_color_mode_log) {
191 auto *name = this->parent_->get_name().c_str();
192 if (use_color_mode_log) {
193 log_color_mode_not_supported(name, feature);
194 } else {
195 log_feature_not_supported(name, feature);
196 }
197 this->clear_flag_(flag);
198}
199
201 auto *name = this->parent_->get_name().c_str();
202 auto traits = this->parent_->get_traits();
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_();
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_();
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 // Turn off when brightness is set to zero, and reset brightness (so that it has nonzero brightness when turned on).
225 if (this->has_brightness() && this->brightness_ == 0.0f) {
226 this->state_ = false;
228 this->brightness_ = 1.0f;
229 }
230
231 // Set color brightness to 100% if currently zero and a color is set.
232 if ((this->has_red() || this->has_green() || this->has_blue()) && !this->has_color_brightness() &&
233 this->parent_->remote_values.get_color_brightness() == 0.0f) {
234 this->color_brightness_ = 1.0f;
236 }
237
238 // Capability validation
239 if (this->has_brightness() && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS))
240 this->log_and_clear_unsupported_(FLAG_HAS_BRIGHTNESS, LOG_STR("brightness"), false);
241
242 // Transition length possible check
243 if (this->has_transition_() && this->transition_length_ != 0 && !(color_mode & ColorCapability::BRIGHTNESS))
244 this->log_and_clear_unsupported_(FLAG_HAS_TRANSITION, LOG_STR("transitions"), false);
245
246 if (this->has_color_brightness() && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB))
247 this->log_and_clear_unsupported_(FLAG_HAS_COLOR_BRIGHTNESS, LOG_STR("RGB brightness"), true);
248
249 // RGB exists check
250 if (((this->has_red() && this->red_ > 0.0f) || (this->has_green() && this->green_ > 0.0f) ||
251 (this->has_blue() && this->blue_ > 0.0f)) &&
252 !(color_mode & ColorCapability::RGB)) {
253 log_color_mode_not_supported(name, LOG_STR("RGB color"));
257 }
258
259 // White value exists check
260 if (this->has_white() && this->white_ > 0.0f &&
261 !(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE))
262 this->log_and_clear_unsupported_(FLAG_HAS_WHITE, LOG_STR("white value"), true);
263
264 // Color temperature exists check
265 if (this->has_color_temperature() &&
267 this->log_and_clear_unsupported_(FLAG_HAS_COLOR_TEMPERATURE, LOG_STR("color temperature"), true);
268
269 // Cold/warm white value exists check
270 if (((this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f)) &&
271 !(color_mode & ColorCapability::COLD_WARM_WHITE)) {
272 log_color_mode_not_supported(name, LOG_STR("cold/warm white value"));
275 }
276
277 // Create color values and validate+apply ranges in one step to eliminate duplicate checks
278 auto v = this->parent_->remote_values;
279 if (this->has_color_mode())
281 if (this->has_state())
282 v.set_state(this->state_);
283
284#define VALIDATE_AND_APPLY(field, setter, name_str, ...) \
285 if (this->has_##field()) { \
286 clamp_and_log_if_invalid(name, this->field##_, LOG_STR(name_str), ##__VA_ARGS__); \
287 v.setter(this->field##_); \
288 }
289
290 VALIDATE_AND_APPLY(brightness, set_brightness, "Brightness")
291 VALIDATE_AND_APPLY(color_brightness, set_color_brightness, "Color brightness")
292 VALIDATE_AND_APPLY(red, set_red, "Red")
293 VALIDATE_AND_APPLY(green, set_green, "Green")
294 VALIDATE_AND_APPLY(blue, set_blue, "Blue")
295 VALIDATE_AND_APPLY(white, set_white, "White")
296 VALIDATE_AND_APPLY(cold_white, set_cold_white, "Cold white")
297 VALIDATE_AND_APPLY(warm_white, set_warm_white, "Warm white")
298 VALIDATE_AND_APPLY(color_temperature, set_color_temperature, "Color temperature", traits.get_min_mireds(),
299 traits.get_max_mireds())
300
301#undef VALIDATE_AND_APPLY
302
303 v.normalize_color();
304
305 // Flash length check
306 if (this->has_flash_() && this->flash_length_ == 0) {
307 log_invalid_parameter(name, LOG_STR("flash length must be >0"));
309 }
310
311 // validate transition length/flash length/effect not used at the same time
312 bool supports_transition = color_mode & ColorCapability::BRIGHTNESS;
313
314 // If effect is already active, remove effect start
315 if (this->has_effect_() && this->effect_ == this->parent_->active_effect_index_) {
317 }
318
319 // validate effect index
320 if (this->has_effect_() && this->effect_ > this->parent_->effects_.size()) {
321 ESP_LOGW(TAG, "'%s': invalid effect index %" PRIu32, name, this->effect_);
323 }
324
325 if (this->has_effect_() && (this->has_transition_() || this->has_flash_())) {
326 log_invalid_parameter(name, LOG_STR("effect cannot be used with transition/flash"));
329 }
330
331 if (this->has_flash_() && this->has_transition_()) {
332 log_invalid_parameter(name, LOG_STR("flash cannot be used with transition"));
334 }
335
336 if (!this->has_transition_() && !this->has_flash_() && (!this->has_effect_() || this->effect_ == 0) &&
337 supports_transition) {
338 // nothing specified and light supports transitions, set default transition length
341 }
342
343 if (this->has_transition_() && this->transition_length_ == 0) {
344 // 0 transition is interpreted as no transition (instant change)
346 }
347
348 if (this->has_transition_() && !supports_transition)
349 this->log_and_clear_unsupported_(FLAG_HAS_TRANSITION, LOG_STR("transitions"), false);
350
351 // If not a flash and turning the light off, then disable the light
352 // Do not use light color values directly, so that effects can set 0% brightness
353 // Reason: When user turns off the light in frontend, the effect should also stop
354 bool target_state = this->has_state() ? this->state_ : v.is_on();
355 if (!this->has_flash_() && !target_state) {
356 if (this->has_effect_()) {
357 log_invalid_parameter(name, LOG_STR("cannot start effect when turning off"));
359 } else if (this->parent_->active_effect_index_ != 0 && explicit_turn_off_request) {
360 // Auto turn off effect
361 this->effect_ = 0;
363 }
364 }
365
366 // Disable saving for flashes
367 if (this->has_flash_())
368 this->clear_flag_(FLAG_SAVE);
369
370 return v;
371}
373 auto traits = this->parent_->get_traits();
374
375 // Allow CWWW modes to be set with a white value and/or color temperature.
376 // This is used in three cases in HA:
377 // - CW/WW lights, which set the "brightness" and "color_temperature"
378 // - RGBWW lights with color_interlock=true, which also sets "brightness" and
379 // "color_temperature" (without color_interlock, CW/WW are set directly)
380 // - Legacy Home Assistant (pre-colormode), which sets "white" and "color_temperature"
381
382 // Cache min/max mireds to avoid repeated calls
383 const float min_mireds = traits.get_min_mireds();
384 const float max_mireds = traits.get_max_mireds();
385
386 if (((this->has_white() && this->white_ > 0.0f) || this->has_color_temperature()) && //
388 !(this->color_mode_ & ColorCapability::WHITE) && //
390 min_mireds > 0.0f && max_mireds > 0.0f) {
391 ESP_LOGD(TAG, "'%s': setting cold/warm white channels using white/color temperature values",
392 this->parent_->get_name().c_str());
393 if (this->has_color_temperature()) {
394 const float color_temp = clamp(this->color_temperature_, min_mireds, max_mireds);
395 const float range = max_mireds - min_mireds;
396 const float ww_fraction = (color_temp - min_mireds) / range;
397 const float cw_fraction = 1.0f - ww_fraction;
398 const float max_cw_ww = std::max(ww_fraction, cw_fraction);
399 const float gamma = this->parent_->get_gamma_correct();
400 this->cold_white_ = gamma_uncorrect(cw_fraction / max_cw_ww, gamma);
401 this->warm_white_ = gamma_uncorrect(ww_fraction / max_cw_ww, gamma);
404 }
405 if (this->has_white()) {
406 this->brightness_ = this->white_;
408 }
409 }
410}
412 auto supported_modes = this->parent_->get_traits().get_supported_color_modes();
413 int supported_count = supported_modes.size();
414
415 // Some lights don't support any color modes (e.g. monochromatic light), leave it at unknown.
416 if (supported_count == 0)
417 return ColorMode::UNKNOWN;
418
419 // In the common case of lights supporting only a single mode, use that one.
420 if (supported_count == 1)
421 return *supported_modes.begin();
422
423 // Don't change if the light is being turned off.
424 ColorMode current_mode = this->parent_->remote_values.get_color_mode();
425 if (this->has_state() && !this->state_)
426 return current_mode;
427
428 // If no color mode is specified, we try to guess the color mode. This is needed for backward compatibility to
429 // pre-colormode clients and automations, but also for the MQTT API, where HA doesn't let us know which color mode
430 // was used for some reason.
431 // Compute intersection of suitable and supported modes using bitwise AND
432 color_mode_bitmask_t intersection = this->get_suitable_color_modes_mask_() & supported_modes.get_mask();
433
434 // Don't change if the current mode is in the intersection (suitable AND supported)
435 if (ColorModeMask::mask_contains(intersection, current_mode)) {
436 ESP_LOGI(TAG, "'%s': color mode not specified; retaining %s", this->parent_->get_name().c_str(),
437 LOG_STR_ARG(color_mode_to_human(current_mode)));
438 return current_mode;
439 }
440
441 // Use the preferred suitable mode.
442 if (intersection != 0) {
444 ESP_LOGI(TAG, "'%s': color mode not specified; using %s", this->parent_->get_name().c_str(),
445 LOG_STR_ARG(color_mode_to_human(mode)));
446 return mode;
447 }
448
449 // There's no supported mode for this call, so warn, use the current more or a mode at random and let validation strip
450 // out whatever we don't support.
451 auto color_mode = current_mode != ColorMode::UNKNOWN ? current_mode : *supported_modes.begin();
452 ESP_LOGW(TAG, "'%s': no suitable color mode supported; defaulting to %s", this->parent_->get_name().c_str(),
453 LOG_STR_ARG(color_mode_to_human(color_mode)));
454 return color_mode;
455}
457 bool has_white = this->has_white() && this->white_ > 0.0f;
458 bool has_ct = this->has_color_temperature();
459 bool has_cwww =
460 (this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f);
461 bool has_rgb = (this->has_color_brightness() && this->color_brightness_ > 0.0f) ||
462 (this->has_red() || this->has_green() || this->has_blue());
463
464 // Build key from flags: [rgb][cwww][ct][white]
465#define KEY(white, ct, cwww, rgb) ((white) << 0 | (ct) << 1 | (cwww) << 2 | (rgb) << 3)
466
467 uint8_t key = KEY(has_white, has_ct, has_cwww, has_rgb);
468
469 switch (key) {
470 case KEY(true, false, false, false): // white only
473 .get_mask();
474 case KEY(false, true, false, false): // ct only
477 .get_mask();
478 case KEY(true, true, false, false): // white + ct
479 return ColorModeMask(
481 .get_mask();
482 case KEY(false, false, true, false): // cwww only
484 case KEY(false, false, false, false): // none
487 .get_mask();
488 case KEY(true, false, false, true): // rgb + white
490 .get_mask();
491 case KEY(false, true, false, true): // rgb + ct
492 case KEY(true, true, false, true): // rgb + white + ct
494 case KEY(false, false, true, true): // rgb + cwww
496 case KEY(false, false, false, true): // rgb only
499 .get_mask();
500 default:
501 return 0; // conflicting flags
502 }
503
504#undef KEY
505}
506
507LightCall &LightCall::set_effect(const char *effect, size_t len) {
508 if (len == 4 && strncasecmp(effect, "none", 4) == 0) {
509 this->set_effect(0);
510 return *this;
511 }
512
513 bool found = false;
514 for (uint32_t i = 0; i < this->parent_->effects_.size(); i++) {
515 LightEffect *e = this->parent_->effects_[i];
516 const char *name = e->get_name();
517
518 if (strncasecmp(effect, name, len) == 0 && name[len] == '\0') {
519 this->set_effect(i + 1);
520 found = true;
521 break;
522 }
523 }
524 if (!found) {
525 ESP_LOGW(TAG, "'%s': no such effect '%.*s'", this->parent_->get_name().c_str(), (int) len, effect);
526 }
527 return *this;
528}
548 this->set_transition_length(transition_length);
549 return *this;
550}
553 this->set_brightness(brightness);
554 return *this;
555}
557 if (this->parent_->get_traits().supports_color_mode(color_mode))
558 this->set_color_mode(color_mode);
559 return *this;
560}
563 this->set_color_brightness(brightness);
564 return *this;
565}
568 this->set_red(red);
569 return *this;
570}
573 this->set_green(green);
574 return *this;
575}
578 this->set_blue(blue);
579 return *this;
580}
583 this->set_white(white);
584 return *this;
585}
594 this->set_cold_white(cold_white);
595 return *this;
596}
599 this->set_warm_white(warm_white);
600 return *this;
601}
602IMPLEMENT_LIGHT_CALL_SETTER(state, bool, FLAG_HAS_STATE)
603IMPLEMENT_LIGHT_CALL_SETTER(transition_length, uint32_t, FLAG_HAS_TRANSITION)
604IMPLEMENT_LIGHT_CALL_SETTER(flash_length, uint32_t, FLAG_HAS_FLASH)
605IMPLEMENT_LIGHT_CALL_SETTER(brightness, float, FLAG_HAS_BRIGHTNESS)
606IMPLEMENT_LIGHT_CALL_SETTER(color_mode, ColorMode, FLAG_HAS_COLOR_MODE)
607IMPLEMENT_LIGHT_CALL_SETTER(color_brightness, float, FLAG_HAS_COLOR_BRIGHTNESS)
608IMPLEMENT_LIGHT_CALL_SETTER(red, float, FLAG_HAS_RED)
609IMPLEMENT_LIGHT_CALL_SETTER(green, float, FLAG_HAS_GREEN)
610IMPLEMENT_LIGHT_CALL_SETTER(blue, float, FLAG_HAS_BLUE)
611IMPLEMENT_LIGHT_CALL_SETTER(white, float, FLAG_HAS_WHITE)
612IMPLEMENT_LIGHT_CALL_SETTER(color_temperature, float, FLAG_HAS_COLOR_TEMPERATURE)
613IMPLEMENT_LIGHT_CALL_SETTER(cold_white, float, FLAG_HAS_COLD_WHITE)
614IMPLEMENT_LIGHT_CALL_SETTER(warm_white, float, FLAG_HAS_WARM_WHITE)
615LightCall &LightCall::set_effect(optional<std::string> effect) {
616 if (effect.has_value())
617 this->set_effect(*effect);
618 return *this;
619}
621 this->effect_ = effect_number;
623 return *this;
624}
626 if (effect_number.has_value()) {
627 this->effect_ = effect_number.value();
628 }
629 this->set_flag_(FLAG_HAS_EFFECT, effect_number.has_value());
630 return *this;
631}
633 this->set_flag_(FLAG_PUBLISH, publish);
634 return *this;
635}
637 this->set_flag_(FLAG_SAVE, save);
638 return *this;
639}
640LightCall &LightCall::set_rgb(float red, float green, float blue) {
641 this->set_red(red);
642 this->set_green(green);
643 this->set_blue(blue);
644 return *this;
645}
646LightCall &LightCall::set_rgbw(float red, float green, float blue, float white) {
647 this->set_rgb(red, green, blue);
648 this->set_white(white);
649 return *this;
650}
651
652} // namespace esphome::light
BedjetMode mode
BedJet operating mode.
const StringRef & get_name() const
static constexpr ColorMode first_value_from_mask(bitmask_t mask)
constexpr size_t size() const
Count the number of values in the set.
static constexpr bool mask_contains(bitmask_t mask, ColorMode value)
constexpr const char * c_str() const
Definition string_ref.h:69
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.
const char * get_name() const
Returns the name of this effect.
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:0
Range range
Definition msa3xx.h:0
IMPLEMENT_LIGHT_CALL_SETTER(state, bool, FLAG_HAS_STATE) IMPLEMENT_LIGHT_CALL_SETTER(transition_length
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
@ ON_OFF
Only on/off control.
@ RGB_COLD_WARM_WHITE
RGB color output, and separate cold and warm white outputs.
@ BRIGHTNESS
Dimmable light.
@ 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:603
std::string size_t len
Definition helpers.h:533