-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStatefulSwitch.h
58 lines (44 loc) · 1.63 KB
/
StatefulSwitch.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
/*
* A wrapper around the HomeSpan outlet, used to represent a stateful switch.
*
* A stateful switch is a physical switch that is not meant to be controlled from
* HomeKit, but only read and used for automations.
*/
#ifndef STATEFUL_SWITCH_H
#define STATEFUL_SWITCH_H
#include <HomeSpan.h>
struct StatefulSwitch : Service::Outlet {
SpanCharacteristic* isOn;
SpanCharacteristic* isUsed;
const char* name;
StatefulSwitch(const char* name) : Service::Outlet() {
/* removePerms can be used to not allow value to be changed through apps. note that
* this causes HomeApp and other apps to register the device as faulty, and a restart
* of the app might be needed to remove the rendered (!) on the device icon. It still works though.
*
* Another way to do this is to override the update method and disregard the new value there (see below).
*
* The update hack is used now, but removePerms might be prefered in the future if the apps respect this properly.
*/
// isOn = (new Characteristic::On())->removePerms(PW);
// isUsed = (new Characteristic::OutletInUse())->removePerms(PW);
isOn = new Characteristic::On();
isUsed = new Characteristic::OutletInUse();
new Characteristic::Name(name);
this->name = name;
}
void turnOn() {
if (!isOn->getVal()) isOn->setVal(true);
if (!isUsed->getVal()) isUsed->setVal(true);
}
void turnOff() {
if (isOn->getVal()) isOn->setVal(false);
if (isUsed->getVal()) isUsed->setVal(false);
}
boolean update() override {
isOn->setVal(isOn->getVal());
isUsed->setVal(isUsed->getVal());
return(true);
}
};
#endif