Skip to content

Commit 502f9fc

Browse files
committed
feat(zigbee): Add color modes support
1 parent d027ba1 commit 502f9fc

File tree

5 files changed

+709
-39
lines changed

5 files changed

+709
-39
lines changed

docs/en/zigbee/ep_color_dimmable_light.rst

Lines changed: 243 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ ZigbeeColorDimmableLight
55
About
66
-----
77

8-
The ``ZigbeeColorDimmableLight`` class provides an endpoint for color dimmable lights in Zigbee networks. This endpoint implements the Zigbee Home Automation (HA) standard for color lighting devices, supporting RGB color control, dimming, and scene management.
8+
The ``ZigbeeColorDimmableLight`` class provides an endpoint for color dimmable lights in Zigbee networks. This endpoint implements the Zigbee Home Automation (HA) standard for color lighting devices, supporting multiple color modes (RGB/XY, HSV, and Color Temperature), dimming, and scene management.
99

1010
**Features:**
1111
* On/off control
12-
* Brightness level control (0-100%)
13-
* RGB color control
14-
* HSV color support
12+
* Brightness level control (0-255)
13+
* RGB/XY color control
14+
* HSV (Hue/Saturation) color support
15+
* Color temperature (mireds) support
16+
* Configurable color capabilities (enable/disable color modes)
17+
* Separate callbacks for RGB, HSV, and Temperature modes
18+
* Automatic color mode switching
1519
* Scene and group support
1620
* Automatic state restoration
1721
* Integration with common endpoint features (binding, OTA, etc.)
@@ -20,6 +24,8 @@ The ``ZigbeeColorDimmableLight`` class provides an endpoint for color dimmable l
2024
**Use Cases:**
2125
* Smart RGB light bulbs
2226
* Color-changing LED strips
27+
* Tunable white light bulbs
28+
* Full-spectrum color temperature lights
2329
* Mood lighting systems
2430
* Entertainment lighting
2531
* Architectural lighting
@@ -42,20 +48,96 @@ Creates a new Zigbee color dimmable light endpoint.
4248
4349
* ``endpoint`` - Endpoint number (1-254)
4450

51+
Color Capabilities
52+
******************
53+
54+
setLightColorCapabilities
55+
^^^^^^^^^^^^^^^^^^^^^^^^^
56+
57+
Configures which color modes are supported by the light. Must be called before starting Zigbee. By default, only XY (RGB) mode is enabled.
58+
59+
.. code-block:: arduino
60+
61+
bool setLightColorCapabilities(uint16_t capabilities);
62+
63+
* ``capabilities`` - Bit flags indicating supported color modes (can be combined with bitwise OR):
64+
65+
* ``ZIGBEE_COLOR_CAPABILITY_HUE_SATURATION`` - Hue/Saturation support
66+
* ``ZIGBEE_COLOR_CAPABILITY_X_Y`` - XY (RGB) support
67+
* ``ZIGBEE_COLOR_CAPABILITY_COLOR_TEMP`` - Color temperature support
68+
* ``ZIGBEE_COLOR_CAPABILITY_ENHANCED_HUE`` - Enhanced hue support
69+
* ``ZIGBEE_COLOR_CAPABILITY_COLOR_LOOP`` - Color loop support
70+
71+
**Example:**
72+
73+
.. code-block:: arduino
74+
75+
// Enable XY and Temperature modes
76+
light.setLightColorCapabilities(
77+
ZIGBEE_COLOR_CAPABILITY_X_Y | ZIGBEE_COLOR_CAPABILITY_COLOR_TEMP
78+
);
79+
80+
// Enable all color modes
81+
light.setLightColorCapabilities(
82+
ZIGBEE_COLOR_CAPABILITY_X_Y |
83+
ZIGBEE_COLOR_CAPABILITY_HUE_SATURATION |
84+
ZIGBEE_COLOR_CAPABILITY_COLOR_TEMP
85+
);
86+
87+
This function will return ``true`` if successful, ``false`` otherwise.
88+
4589
Callback Functions
4690
******************
4791

