-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWiFiMQTT_HA_Light.h
150 lines (118 loc) · 3.19 KB
/
WiFiMQTT_HA_Light.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef _WIFIMQTT_HA_LIGHT_H_
#define _WIFIMQTT_HA_LIGHT_H_
#include <WiFiMQTT_HA.h>
#include "FastLEDLight.h"
typedef void (*EventCommandPower)(void);
typedef void (*EventCommandBrightness)(uint8_t);
typedef void (*EventCommandEffect)(const char*);
class WiFiMQTT_HA_Light : public WiFiMQTT_HA
{
public:
WiFiMQTT_HA_Light(const FastLEDLight& ledLight)
: WiFiMQTT_HA(WLAN_AP_SSID, WLAN_AP_PASS, WLAN_HOSTNAME, DEVICE_UNIQUE_ID, AVAIL_STATUS_TOPIC),
ledLight(ledLight)
{
}
void setEventON(EventCommandPower onCallback)
{
commandON = onCallback;
}
void setEventOFF(EventCommandPower offCallback)
{
commandOFF = offCallback;
}
void setEventBrightness(EventCommandBrightness brightnessCallback)
{
commandBrightness = brightnessCallback;
}
void setEventEffect(EventCommandEffect effectCallback)
{
commandEffect = effectCallback;
}
virtual void publishState()
{
DynamicJsonDocument doc(128);
doc["state"] = ledLight.isON() ? "ON" : "OFF";
doc["brightness"] = ledLight.getBrightness();
doc["effect"] = ledLight.getEffectName();
publishJson(STATE_TOPIC, doc);
}
protected:
void discover() override
{
DynamicJsonDocument doc(1024);
doc["platform"] = "mqtt";
doc["name"] = "LED";
doc["unique_id"] = DEVICE_UNIQUE_ID;
doc["schema"] = "json";
doc["availability"][0]["topic"] = AVAIL_STATUS_TOPIC;
doc["command_topic"] = COMMAND_TOPIC;
doc["state_topic"] = STATE_TOPIC;
doc["brightness"] = true;
doc["effect"] = true;
for (uint8_t x = 0; x < ledLight.getEffectsCount(); x++)
{
doc["effect_list"][x] = ledLight.getEffectsList()[x];
}
JsonObject device = doc.createNestedObject("device");
device["identifiers"] = DEVICE_UNIQUE_ID;
device["manufacturer"] = DEVICE_manufacturer;
device["model"] = DEVICE_model;
device["name"] = DEVICE_name;
device["sw_version"] = DEVICE_sw_version;
publishJson(DISCOVERY_TOPIC, doc, true);
}
void subscribe() override
{
mqtt.subscribe(COMMAND_TOPIC);
}
void receiveMessage(char* topic, uint8_t* payload, unsigned int length) override
{
WiFiMQTT_HA::receiveMessage(topic, payload, length);
DynamicJsonDocument doc(1024);
DeserializationError err = deserializeJson(doc, payload);
if (err)
{
SerialDebug::log(LOG_LEVEL::ERROR, String(F("deserializeJson() failed with code ")) + err.f_str());
return;
}
processTopic(topic, doc);
}
virtual void processTopic(char* topic, DynamicJsonDocument& payload)
{
processPayload(payload);
publishState();
}
void processPayload(DynamicJsonDocument& payload)
{
if (payload.containsKey("state"))
{
String state = payload["state"];
if (state.equals("OFF"))
{
if (commandOFF != nullptr)
commandOFF();
}
else
{
if (commandON != nullptr)
commandON();
}
}
if (payload.containsKey("brightness") && commandBrightness != nullptr)
{
commandBrightness(payload["brightness"]);
}
if (payload.containsKey("effect") && commandEffect != nullptr)
{
commandEffect(payload["effect"]);
}
}
protected:
const FastLEDLight& ledLight;
EventCommandPower commandON = nullptr;
EventCommandPower commandOFF = nullptr;
EventCommandBrightness commandBrightness = nullptr;
EventCommandEffect commandEffect = nullptr;
};
#endif