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