4892
onLightChange
4993
^^^^^^^^^^^^^
5094

51-
Sets the callback function for light state changes.
95+
.. deprecated:: This method is deprecated and will be removed in a future major version. Use :ref:`onLightChangeRgb <onLightChangeRgb>` instead.
96+
97+
Sets the legacy callback function for light state changes (RGB mode).
5298

5399
.. code-block:: arduino
54100
55101
void onLightChange(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t));
56102
57103
* ``callback`` - Function pointer to the light change callback (state, red, green, blue, level)
58104

105+
.. note::
106+
This method is deprecated. Please use ``onLightChangeRgb()`` for RGB/XY mode callbacks.
107+
108+
onLightChangeRgb
109+
^^^^^^^^^^^^^^^^^
110+
111+
Sets the callback function for RGB/XY color mode changes.
112+
113+
.. code-block:: arduino
114+
115+
void onLightChangeRgb(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t));
116+
117+
* ``callback`` - Function pointer to the RGB light change callback (state, red, green, blue, level)
118+
119+
onLightChangeHsv
120+
^^^^^^^^^^^^^^^^
121+
122+
Sets the callback function for HSV (Hue/Saturation) color mode changes.
123+
124+
.. code-block:: arduino
125+
126+
void onLightChangeHsv(void (*callback)(bool, uint8_t, uint8_t, uint8_t, uint8_t));
127+
128+
* ``callback`` - Function pointer to the HSV light change callback (state, hue, saturation, value, level)
129+
130+
onLightChangeTemp
131+
^^^^^^^^^^^^^^^^^
132+
133+
Sets the callback function for color temperature mode changes.
134+
135+
.. code-block:: arduino
136+
137+
void onLightChangeTemp(void (*callback)(bool, uint8_t, uint16_t));
138+
139+
* ``callback`` - Function pointer to the temperature light change callback (state, level, temperature_mireds)
140+
59141
Control Methods
60142
***************
61143

@@ -81,14 +163,14 @@ Sets the light brightness level.
81163
82164
bool setLightLevel(uint8_t level);
83165
84-
* ``level`` - Brightness level (0-100, where 0 is off, 100 is full brightness)
166+
* ``level`` - Brightness level (0-255, where 0 is off, 255 is full brightness)
85167

86168
This function will return ``true`` if successful, ``false`` otherwise.
87169

88170
setLightColor (RGB)
89171
^^^^^^^^^^^^^^^^^^^
90172

91-
Sets the light color using RGB values.
173+
Sets the light color using RGB values. Requires ``ZIGBEE_COLOR_CAPABILITY_X_Y`` capability to be enabled.
92174

93175
.. code-block:: arduino
94176
@@ -100,37 +182,85 @@ Sets the light color using RGB values.
100182
* ``blue`` - Blue component (0-255)
101183
* ``rgb_color`` - RGB color structure
102184

103-
This function will return ``true`` if successful, ``false`` otherwise.
185+
This function will return ``true`` if successful, ``false`` otherwise. Returns ``false`` if XY capability is not enabled.
104186

105187
setLightColor (HSV)
106188
^^^^^^^^^^^^^^^^^^^
107189

108-
Sets the light color using HSV values.
190+
Sets the light color using HSV values. Requires ``ZIGBEE_COLOR_CAPABILITY_HUE_SATURATION`` capability to be enabled.
109191

110192
.. code-block:: arduino
111193
112194
bool setLightColor(espHsvColor_t hsv_color);
113195
114196
* ``hsv_color`` - HSV color structure
115197

