ESPHome 2025.12.1
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 this->wrapper_state_ = new uint8_t[5]; // NOLINT(cppcoreguidelines-owning-memory)
12 }
13
14 int32_t size() const override { return 1; }
15
16 void clear_effect_data() override { this->wrapper_state_[4] = 0; }
17
19 LightTraits traits;
20
21 // Choose which color mode to use.
22 // This is ordered by how closely each color mode matches the underlying RGBW data structure used in LightPartition.
23 ColorMode color_mode_precedence[] = {ColorMode::RGB_WHITE,
33
34 LightTraits parent_traits = this->light_state_->get_traits();
35 for (auto cm : color_mode_precedence) {
36 if (parent_traits.supports_color_mode(cm)) {
37 this->color_mode_ = cm;
38 break;
39 }
40 }
41
42 // Report a color mode that's compatible with both the partition and the underlying light
43 switch (this->color_mode_) {
48 break;
49
50 case ColorMode::RGB:
52 break;
53
59 break;
60
63 break;
64
65 default:
67 }
68
69 return traits;
70 }
71
73 // Don't overwrite state if the underlying light is turned on
74 if (this->light_state_->remote_values.is_on()) {
75 this->mark_shown_();
76 return;
77 }
78
79 float gamma = this->light_state_->get_gamma_correct();
80 float r = gamma_uncorrect(this->wrapper_state_[0] / 255.0f, gamma);
81 float g = gamma_uncorrect(this->wrapper_state_[1] / 255.0f, gamma);
82 float b = gamma_uncorrect(this->wrapper_state_[2] / 255.0f, gamma);
83 float w = gamma_uncorrect(this->wrapper_state_[3] / 255.0f, gamma);
84
85 auto call = this->light_state_->make_call();
86
87 float color_brightness = fmaxf(r, fmaxf(g, b));
88 float brightness = fmaxf(color_brightness, w);
89 if (brightness == 0.0f) {
90 call.set_state(false);
91 } else {
92 color_brightness /= brightness;
93 w /= brightness;
94
95 call.set_state(true);
96 call.set_color_mode_if_supported(this->color_mode_);
97 call.set_brightness_if_supported(brightness);
98 call.set_color_brightness_if_supported(color_brightness);
99 call.set_red_if_supported(r);
100 call.set_green_if_supported(g);
101 call.set_blue_if_supported(b);
102 call.set_white_if_supported(w);
103 call.set_warm_white_if_supported(w);
104 call.set_cold_white_if_supported(w);
105 }
106 call.set_transition_length_if_supported(0);
107 call.set_publish(false);
108 call.set_save(false);
109 call.perform();
110
111 this->mark_shown_();
112 }
113
114 protected:
115 light::ESPColorView get_view_internal(int32_t index) const override {
116 return {&this->wrapper_state_[0], &this->wrapper_state_[1], &this->wrapper_state_[2],
117 &this->wrapper_state_[3], &this->wrapper_state_[4], &this->correction_};
118 }
119
123};
124
125} // 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:0
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:562