ESPHome  2024.11.1
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 
6 namespace esphome {
7 namespace light {
8 
9 // See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
10 
11 void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
12  if (state.supports_effects())
13  root["effect"] = state.get_effect_name();
14 
15  auto values = state.remote_values;
16  auto traits = state.get_output()->get_traits();
17 
18  switch (values.get_color_mode()) {
19  case ColorMode::UNKNOWN: // don't need to set color mode if we don't know it
20  break;
21  case ColorMode::ON_OFF:
22  root["color_mode"] = "onoff";
23  break;
25  root["color_mode"] = "brightness";
26  break;
27  case ColorMode::WHITE: // not supported by HA in MQTT
28  root["color_mode"] = "white";
29  break;
31  root["color_mode"] = "color_temp";
32  break;
33  case ColorMode::COLD_WARM_WHITE: // not supported by HA
34  root["color_mode"] = "cwww";
35  break;
36  case ColorMode::RGB:
37  root["color_mode"] = "rgb";
38  break;
40  root["color_mode"] = "rgbw";
41  break;
42  case ColorMode::RGB_COLOR_TEMPERATURE: // not supported by HA
43  root["color_mode"] = "rgbct";
44  break;
46  root["color_mode"] = "rgbww";
47  break;
48  }
49 
50  if (values.get_color_mode() & ColorCapability::ON_OFF)
51  root["state"] = (values.get_state() != 0.0f) ? "ON" : "OFF";
52  if (values.get_color_mode() & ColorCapability::BRIGHTNESS)
53  root["brightness"] = uint8_t(values.get_brightness() * 255);
54 
55  JsonObject color = root.createNestedObject("color");
56  if (values.get_color_mode() & ColorCapability::RGB) {
57  color["r"] = uint8_t(values.get_color_brightness() * values.get_red() * 255);
58  color["g"] = uint8_t(values.get_color_brightness() * values.get_green() * 255);
59  color["b"] = uint8_t(values.get_color_brightness() * values.get_blue() * 255);
60  }
61  if (values.get_color_mode() & ColorCapability::WHITE) {
62  color["w"] = uint8_t(values.get_white() * 255);
63  root["white_value"] = uint8_t(values.get_white() * 255); // legacy API
64  }
65  if (values.get_color_mode() & ColorCapability::COLOR_TEMPERATURE) {
66  // this one isn't under the color subkey for some reason
67  root["color_temp"] = uint32_t(values.get_color_temperature());
68  }
69  if (values.get_color_mode() & ColorCapability::COLD_WARM_WHITE) {
70  color["c"] = uint8_t(values.get_cold_white() * 255);
71  color["w"] = uint8_t(values.get_warm_white() * 255);
72  }
73 }
74 
76  if (root.containsKey("state")) {
77  auto val = parse_on_off(root["state"]);
78  switch (val) {
79  case PARSE_ON:
80  call.set_state(true);
81  break;
82  case PARSE_OFF:
83  call.set_state(false);
84  break;
85  case PARSE_TOGGLE:
86  call.set_state(!state.remote_values.is_on());
87  break;
88  case PARSE_NONE:
89  break;
90  }
91  }
92 
93  if (root.containsKey("brightness")) {
94  call.set_brightness(float(root["brightness"]) / 255.0f);
95  }
96 
97  if (root.containsKey("color")) {
98  JsonObject color = root["color"];
99  // HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness.
100  float max_rgb = 0.0f;
101  if (color.containsKey("r")) {
102  float r = float(color["r"]) / 255.0f;
103  max_rgb = fmaxf(max_rgb, r);
104  call.set_red(r);
105  }
106  if (color.containsKey("g")) {
107  float g = float(color["g"]) / 255.0f;
108  max_rgb = fmaxf(max_rgb, g);
109  call.set_green(g);
110  }
111  if (color.containsKey("b")) {
112  float b = float(color["b"]) / 255.0f;
113  max_rgb = fmaxf(max_rgb, b);
114  call.set_blue(b);
115  }
116  if (color.containsKey("r") || color.containsKey("g") || color.containsKey("b")) {
117  call.set_color_brightness(max_rgb);
118  }
119 
120  if (color.containsKey("c")) {
121  call.set_cold_white(float(color["c"]) / 255.0f);
122  }
123  if (color.containsKey("w")) {
124  // the HA scheme is ambiguous here, the same key is used for white channel in RGBW and warm
125  // white channel in RGBWW.
126  if (color.containsKey("c")) {
127  call.set_warm_white(float(color["w"]) / 255.0f);
128  } else {
129  call.set_white(float(color["w"]) / 255.0f);
130  }
131  }
132  }
133 
134  if (root.containsKey("white_value")) { // legacy API
135  call.set_white(float(root["white_value"]) / 255.0f);
136  }
137 
138  if (root.containsKey("color_temp")) {
139  call.set_color_temperature(float(root["color_temp"]));
140  }
141 }
142 
143 void LightJSONSchema::parse_json(LightState &state, LightCall &call, JsonObject root) {
144  LightJSONSchema::parse_color_json(state, call, root);
145 
146  if (root.containsKey("flash")) {
147  auto length = uint32_t(float(root["flash"]) * 1000);
148  call.set_flash_length(length);
149  }
150 
151  if (root.containsKey("transition")) {
152  auto length = uint32_t(float(root["transition"]) * 1000);
154  }
155 
156  if (root.containsKey("effect")) {
157  const char *effect = root["effect"];
158  call.set_effect(effect);
159  }
160 }
161 
162 } // namespace light
163 } // namespace esphome
164 
165 #endif
This class represents the communication layer between the front-end MQTT layer and the hardware outpu...
Definition: light_state.h:63
LightCall & set_color_brightness(optional< float > brightness)
Set the color brightness of the light from 0.0 (no color) to 1.0 (fully on)
Definition: light_call.cpp:592
bool is_on() const
Get the binary true/false state of these light color values.
LightCall & set_red(optional< float > red)
Set the red RGB value of the light from 0.0 to 1.0.
Definition: light_call.cpp:600
LightCall & set_color_temperature(optional< float > color_temperature)
Set the color temperature of the light in mireds for CWWW or RGBWW lights.
Definition: light_call.cpp:632
LightCall & set_cold_white(optional< float > cold_white)
Set the cold white value of the light from 0.0 to 1.0.
Definition: light_call.cpp:640
std::string get_effect_name()
Return the name of the current effect, or if no effect is active "None".
LightOutput * get_output() const
Get the light output associated with this object.
RGB color output and a separate white output.
bool supports_effects()
Return whether the light has any effects that meet the trait requirements.
Color temperature can be controlled.
mopeka_std_values val[4]
LightCall & set_transition_length(optional< uint32_t > transition_length)
Set the transition length of this call in milliseconds.
Definition: light_call.cpp:560
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:400
RGB color output, and separate cold and warm white outputs.
Brightness of cold and warm white output can be controlled.
static void parse_json(LightState &state, LightCall &call, JsonObject root)
Parse the JSON state of a light to a LightCall.
Brightness of white channel can be controlled separately from other channels.
Light can be turned on/off.
RGB color output and a separate white output with controllable color temperature. ...
This class represents a requested change in a light state.
Definition: light_call.h:14
LightCall & set_state(optional< bool > state)
Set the binary ON/OFF state of the light.
Definition: light_call.cpp:552
Master brightness of the light can be controlled.
static void dump_json(LightState &state, JsonObject root)
Dump the state of a light as JSON.
static void parse_color_json(LightState &state, LightCall &call, JsonObject root)
White output only (use only if the light also has another color mode such as RGB).
LightCall & set_warm_white(optional< float > warm_white)
Set the warm white value of the light from 0.0 to 1.0.
Definition: light_call.cpp:648
No color mode configured (cannot be a supported mode, only active when light is off).
LightCall & set_effect(optional< std::string > effect)
Set the effect of the light by its name.
Definition: light_call.cpp:656
LightCall & set_flash_length(optional< uint32_t > flash_length)
Start and set the flash length of this call in milliseconds.
Definition: light_call.cpp:568
LightCall & set_green(optional< float > green)
Set the green RGB value of the light from 0.0 to 1.0.
Definition: light_call.cpp:608
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Color can be controlled using RGB format (includes a brightness control for the color).
LightColorValues remote_values
The remote color values reported to the frontend.
Definition: light_state.h:106
virtual LightTraits get_traits()=0
Return the LightTraits of this LightOutput.
LightCall & set_white(optional< float > white)
Set the white value value of the light from 0.0 to 1.0 for RGBW[W] lights.
Definition: light_call.cpp:624
LightCall & set_brightness(optional< float > brightness)
Set the target brightness of the light from 0.0 (fully off) to 1.0 (fully on)
Definition: light_call.cpp:576
LightCall & set_blue(optional< float > blue)
Set the blue RGB value of the light from 0.0 to 1.0.
Definition: light_call.cpp:616
bool state
Definition: fan.h:34