Skip to content

Commit 408e6c1

Browse files
committed
added light effects example
1 parent 612971a commit 408e6c1

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

examples/light/light.ino

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,21 @@ HAMqtt mqtt(client, device);
1111

1212
// HALight::BrightnessFeature enables support for setting brightness of the light.
1313
// HALight::ColorTemperatureFeature enables support for setting color temperature of the light.
14-
// Both features are optional and you can remove them if they're not needed.
14+
// HALight::EffectsFeature enables support for setting effect of the light.
15+
// All features are optional, and you can remove them if they're not needed.
1516
// "prettyLight" is unique ID of the light. You should define your own ID.
16-
HALight light("prettyLight", HALight::BrightnessFeature | HALight::ColorTemperatureFeature | HALight::RGBFeature);
17+
HALight light("prettyLight", HALight::BrightnessFeature |
18+
HALight::ColorTemperatureFeature |
19+
HALight::RGBFeature |
20+
HALight::EffectsFeature);
21+
22+
const char* const lightEffects[] = {
23+
"Fire",
24+
"Rainbow",
25+
"Polar light",
26+
"Rain",
27+
"Smoke"
28+
};
1729

1830
void onStateCommand(bool state, HALight* sender) {
1931
Serial.print("State: ");
@@ -47,6 +59,15 @@ void onRGBColorCommand(HALight::RGBColor color, HALight* sender) {
4759
sender->setRGBColor(color); // report color back to the Home Assistant
4860
}
4961

62+
void onEffectCommand(uint8_t index, HALight* sender) {
63+
Serial.print("Effect index: ");
64+
Serial.println(index);
65+
Serial.print("Effect name: ");
66+
Serial.println(lightEffects[index]);
67+
68+
sender->setEffect(index); // report effect back to the Home Assistant
69+
}
70+
5071
void setup() {
5172
Serial.begin(9600);
5273

@@ -74,11 +95,15 @@ void setup() {
7495
// light.setMinMireds(50);
7596
// light.setMaxMireds(200);
7697

98+
// Light effects (mandatory if HALight::EffectsFeature is set, optional otherwise)
99+
light.setEffects(lightEffects, 5);
100+
77101
// handle light states
78102
light.onStateCommand(onStateCommand);
79103
light.onBrightnessCommand(onBrightnessCommand); // optional
80104
light.onColorTemperatureCommand(onColorTemperatureCommand); // optional
81105
light.onRGBColorCommand(onRGBColorCommand); // optional
106+
light.onEffectCommand(onEffectCommand); // optional
82107

83108
mqtt.begin(BROKER_ADDR);
84109
}

0 commit comments

Comments
 (0)