ESPHome 2025.10.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"
3
4#ifdef USE_JSON
5
6namespace esphome {
7namespace light {
8
9// See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
10
11// Lookup table for color mode strings
12static constexpr const char *get_color_mode_json_str(ColorMode mode) {
13 switch (mode) {
15 return "onoff";
17 return "brightness";
19 return "white"; // not supported by HA in MQTT
21 return "color_temp";
23 return "cwww"; // not supported by HA
24 case ColorMode::RGB:
25 return "rgb";
27 return "rgbw";
29 return "rgbct"; // not supported by HA
31 return "rgbww";
32 default:
33 return nullptr;
34 }
35}
36
38 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
39 if (state.supports_effects()) {
40 root["effect"] = state.get_effect_name();
41 root["effect_index"] = state.get_current_effect_index();
42 root["effect_count"] = state.get_effect_count();
43 }
44
45 auto values = state.remote_values;
46 auto traits = state.get_output()->get_traits();
47
48 const auto color_mode = values.get_color_mode();
49 const char *mode_str = get_color_mode_json_str(color_mode);
50 if (mode_str != nullptr) {
51 root["color_mode"] = mode_str;
52 }
53
54 if (color_mode & ColorCapability::ON_OFF)
55 root["state"] = (values.get_state() != 0.0f) ? "ON" : "OFF";
56 if (color_mode & ColorCapability::BRIGHTNESS)
57 root["brightness"] = to_uint8_scale(values.get_brightness());
58
59 JsonObject color = root["color"].to<JsonObject>();
60 if (color_mode & ColorCapability::RGB) {
61 float color_brightness = values.get_color_brightness();
62 color["r"] = to_uint8_scale(color_brightness * values.get_red());
63 color["g"] = to_uint8_scale(color_brightness * values.get_green());
64 color["b"] = to_uint8_scale(color_brightness * values.get_blue());
65 }
66 if (color_mode & ColorCapability::WHITE) {
67 uint8_t white_val = to_uint8_scale(values.get_white());
68 color["w"] = white_val;
69 root["white_value"] = white_val; // legacy API
70 }
71 if (color_mode & ColorCapability::COLOR_TEMPERATURE) {
72 // this one isn't under the color subkey for some reason
73 root["color_temp"] = uint32_t(values.get_color_temperature());
74 }
75 if (color_mode & ColorCapability::COLD_WARM_WHITE) {
76 color["c"] = to_uint8_scale(values.get_cold_white());
77 color["w"] = to_uint8_scale(values.get_warm_white());
78 }
79}
80
82 if (root["state"].is<const char *>()) {
83 auto val = parse_on_off(root["state"]);
84 switch (val) {
85 case PARSE_ON:
86 call.set_state(true);
87 break;
88 case PARSE_OFF:
89 call.set_state(false);
90 break;
91 case PARSE_TOGGLE:
92 call.set_state(!state.remote_values.is_on());
93 break;
94 case PARSE_NONE:
95 break;
96 }
97 }
98
99 if (root["brightness"].is<uint8_t>()) {
100 call.set_brightness(float(root["brightness"]) / 255.0f);
101 }
102
103 if (root["color"].is<JsonObject>()) {
104 JsonObject color = root["color"];
105 // HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness.
106 float max_rgb = 0.0f;
107 if (color["r"].is<uint8_t>()) {
108 float r = float(color["r"]) / 255.0f;
109 max_rgb = fmaxf(max_rgb, r);
110 call.set_red(r);
111 }
112 if (color["g"].is<uint8_t>()) {
113 float g = float(color["g"]) / 255.0f;
114 max_rgb = fmaxf(max_rgb, g);
115 call.set_green(g);
116 }
117 if (color["b"].is<uint8_t>()) {
118 float b = float(color["b"]) / 255.0f;
119 max_rgb = fmaxf(max_rgb, b);
120 call.set_blue(b);
121 }
122 if (color["r"].is<uint8_t>() || color["g"].is<uint8_t>() || color["b"].is<uint8_t>()) {
123 call.set_color_brightness(max_rgb);
124 }
125
126 if (color["c"].is<uint8_t>()) {
127 call.set_cold_white(float(color["c"]) / 255.0f);
128 }
129 if (color["w"].is<uint8_t>()) {
130 // the HA scheme is ambiguous here, the same key is used for white channel in RGBW and warm
131 // white channel in RGBWW.
132 if (color["c"].is<uint8_t>()) {
133 call.set_warm_white(float(color["w"]) / 255.0f);
134 } else {
135 call.set_white(float(color["w"]) / 255.0f);
136 }
137 }
138 }
139
140 if (root["white_value"].is<uint8_t>()) { // legacy API
141 call.set_white(float(root["white_value"]) / 255.0f);
142 }
143
144 if (root["color_temp"].is<uint16_t>()) {
145 call.set_color_temperature(float(root["color_temp"]));
146 }
147}
148
151
152 if (root["flash"].is<uint32_t>()) {
153 auto length = uint32_t(float(root["flash"]) * 1000);
155 }
156
157 if (root["transition"].is<uint16_t>()) {
158 auto length = uint32_t(float(root["transition"]) * 1000);
160 }
161
162 if (root["effect"].is<const char *>()) {
163 const char *effect = root["effect"];
164 call.set_effect(effect);
165 }
166
167 if (root["effect_index"].is<uint32_t>()) {
168 uint32_t effect_index = root["effect_index"];
169 call.set_effect(effect_index);
170 }
171}
172
173} // namespace light
174} // namespace esphome
175
176#endif
BedjetMode mode
BedJet operating mode.
This class represents a requested change in a light state.
Definition light_call.h:18
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:68
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.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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:336
@ PARSE_ON
Definition helpers.h:594
@ PARSE_TOGGLE
Definition helpers.h:596
@ PARSE_OFF
Definition helpers.h:595
@ PARSE_NONE
Definition helpers.h:593
uint16_t length
Definition tt21100.cpp:0