198+
This function will return ``true`` if successful, ``false`` otherwise. Returns ``false`` if HSV capability is not enabled.
199+
200+
setLightColorTemperature
201+
^^^^^^^^^^^^^^^^^^^^^^^^
202+
203+
Sets the light color temperature in mireds. Requires ``ZIGBEE_COLOR_CAPABILITY_COLOR_TEMP`` capability to be enabled.
204+
205+
.. code-block:: arduino
206+
207+
bool setLightColorTemperature(uint16_t color_temperature);
208+
209+
* ``color_temperature`` - Color temperature in mireds (inverse of Kelvin: mireds = 1000000 / Kelvin)
210+
211+
**Example:**
212+
213+
.. code-block:: arduino
214+
215+
// Set to 4000K (cool white)
216+
uint16_t mireds = 1000000 / 4000; // = 250 mireds
217+
light.setLightColorTemperature(mireds);
218+
219+
// Set to 2700K (warm white)
220+
mireds = 1000000 / 2700; // = 370 mireds
221+
light.setLightColorTemperature(mireds);
222+
223+
This function will return ``true`` if successful, ``false`` otherwise. Returns ``false`` if color temperature capability is not enabled.
224+
225+
setLightColorTemperatureRange
226+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
227+
228+
Sets the minimum and maximum color temperature range supported by the hardware.
229+
230+
.. code-block:: arduino
231+
232+
bool setLightColorTemperatureRange(uint16_t min_temp, uint16_t max_temp);
233+
234+
* ``min_temp`` - Minimum color temperature in mireds
235+
* ``max_temp`` - Maximum color temperature in mireds
236+
237+
**Example:**
238+
239+
.. code-block:: arduino
240+
241+
// Set range for 2000K (warm) to 6500K (cool)
242+
uint16_t min_mireds = 1000000 / 6500; // = 154 mireds
243+
uint16_t max_mireds = 1000000 / 2000; // = 500 mireds
244+
light.setLightColorTemperatureRange(min_mireds, max_mireds);
245+
116246
This function will return ``true`` if successful, ``false`` otherwise.
117247

118248
setLight
119249
^^^^^^^^
120250

121-
Sets all light parameters at once.
251+
Sets all light parameters at once (RGB mode). Requires ``ZIGBEE_COLOR_CAPABILITY_X_Y`` capability to be enabled.
122252

123253
.. code-block:: arduino
124254
125255
bool setLight(bool state, uint8_t level, uint8_t red, uint8_t green, uint8_t blue);
126256
127257
* ``state`` - Light state (true/false)
128-
* ``level`` - Brightness level (0-100)
258+
* ``level`` - Brightness level (0-255)
129259
* ``red`` - Red component (0-255)
130260
* ``green`` - Green component (0-255)
131261
* ``blue`` - Blue component (0-255)
132262

133-
This function will return ``true`` if successful, ``false`` otherwise.
263+
This function will return ``true`` if successful, ``false`` otherwise. Returns ``false`` if XY capability is not enabled.
134264

135265
State Retrieval Methods
136266
***********************
@@ -155,7 +285,7 @@ Gets the current brightness level.
155285
156286
uint8_t getLightLevel();
157287
158-
This function will return current brightness level (0-100).
288+
This function will return current brightness level (0-255).
159289

160290
getLightColor
161291
^^^^^^^^^^^^^
@@ -201,18 +331,117 @@ Gets the current blue component.
201331
202332
This function will return current blue component (0-255).
203333

