forked from dawidchyrzynski/arduino-home-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.ino
56 lines (43 loc) · 1.74 KB
/
lock.ino
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
#include <Ethernet.h>
#include <ArduinoHA.h>
#define BROKER_ADDR IPAddress(192,168,0,17)
byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A};
EthernetClient client;
HADevice device(mac, sizeof(mac));
HAMqtt mqtt(client, device);
// "myLock" is unique ID of the lock. You should define your own ID.
HALock lock("myLock");
void onLockCommand(HALock::LockCommand cmd, HALock* sender) {
if (cmd == HALock::CommandLock) {
Serial.println("Command: Lock");
sender->setState(HALock::StateLocked); // report state back to the HA
} else if (cmd == HALock::CommandUnlock) {
Serial.println("Command: Unlock");
sender->setState(HALock::StateUnlocked); // report state back to the HA
} else if (cmd == HALock::CommandOpen) {
if (sender->getCurrentState() != HALock::StateUnlocked) {
return; // optionally you can verify if the lock is unlocked before opening
}
Serial.println("Command: Open");
}
}
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
lock.onCommand(onLockCommand);
lock.setName("My door lock"); // optional
lock.setIcon("mdi:home"); // optional
// Optionally you can set retain flag for the HA commands
// lock.setRetain(true);
// Optionally you can enable optimistic mode for the HALock.
// In this mode you won't need to report state back to the HA when commands are executed.
// lock.setOptimistic(true);
mqtt.begin(BROKER_ADDR);
}
void loop() {
Ethernet.maintain();
mqtt.loop();
// You can also change the state at runtime as shown below.
// This kind of logic can be used if you want to control your lock using a button connected to the device.
// lock.setState(HALock::StateLocked); // use any state you want
}