ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
light_json_schema.cpp
Go to the documentation of this file.
1#include "light_json_schema.h"
2#include "light_output.h"
4
5#ifdef USE_JSON
6
7namespace esphome::light {
8
9// See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
10
11// Get JSON string for color mode using linear search (avoids large switch jump table)
12static const char *get_color_mode_json_str(ColorMode mode) {
13 // Parallel arrays: mode values and their corresponding strings
14 // Uses less RAM than a switch jump table on sparse enum values
15 static constexpr ColorMode MODES[] = {
25 };
26 static constexpr const char *STRINGS[] = {
27 "onoff", "brightness", "white", "color_temp", "cwww", "rgb", "rgbw", "rgbct", "rgbww",
28 };
29 for (size_t i = 0; i < sizeof(MODES) / sizeof(MODES[0]); i++) {
30 if (MODES[i] == mode)
31 return STRINGS[i];
32 }
33 return nullptr;
34}
35
37 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
38 if (state.supports_effects()) {
39 root[ESPHOME_F("effect")] = state.get_effect_name_ref();
40 root[ESPHOME_F("effect_index")] = state.get_current_effect_index();
41 root[ESPHOME_F("effect_count")] = state.get_effect_count();
42 }
43
44 auto values = state.remote_values;
45
46 const auto color_mode = values.get_color_mode();
47 const char *mode_str = get_color_mode_json_str(color_mode);
48 if (mode_str != nullptr) {
49 root[ESPHOME_F("color_mode")] = mode_str;
50 }
51
52 if (color_mode & ColorCapability::ON_OFF)
53 root[ESPHOME_F("state")] = (values.get_state() != 0.0f) ? "ON" : "OFF";
54 if (color_mode & ColorCapability::BRIGHTNESS)
55 root[ESPHOME_F("brightness")] = to_uint8_scale(values.get_brightness());
56
57 JsonObject color = root[ESPHOME_F("color")].to<JsonObject>();
58 if (color_mode & ColorCapability::RGB) {
59 float color_brightness = values.get_color_brightness();
60 color[ESPHOME_F("r")] = to_uint8_scale(color_brightness * values.get_red());
61 color[ESPHOME_F("g")] = to_uint8_scale(color_brightness * values.get_green());
62 color[ESPHOME_F("b")] = to_uint8_scale(color_brightness * values.get_blue());
63 }
64 if (color_mode & ColorCapability::WHITE) {
65 uint8_t white_val = to_uint8_scale(values.get_white());
66 color[ESPHOME_F("w")] = white_val;
67 root[ESPHOME_F("white_value")] = white_val; // legacy API
68 }
69 if (color_mode & ColorCapability::COLOR_TEMPERATURE) {
70 // this one isn't under the color subkey for some reason
71 root[ESPHOME_F("color_temp")] = uint32_t(values.get_color_temperature());
72 }
73 if (color_mode & ColorCapability::COLD_WARM_WHITE) {
74 color[ESPHOME_F("c")] = to_uint8_scale(values.get_cold_white());
75 color[ESPHOME_F("w")] = to_uint8_scale(values.get_warm_white());
76 }
77}
78
80 if (root[ESPHOME_F("state")].is<const char *>()) {
81 auto val = parse_on_off(root[ESPHOME_F("state")]);
82 switch (val) {
83 case PARSE_ON:
84 call.set_state(true);
85 break;
86 case PARSE_OFF:
87 call.set_state(false);
88 break;
89 case PARSE_TOGGLE:
90 call.set_state(!state.remote_values.is_on());
91 break;
92 case PARSE_NONE:
93 break;
94 }
95 }
96
97 if (root[ESPHOME_F("brightness")].is<uint8_t>()) {
98 call.set_brightness(float(root[ESPHOME_F("brightness")]) / 255.0f);
99 }
100
101 if (root[ESPHOME_F("color")].is<JsonObject>()) {
102 JsonObject color = root[ESPHOME_F("color")];
103 // HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness.
104 float max_rgb = 0.0f;
105 if (color[ESPHOME_F("r")].is<uint8_t>()) {
106 float r = float(color[ESPHOME_F("r")]) / 255.0f;
107 max_rgb = fmaxf(max_rgb, r);
108 call.set_red(r);
109 }
110 if (color[ESPHOME_F("g")].is<uint8_t>()) {
111 float g = float(color[ESPHOME_F("g")]) / 255.0f;
112 max_rgb = fmaxf(max_rgb, g);
113 call.set_green(g);
114 }
115 if (color[ESPHOME_F("b")].is<uint8_t>()) {
116 float b = float(color[ESPHOME_F("b")]) / 255.0f;
117 max_rgb = fmaxf(max_rgb, b);
118 call.set_blue(b);
119 }
120 if (color[ESPHOME_F("r")].is<uint8_t>() || color[ESPHOME_F("g")].is<uint8_t>() ||
121 color[ESPHOME_F("b")].is<uint8_t>()) {
122 call.set_color_brightness(max_rgb);
123 }
124
125 if (color[ESPHOME_F("c")].is<uint8_t>()) {
126 call.set_cold_white(float(color[ESPHOME_F("c")]) / 255.0f);
127 }
128 if (color[ESPHOME_F("w")].is<uint8_t>()) {
129 // the HA scheme is ambiguous here, the same key is used for white channel in RGBW and warm
130 // white channel in RGBWW.
131 if (color[ESPHOME_F("c")].is<uint8_t>()) {
132 call.set_warm_white(float(color[ESPHOME_F("w")]) / 255.0f);
133 } else {
134 call.set_white(float(color[ESPHOME_F("w")]) / 255.0f);
135 }
136 }
137 }
138
139 if (root[ESPHOME_F("white_value")].is<uint8_t>()) { // legacy API
140 call.set_white(float(root[ESPHOME_F("white_value")]) / 255.0f);
141 }
142
143 if (root[ESPHOME_F("color_temp")].is<uint16_t>()) {
144 call.set_color_temperature(float(root[ESPHOME_F("color_temp")]));
145 }
146}
147
150
151 if (root[ESPHOME_F("flash")].is<uint32_t>()) {
152 auto length = uint32_t(float(root[ESPHOME_F("flash")]) * 1000);
154 }
155
156 if (root[ESPHOME_F("transition")].is<uint16_t>()) {
157 auto length = uint32_t(float(root[ESPHOME_F("transition")]) * 1000);
159 }
160
161 if (root[ESPHOME_F("effect")].is<const char *>()) {
162 const char *effect = root[ESPHOME_F("effect")];
163 call.set_effect(effect);
164 }
165
166 if (root[ESPHOME_F("effect_index")].is<uint32_t>()) {
167 uint32_t effect_index = root[ESPHOME_F("effect_index")];
168 call.set_effect(effect_index);
169 }
170}
171
172} // namespace esphome::light
173
174#endif
BedjetMode mode
BedJet operating mode.
This class represents a requested change in a light state.
Definition light_call.h:21
LightCall & set_color_temperature(optional< float > color_temperature)
Set the color temperature of the light in mireds for CWWW or RGBWW lights.
LightCall & set_color_brightness(optional< float > brightness)
Set the color brightness of the light from 0.0 (no color) to 1.0 (fully on)
LightCall & set_effect(optional< std::string > effect)
Set the effect of the light by its name.
LightCall & set_white(optional< float > white)
Set the white value value of the light from 0.0 to 1.0 for RGBW[W] lights.
LightCall & set_green(optional< float > green)
Set the green RGB value of the light from 0.0 to 1.0.
LightCall & set_warm_white(optional< float > warm_white)
Set the warm white value of the light from 0.0 to 1.0.
LightCall & set_blue(optional< float > blue)
Set the blue RGB value of the light from 0.0 to 1.0.
LightCall & set_flash_length(optional< uint32_t > flash_length)
Start and set the flash length of this call in milliseconds.
LightCall & set_cold_white(optional< float > cold_white)
Set the cold white value of the light from 0.0 to 1.0.
LightCall & set_red(optional< float > red)
Set the red RGB value of the light from 0.0 to 1.0.
LightCall & set_brightness(optional< float > brightness)
Set the target brightness of the light from 0.0 (fully off) to 1.0 (fully on)
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
LightCall & set_transition_length(optional< uint32_t > transition_length)
Set the transition length of this call in milliseconds.
static void parse_color_json(LightState &state, LightCall &call, JsonObject root)
static void parse_json(LightState &state, LightCall &call, JsonObject root)
Parse the JSON state of a light to a LightCall.
static void dump_json(LightState &state, JsonObject root)
Dump the state of a light as JSON.
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition light_state.h:91
bool state
Definition fan.h:0
mopeka_std_values val[4]
FLAG_HAS_TRANSITION float
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.
@ 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.
@ ON_OFF
Light can be turned on/off.
@ BRIGHTNESS
Master brightness of the light can be controlled.
@ RGB
Color can be controlled using RGB format (includes a brightness control for the color).
@ COLOR_TEMPERATURE
Color temperature can be controlled.
@ WHITE
Brightness of white channel can be controlled separately from other channels.
@ COLD_WARM_WHITE
Brightness of cold and warm white output can be controlled.
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition helpers.cpp:392
@ PARSE_ON
Definition helpers.h:945
@ PARSE_TOGGLE
Definition helpers.h:947
@ PARSE_OFF
Definition helpers.h:946
@ PARSE_NONE
Definition helpers.h:944
uint16_t length
Definition tt21100.cpp:0