334+
getLightColorTemperature
335+
^^^^^^^^^^^^^^^^^^^^^^^^^
336+
337+
Gets the current color temperature.
338+
339+
.. code-block:: arduino
340+
341+
uint16_t getLightColorTemperature();
342+
343+
This function will return current color temperature in mireds.
344+
345+
getLightColorMode
346+
^^^^^^^^^^^^^^^^^
347+
348+
Gets the current active color mode.
349+
350+
.. code-block:: arduino
351+
352+
uint8_t getLightColorMode();
353+
354+
This function will return current color mode:
355+
* ``ZIGBEE_COLOR_MODE_CURRENT_X_Y`` (0x01) - XY/RGB mode
356+
* ``ZIGBEE_COLOR_MODE_HUE_SATURATION`` (0x00) - HSV mode
357+
* ``ZIGBEE_COLOR_MODE_TEMPERATURE`` (0x02) - Temperature mode
358+
359+
getLightColorHue
360+
^^^^^^^^^^^^^^^^
361+
362+
Gets the current hue value (HSV mode).
363+
364+
.. code-block:: arduino
365+
366+
uint8_t getLightColorHue();
367+
368+
This function will return current hue value (0-254).
369+
370+
getLightColorSaturation
371+
^^^^^^^^^^^^^^^^^^^^^^^
372+
373+
Gets the current saturation value (HSV mode).
374+
375+
.. code-block:: arduino
376+
377+
uint8_t getLightColorSaturation();
378+
379+
This function will return current saturation value (0-254).
380+
381+
getLightColorCapabilities
382+
^^^^^^^^^^^^^^^^^^^^^^^^^
383+
384+
Gets the currently configured color capabilities.
385+
386+
.. code-block:: arduino
387+
388+
uint16_t getLightColorCapabilities();
389+
390+
This function will return the current color capabilities bit flags.
391+
204392
Utility Methods
205393
***************
206394

207395
restoreLight
208396
^^^^^^^^^^^^
209397

210-
Restores the light to its last known state.
398+
Restores the light to its last known state. Uses the appropriate callback based on the current color mode.
211399

212400
.. code-block:: arduino
213401
214402
void restoreLight();
215403
404+
Color Modes and Automatic Behavior
405+
**********************************
406+
407+
The ``ZigbeeColorDimmableLight`` class supports three color modes:
408+
409+
* **XY/RGB Mode** (``ZIGBEE_COLOR_MODE_CURRENT_X_Y``) - Uses X/Y coordinates for color representation, internally converted to RGB
410+
* **HSV Mode** (``ZIGBEE_COLOR_MODE_HUE_SATURATION``) - Uses Hue and Saturation values directly, without RGB conversion
411+
* **Temperature Mode** (``ZIGBEE_COLOR_MODE_TEMPERATURE``) - Uses color temperature in mireds
412+
413+
**Automatic Mode Switching:**
414+
415+
The color mode is automatically updated based on which attributes are set:
416+
417+
* Setting RGB colors via ``setLight()`` or ``setLightColor()`` (RGB) → switches to XY mode
418+
* Setting HSV colors via ``setLightColor()`` (HSV) → switches to HSV mode
419+
* Setting temperature via ``setLightColorTemperature()`` → switches to Temperature mode
420+
* Receiving Zigbee commands for XY/HSV/TEMP attributes → automatically switches to the corresponding mode
421+
422+
**Capability Validation:**
423+
424+
All set methods validate that the required capability is enabled before allowing the operation:
425+
426+
* RGB/XY methods require ``ZIGBEE_COLOR_CAPABILITY_X_Y``
427+
* HSV methods require ``ZIGBEE_COLOR_CAPABILITY_HUE_SATURATION``
428+
* Temperature methods require ``ZIGBEE_COLOR_CAPABILITY_COLOR_TEMP``
429+
430+
If a capability is not enabled, the method will return ``false`` and log an error.
431+
432+
**Callback Selection:**
433+
434+
The appropriate callback is automatically called based on the current color mode:
435+
436+
* RGB/XY mode → ``onLightChangeRgb()`` callback
437+
* HSV mode → ``onLightChangeHsv()`` callback
438+
* Temperature mode → ``onLightChangeTemp()`` callback
439+
440+
When level or state changes occur, the callback for the current color mode is used automatically.
441+
442+
.. note::
443+
The legacy ``onLightChange()`` callback is deprecated and will be removed in a future major version. Always use the mode-specific callbacks (``onLightChangeRgb()``, ``onLightChangeHsv()``, or ``onLightChangeTemp()``).
444+
216445
Example
217446
-------
218447

0 commit comments

Comments
 (0)