6
6
7
7
#ifndef EX_ARDUINOHA_LIGHT
8
8
9
+ class HASerializerArray ;
10
+
9
11
#define HALIGHT_STATE_CALLBACK (name ) void (*name)(bool state, HALight* sender)
10
12
#define HALIGHT_BRIGHTNESS_CALLBACK (name ) void (*name)(uint8_t brightness, HALight* sender)
11
13
#define HALIGHT_COLOR_TEMP_CALLBACK (name ) void (*name)(uint16_t temperature, HALight* sender)
12
14
#define HALIGHT_RGB_COLOR_CALLBACK (name ) void (*name)(HALight::RGBColor color, HALight* sender)
15
+ #define HALIGHT_EFFECT_CALLBACK (name ) void (*name)(uint8_t index, HALight* sender)
13
16
14
17
/* *
15
18
* HALight allows adding a controllable light in the Home Assistant panel.
16
- * The library supports only the state, brightness, color temperature and RGB color.
19
+ * The library supports only the state, brightness, color temperature, RGB color and effects .
17
20
* If you need more features please open a new GitHub issue.
18
21
*
19
22
* @note
@@ -29,7 +32,8 @@ class HALight : public HABaseDeviceType
29
32
DefaultFeatures = 0 ,
30
33
BrightnessFeature = 1 ,
31
34
ColorTemperatureFeature = 2 ,
32
- RGBFeature = 4
35
+ RGBFeature = 4 ,
36
+ EffectsFeature = 8
33
37
};
34
38
35
39
struct RGBColor {
@@ -77,6 +81,7 @@ class HALight : public HABaseDeviceType
77
81
* `HALight::BrightnessFeature | HALight::ColorTemperatureFeature`
78
82
*/
79
83
HALight (const char * uniqueId, const uint8_t features = DefaultFeatures);
84
+ ~HALight ();
80
85
81
86
/* *
82
87
* Changes state of the light and publishes MQTT message.
@@ -122,6 +127,33 @@ class HALight : public HABaseDeviceType
122
127
*/
123
128
bool setRGBColor (const RGBColor& color, const bool force = false );
124
129
130
+ /* *
131
+ * Sets the list of available effects that will be listed.
132
+ * For example:
133
+ * `
134
+ * const char* const lightEffects[] = {"Fire","Rainbow","Snowflales","Rain","Smoke"};
135
+ * light.setEffects(lightEffects, 5);
136
+ * `
137
+ *
138
+ *
139
+ * @param effects The list of effects i.e. array of strings.
140
+ * @param size The size of the effects list i.e. total number of effects.
141
+ * @note The effects list can be set only once.
142
+ */
143
+ void setEffects (const char * const effects[], const uint8_t size);
144
+
145
+ /* *
146
+ * Changes the effect of the light and publishes MQTT message.
147
+ * Effect represents the index of the effect that was set using setEffects method.
148
+ * Please note that if a new value is the same as previous one,
149
+ * the MQTT message won't be published.
150
+ *
151
+ * @param effect The new effect index of the light.
152
+ * @param force Forces to update the value without comparing it to a previous known value.
153
+ * @return Returns `true` if the effect is set successfully.
154
+ */
155
+ bool setEffect (const uint8_t effect, const bool force = false );
156
+
125
157
/* *
126
158
* Alias for `setState(true)`.
127
159
*/
@@ -202,6 +234,24 @@ class HALight : public HABaseDeviceType
202
234
inline const RGBColor& getCurrentRGBColor () const
203
235
{ return _currentRGBColor; }
204
236
237
+ /* *
238
+ * Sets the current effect of the light without pushing the value to Home Assistant.
239
+ * This method may be useful if you want to change the effect before the connection
240
+ * with the MQTT broker is acquired.
241
+ *
242
+ * @param effect The new effect.
243
+ */
244
+ inline void setCurrentEffect (const uint8_t effect)
245
+ { _currentEffect = effect; }
246
+
247
+ /* *
248
+ * Returns the last known effect of the light.
249
+ * Effect represents the index of the effect that was set using setEffects method.
250
+ * By default the effect is set to `0`.
251
+ */
252
+ inline uint8_t getCurrentEffect () const
253
+ { return _currentEffect; }
254
+
205
255
/* *
206
256
* Sets icon of the light.
207
257
* Any icon from MaterialDesignIcons.com (for example: `mdi:home`).
@@ -297,6 +347,21 @@ class HALight : public HABaseDeviceType
297
347
inline void onRGBColorCommand (HALIGHT_RGB_COLOR_CALLBACK(callback))
298
348
{ _rgbColorCallback = callback; }
299
349
350
+ /* *
351
+ * Registers callback that will be called each time the effect command from HA is received.
352
+ * Please note that it's not possible to register multiple callbacks for the same light.
353
+ *
354
+ * @param callback
355
+ * @note In non-optimistic mode, the effect must be reported back to HA using the HALight::setEffect method.
356
+ */
357
+ inline void onEffectCommand (HALIGHT_EFFECT_CALLBACK(callback))
358
+ { _effectCallback = callback; }
359
+
360
+ #ifdef ARDUINOHA_TEST
361
+ inline HASerializerArray* getEffects () const
362
+ { return _effects; }
363
+ #endif
364
+
300
365
protected:
301
366
virtual void buildSerializer () override ;
302
367
virtual void onMqttConnected () override ;
@@ -339,6 +404,14 @@ class HALight : public HABaseDeviceType
339
404
*/
340
405
bool publishRGBColor (const RGBColor& color);
341
406
407
+ /* *
408
+ * Publishes the MQTT message with the given effect.
409
+ *
410
+ * @param effect The effect to publish.
411
+ * @returns Returns `true` if the MQTT message has been published successfully.
412
+ */
413
+ bool publishEffect (const uint8_t effect);
414
+
342
415
/* *
343
416
* Parses the given state command and executes the callback with proper value.
344
417
*
@@ -371,6 +444,14 @@ class HALight : public HABaseDeviceType
371
444
*/
372
445
void handleRGBCommand (const uint8_t * cmd, const uint16_t length);
373
446
447
+ /* *
448
+ * Parses the given effect command and executes the callback with proper value.
449
+ *
450
+ * @param cmd The data of the command.
451
+ * @param length Length of the command.
452
+ */
453
+ void handleEffectCommand (const uint8_t * cmd, const uint16_t length);
454
+
374
455
// / Features enabled for the light.
375
456
const uint8_t _features;
376
457
@@ -404,6 +485,12 @@ class HALight : public HABaseDeviceType
404
485
// / The current RBB color. By default the value is not set.
405
486
RGBColor _currentRGBColor;
406
487
488
+ // / Array of effects for the serializer.
489
+ HASerializerArray* _effects;
490
+
491
+ // / The current effect (the current effect's index). By default it's `0`.
492
+ uint8_t _currentEffect;
493
+
407
494
// / The callback that will be called when the state command is received from the HA.
408
495
HALIGHT_STATE_CALLBACK (_stateCallback);
409
496
@@ -415,6 +502,9 @@ class HALight : public HABaseDeviceType
415
502
416
503
// / The callback that will be called when the RGB command is received from the HA.
417
504
HALIGHT_RGB_COLOR_CALLBACK (_rgbColorCallback);
505
+
506
+ // / The callback that will be called when the effect is received from the HA.
507
+ HALIGHT_EFFECT_CALLBACK (_effectCallback);
418
508
};
419
509
420
510
#endif
0 commit comments