ESPHome 2026.3.0-dev
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:
10 explicit AddressableLightWrapper(light::LightState *light_state) : light_state_(light_state) {}
11
12 int32_t size() const override { return 1; }
13
14 void clear_effect_data() override { this->wrapper_state_[4] = 0; }
15
17 LightTraits traits;
18
19 // Choose which color mode to use.
20 // This is ordered by how closely each color mode matches the underlying RGBW data structure used in LightPartition.
21 ColorMode color_mode_precedence[] = {ColorMode::RGB_WHITE,
31
32 LightTraits parent_traits = this->light_state_->get_traits();
33 for (auto cm : color_mode_precedence) {
34 if (parent_traits.supports_color_mode(cm)) {
35 this->color_mode_ = cm;
36 break;
37 }
38 }
39
40 // Report a color mode that's compatible with both the partition and the underlying light
41 switch (this->color_mode_) {
46 break;
47
48 case ColorMode::RGB:
50 break;
51
57 break;
58
61 break;
62
63 default:
65 }
66
67 return traits;
68 }
69
71 // Don't overwrite state if the underlying light is turned on
72 if (this->light_state_->remote_values.is_on()) {
73 this->mark_shown_();
74 return;
75 }
76
77 float r = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[0] / 255.0f);
78 float g = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[1] / 255.0f);
79 float b = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[2] / 255.0f);
80 float w = this->light_state_->gamma_uncorrect_lut(this->wrapper_state_[3] / 255.0f);
81
82 auto call = this->light_state_->make_call();
83
84 float color_brightness = fmaxf(r, fmaxf(g, b));
85 float brightness = fmaxf(color_brightness, w);
86 if (brightness == 0.0f) {
87 call.set_state(false);
88 } else {
89 color_brightness /= brightness;
90 w /= brightness;
91
92 call.set_state(true);
93 call.set_color_mode_if_supported(this->color_mode_);
94 call.set_brightness_if_supported(brightness);
95 call.set_color_brightness_if_supported(color_brightness);
96 call.set_red_if_supported(r);
97 call.set_green_if_supported(g);
98 call.set_blue_if_supported(b);
99 call.set_white_if_supported(w);
100 call.set_warm_white_if_supported(w);
101 call.set_cold_white_if_supported(w);
102 }
103 call.set_transition_length_if_supported(0);
104 call.set_publish(false);
105 call.set_save(false);
106 call.perform();
107
108 this->mark_shown_();
109 }
110
111 protected:
112 light::ESPColorView get_view_internal(int32_t index) const override {
113 return {&this->wrapper_state_[0], &this->wrapper_state_[1], &this->wrapper_state_[2],
114 &this->wrapper_state_[3], &this->wrapper_state_[4], &this->correction_};
115 }
116
118 mutable uint8_t wrapper_state_[5]{};
120};
121
122} // namespace esphome::light
void write_state(light::LightState *state) override
light::ESPColorView get_view_internal(int32_t index) const override
AddressableLightWrapper(light::LightState *light_state)
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:93
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.
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: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.