ESPHome 2026.2.4
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 gamma = this->light_state_->get_gamma_correct();
78 float r = gamma_uncorrect(this->wrapper_state_[0] / 255.0f, gamma);
79 float g = gamma_uncorrect(this->wrapper_state_[1] / 255.0f, gamma);
80 float b = gamma_uncorrect(this->wrapper_state_[2] / 255.0f, gamma);
81 float w = gamma_uncorrect(this->wrapper_state_[3] / 255.0f, gamma);
82
83 auto call = this->light_state_->make_call();
84
85 float color_brightness = fmaxf(r, fmaxf(g, b));
86 float brightness = fmaxf(color_brightness, w);
87 if (brightness == 0.0f) {
88 call.set_state(false);
89 } else {
90 color_brightness /= brightness;
91 w /= brightness;
92
93 call.set_state(true);
94 call.set_color_mode_if_supported(this->color_mode_);
95 call.set_brightness_if_supported(brightness);
96 call.set_color_brightness_if_supported(color_brightness);
97 call.set_red_if_supported(r);
98 call.set_green_if_supported(g);
99 call.set_blue_if_supported(b);
100 call.set_white_if_supported(w);
101 call.set_warm_white_if_supported(w);
102 call.set_cold_white_if_supported(w);
103 }
104 call.set_transition_length_if_supported(0);
105 call.set_publish(false);
106 call.set_save(false);
107 call.perform();
108
109 this->mark_shown_();
110 }
111
112 protected:
113 light::ESPColorView get_view_internal(int32_t index) const override {
114 return {&this->wrapper_state_[0], &this->wrapper_state_[1], &this->wrapper_state_[2],
115 &this->wrapper_state_[3], &this->wrapper_state_[4], &this->correction_};
116 }
117
119 mutable uint8_t wrapper_state_[5]{};
121};
122
123} // 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:91
LightColorValues remote_values
The remote color values reported to the frontend.
float get_gamma_correct() const
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
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.
float gamma_uncorrect(float value, float gamma)
Reverts gamma correction of gamma to value.
Definition helpers.cpp:712