-
Notifications
You must be signed in to change notification settings - Fork 8
/
linp-doorbell.h
312 lines (279 loc) · 10.7 KB
/
linp-doorbell.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include "esphome.h"
#include "ArduinoQueue.h"
#define get_linp_doorbell(constructor) static_cast<LinpDoorbell *> \
(const_cast<custom_component::CustomComponentConstructor *>(&constructor)->get_component(0))
class LinpDoorbell : public Component, CustomAPIDevice {
ArduinoQueue<String> commandQueue;
ArduinoQueue<String> requests;
#ifdef LOG_BINARY_SENSOR
ArduinoQueue<int> buttonPresses;
#endif
#ifdef LOG_SENSOR
bool isChiming = false;
#endif
bool hasSetVolume = false;
public:
#ifdef LOG_BINARY_SENSOR
BinarySensor *button1_sensor = new BinarySensor("Button 1");
BinarySensor *button2_sensor = new BinarySensor("Button 2");
BinarySensor *button3_sensor = new BinarySensor("Button 3");
BinarySensor *button4_sensor = new BinarySensor("Button 4");
BinarySensor *button5_sensor = new BinarySensor("Button 5");
BinarySensor *button6_sensor = new BinarySensor("Button 6");
BinarySensor *button7_sensor = new BinarySensor("Button 7");
BinarySensor *button8_sensor = new BinarySensor("Button 8");
BinarySensor *button9_sensor = new BinarySensor("Button 9");
BinarySensor *button10_sensor = new BinarySensor("Button 10");
BinarySensor *button_sensors[10] = {
button1_sensor,
button2_sensor,
button3_sensor,
button4_sensor,
button5_sensor,
button6_sensor,
button7_sensor,
button8_sensor,
button9_sensor,
button10_sensor
};
#endif
#ifdef LOG_SENSOR
Sensor *volume_sensor = new Sensor("Volume");
Sensor *playing_sensor = new Sensor("Chime Playing");
Sensor *chime1_sensor = new Sensor("Button 1 Tune");
Sensor *chime2_sensor = new Sensor("Button 2 Tune");
Sensor *chime3_sensor = new Sensor("Button 3 Tune");
Sensor *chime4_sensor = new Sensor("Button 4 Tune");
Sensor *chime5_sensor = new Sensor("Button 5 Tune");
Sensor *chime6_sensor = new Sensor("Button 6 Tune");
Sensor *chime7_sensor = new Sensor("Button 7 Tune");
Sensor *chime8_sensor = new Sensor("Button 8 Tune");
Sensor *chime9_sensor = new Sensor("Button 9 Tune");
Sensor *chime10_sensor = new Sensor("Button 10 Tune");
Sensor *chime_sensors[10] = {
chime1_sensor,
chime2_sensor,
chime3_sensor,
chime4_sensor,
chime5_sensor,
chime6_sensor,
chime7_sensor,
chime8_sensor,
chime9_sensor,
chime10_sensor
};
#endif
#ifdef LOG_BINARY_SENSOR
LinpDoorbell() : commandQueue(255), requests(255), buttonPresses(255) { }
#else
LinpDoorbell() : commandQueue(255), requests(255) { }
#endif
float get_setup_priority() const { return setup_priority::HARDWARE; }
void setup() override {
Serial2.begin(115200);
hasSetVolume = false;
commandQueue.enqueue("down get_volume");
commandQueue.enqueue("down get_switch_list");
register_service(&LinpDoorbell::setVolume, "linp_set_volume", {"volume"});
register_service(&LinpDoorbell::playTune, "linp_play_tune", {"tune"});
register_service(&LinpDoorbell::stopTune, "linp_stop_tune");
register_service(&LinpDoorbell::learnButton, "linp_learn_button", {"tune"});
register_service(&LinpDoorbell::setTune, "linp_set_tune", {"button", "tune"});
register_service(&LinpDoorbell::forgetButton, "linp_forget_button", {"button"});
register_service(&LinpDoorbell::sendRawCommand, "linp_send_raw_command", {"command"});
}
void loop() override {
if (Serial2.available() > 0) {
String received = Serial2.readStringUntil('\r');
ESP_LOGV("linp-doorbell", "RX: %s", received.c_str());
String response = handleMessage(received);
if (response.length() > 0) {
ESP_LOGV("linp-doorbell", "TX: %s", response.c_str());
Serial2.print(response + "\r");
}
}
}
String handleMessage(String received) {
#ifdef LOG_BINARY_SENSOR
if (!buttonPresses.isEmpty()) {
// Revert button state to `false`.
int buttonIndex = buttonPresses.dequeue();
button_sensors[buttonIndex]->publish_state(false);
}
#endif
#ifdef LOG_SENSOR
if (isChiming) {
isChiming = false;
playing_sensor->publish_state(0.0);
}
#endif
if (received.equals("net")) {
return String("local");
} else if (received.startsWith("result ")) {
String value = String(received);
value.remove(0, 7);
if (!value.equals("\"ok\"")) {
if (requests.isEmpty()) {
ESP_LOGI("linp-doorbell", "Unexpected property received: %s", value.c_str());
} else {
String param = requests.dequeue();
handleParam(param, value);
}
}
} else if (received.startsWith("props ")) {
String param = String(received);
param.remove(0, 6);
String value = String(param);
int spacePos = param.indexOf(" ");
param.remove(spacePos, param.length() - spacePos);
value.remove(0, spacePos + 1);
ESP_LOGI("linp-doorbell", "Property received: %s = %s", param.c_str(), value.c_str());
} else if (received.startsWith("event ")) {
received.remove(0, 6);
handleEvent(received);
return String("ok");
} else if (received.equals("get_down")) {
if (commandQueue.isEmpty()) {
return String("down none");
} else {
String response = commandQueue.dequeue();
ESP_LOGI("linp-doorbell", "Sending command: %s", response.c_str());
if (response.startsWith("down get_")) {
// Strip the "down get_" prefix off.
String request = String(response);
request.remove(0, 9);
requests.enqueue(request);
} else if (response.startsWith("down set_music")) {
requests.enqueue(String("music"));
}
return response;
}
}
return "";
}
void handleEvent(String event) {
ESP_LOGI("linp-doorbell", "Event received: %s", event.c_str());
if (event.startsWith("switch_pressed_")) {
event.remove(0, 15);
int buttonIndex = atoi(event.c_str());
#ifdef LOG_BINARY_SENSOR
buttonPresses.enqueue(buttonIndex);
button_sensors[buttonIndex]->publish_state(true);
#endif
char buttonStr[2];
itoa(buttonIndex+1, buttonStr, 10); // Increment to get a 1-based number
fire_homeassistant_event("esphome.linp_doorbell_button_pressed", {{"button", buttonStr}});
} else if(event.startsWith("bell_ring")) {
event.remove(0, 10);
#ifdef LOG_SENSOR
playing_sensor->publish_state(parse_float(event.c_str()).value());
isChiming = true;
#endif
fire_homeassistant_event("esphome.linp_doorbell_tune_played", {{"tune", event.c_str()}});
} else if(event.startsWith("learn_success")) {
// Button learning succeded; request a fresh list (event supplies a list, but it's space-separated).
commandQueue.enqueue("down get_switch_list");
}
}
void handleParam(String param, String value) {
ESP_LOGI("linp-doorbell", "Param received: %s = %s", param.c_str(), value.c_str());
#ifdef LOG_SENSOR
if (param.equals("volume")) {
if (!hasSetVolume) {
// Volume needs to be reset on boot to put it back to the last set value.
// This also jogs the doorbell to life so that it'll actually chime on first button press.
// If we don't do this, it won't chime on first press but will on subsequent presses.
int initialVolume = atoi(value.c_str());
ESP_LOGI("linp-doorbell", "Setting initial volume: %i", initialVolume);
hasSetVolume = true;
setVolume(initialVolume);
}
volume_sensor->publish_state(parse_float(value.c_str()).value());
} else if (param.equals("switch_list")) {
// Comma-separated list of button tunes.
int offset = 0;
for(int i=0; i<10; i++) {
int commaPos = value.indexOf(',', offset);
if (commaPos == -1 && i < 9) {
// Comma not found. Value might be malformed; we won't be able to find any more tunes, so stop here.
break;
}
String tune = value.substring(offset, commaPos);
auto tuneFloat = parse_float(tune.c_str());
if (tune.equals("255")) {
tuneFloat = -1;
}
chime_sensors[i]->publish_state(tuneFloat.value());
offset = commaPos + 1;
}
}
#endif
}
void setVolume(int volume) {
if (volume < 0 || volume > 4) {
ESP_LOGI("linp-doorbell", "Ignoring invalid volume request: %i", volume);
return;
}
ESP_LOGI("linp-doorbell", "Setting volume to: %i", volume);
String command = String("down set_volume ");
command.concat(volume);
commandQueue.enqueue(command);
commandQueue.enqueue("down get_volume");
}
void playTune(int tune) {
if (tune < 1 || tune > 36) {
ESP_LOGI("linp-doorbell", "Ignoring invalid tune request: %i", tune);
return;
}
ESP_LOGI("linp-doorbell", "Playing tune: %i", tune);
String command = String("down play_specified_music ");
command.concat(tune);
commandQueue.enqueue(command);
}
void stopTune() {
ESP_LOGI("linp-doorbell", "Stopping tune");
commandQueue.enqueue(String("down stop_play"));
}
void learnButton(int tune) {
if (tune < 1 || tune > 36) {
ESP_LOGI("linp-doorbell", "Ignoring learn request - invalid tune: %i", tune);
return;
}
ESP_LOGI("linp-doorbell", "Entering learn mode with tune: %i", tune);
String command = String("down enter_specified_learn_mode ");
command.concat(tune);
commandQueue.enqueue(command);
}
void setTune(int button, int tune) {
if (button < 1 || button > 10) {
ESP_LOGI("linp-doorbell", "Ignoring set tune request - invalid button: %i", button);
return;
}
if (tune < 1 || tune > 36) {
ESP_LOGI("linp-doorbell", "Ignoring set tune request - invalid tune: %i", tune);
return;
}
ESP_LOGI("linp-doorbell", "Setting button %i to tune: %i", button, tune);
String command = String("down set_music_for_switch ");
command.concat(button-1);
command.concat(",");
command.concat(tune);
commandQueue.enqueue(command);
}
void forgetButton(int button) {
if (button < 1 || button > 10) {
ESP_LOGI("linp-doorbell", "Ignoring forget button request - invalid button: %i", button);
return;
}
ESP_LOGI("linp-doorbell", "Forgetting button %i", button);
String command = String("down delete_specified_switch ");
command.concat(button-1);
commandQueue.enqueue(command);
// Doorbell sends a "switch list" param after forgetting the button.
requests.enqueue(String("switch_list"));
}
void sendRawCommand(std::string command) {
ESP_LOGI("linp-doorbell", "Sending raw command: %s", command.c_str());
commandQueue.enqueue(String(command.c_str()));
}
};