ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
addressable_light_wrapper.h
Go to the documentation of this file.
1#pragma once
2
4#include "addressable_light.h"
5
6namespace esphome::light {
7
9 public:
11 : light_state_(light_state),
13 &this->correction_) {
14 this->accessor_ = &this->wrapper_accessor_;
15 }
16
17 int32_t size() const override { return 1; }
18
19 void clear_effect_data() override { this->wrapper_state_[4] = 0; }
20
22 LightTraits traits;
23
24 // Choose which color mode to use.
25 // This is ordered by how closely each color mode matches the underlying RGBW data structure used in LightPartition.
26 ColorMode color_mode_precedence[] = {ColorMode::RGB_WHITE,
36
37 LightTraits parent_traits = this->light_state_->get_traits();
38 for (auto cm : color_mode_precedence) {
39 if (parent_traits.supports_color_mode(cm)) {
40 this->color_mode_ = cm;
41 break;
42 }
43 }
44
45 // Report a color mode that's compatible with both the partition and the underlying light
46 switch (this->color_mode_) {
51 break;
52
53 case ColorMode::RGB:
55 break;
56
62 break;
63
66 break;
67
68 default:
70 }
71
72 return traits;
73 }
74
76 // Don't overwrite state if the underlying light is turned on
77 if (this->light_state_->remote_values.is_on()) {
78 this->mark_shown_();
79 return;
80 }
81
82 float r = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[0] / 255.0f);
83 float g = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[1] / 255.0f);
84 float b = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[2] / 255.0f);
85 float w = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[3] / 255.0f);
86
87 auto call = this->light_state_->make_call();
88
89 float color_brightness = fmaxf(r, fmaxf(g, b));
90 float brightness = fmaxf(color_brightness, w);
91 if (brightness == 0.0f) {
92 call.set_state(false);
93 } else {
94 color_brightness /= brightness;
95 w /= brightness;
96
97 call.set_state(true);
98 call.set_color_mode_if_supported(this->color_mode_);
99 call.set_brightness_if_supported(brightness);
100 call.set_color_brightness_if_supported(color_brightness);
101 call.set_red_if_supported(r);
102 call.set_green_if_supported(g);
103 call.set_blue_if_supported(b);
104 call.set_white_if_supported(w);
105 call.set_warm_white_if_supported(w);
106 call.set_cold_white_if_supported(w);
107 }
108 call.set_transition_length_if_supported(0);
109 call.set_publish(false);
110 call.set_save(false);
111 call.perform();
112
113 this->mark_shown_();
114 }
115
116 protected:
119 uint8_t wrapper_state_[5]{};
121};
122
123} // namespace esphome::light
void write_state(light::LightState *state) override
AddressableLightWrapper(light::LightState *light_state)
Color accessor backed by a buffer whose contents have color correction applied.
bool is_on() const
Get the binary true/false state of these light color values.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:68
LightColorValues remote_values
The remote color values reported to the frontend.
float gamma_uncorrect_lut(float value) const
Reverse gamma correction by binary-searching the forward LUT.
This class is used to represent the capabilities of a light.
Definition light_traits.h:9
void set_supported_color_modes(ColorModeMask supported_color_modes)
bool supports_color_mode(ColorMode color_mode) const
bool state
Definition fan.h:2
static float float b
ColorMode
Color modes are a combination of color capabilities that can be used at the same time.
Definition color_mode.h:50
@ 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.
Which byte of an addressable LED's data carries each colour.