generated from homebridge/homebridge-plugin-template
/
myStromSwitchAccessory.ts
192 lines (167 loc) · 6.31 KB
/
myStromSwitchAccessory.ts
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
import { CharacteristicEventTypes } from 'homebridge';
import type {
Service,
PlatformAccessory,
CharacteristicValue,
CharacteristicSetCallback,
CharacteristicGetCallback,
} from 'homebridge';
import { DingzDaHomebridgePlatform } from './platform';
import { MyStromDeviceInfo, MyStromSwitchReport } from './lib/myStromTypes';
import { PlatformEvent } from './lib/platformEventBus';
import { DingzDaBaseAccessory } from './lib/dingzDaBaseAccessory';
/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
export class MyStromSwitchAccessory extends DingzDaBaseAccessory {
private outletService: Service;
private temperatureService: Service | undefined = undefined;
// Eventually replaced by:
private switchOn = false;
private mystromDeviceInfo: MyStromDeviceInfo;
private outletState = {
relay: false,
temperature: 0,
power: 0,
} as MyStromSwitchReport;
constructor(
private readonly _platform: DingzDaHomebridgePlatform,
private readonly _accessory: PlatformAccessory,
) {
super(_platform, _accessory);
// Set Base URL
this.mystromDeviceInfo = this.device.hwInfo as MyStromDeviceInfo;
this.log.debug(
'Setting informationService Characteristics ->',
this.device.model,
);
this.accessory
.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(
this.platform.Characteristic.Manufacturer,
'MyStrom AG',
)
.setCharacteristic(
this.platform.Characteristic.AppMatchingIdentifier,
'ch.mystrom.iOSApp',
)
.setCharacteristic(
this.platform.Characteristic.Model,
this.device.model as string,
)
.setCharacteristic(
this.platform.Characteristic.FirmwareRevision,
this.mystromDeviceInfo.version ?? 'N/A',
)
.setCharacteristic(
this.platform.Characteristic.HardwareRevision,
this.mystromDeviceInfo ? 'EU/CH v2' : 'CH v1',
)
.setCharacteristic(
this.platform.Characteristic.SerialNumber,
this.device.mac,
);
this.outletService =
this.accessory.getService(this.platform.Service.Outlet) ??
this.accessory.addService(this.platform.Service.Outlet);
this.outletService.setCharacteristic(
this.platform.Characteristic.Name,
this.device.name ?? `${this.accessory.context.device.model} Outlet`,
);
// register handlers for the On/Off Characteristic
this.outletService
.getCharacteristic(this.platform.Characteristic.On)
.on(CharacteristicEventTypes.SET, this.setOn.bind(this)) // SET - bind to the `setOn` method below
.on(CharacteristicEventTypes.GET, this.getOn.bind(this)); // GET - bind to the `getOn` method below
this.outletService
.getCharacteristic(this.platform.Characteristic.OutletInUse)
// .on(CharacteristicEventTypes.SET, this.setOutletInUse.bind(this)) // SET - bind to the `setOn` method below
.on(CharacteristicEventTypes.GET, this.getOutletInUse.bind(this)); // GET - bind to the `getOn` method below
if (this.device.hwInfo?.type !== undefined) {
// Switch has a temperature sensor, make it available here
// create a new Temperature Sensor service
this.temperatureService =
this.accessory.getService(this.platform.Service.TemperatureSensor) ??
this.accessory.addService(this.platform.Service.TemperatureSensor);
this.temperatureService.setCharacteristic(
this.platform.Characteristic.Name,
'Temperature',
);
// create handlers for required characteristics
this.temperatureService
.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.on(CharacteristicEventTypes.GET, this.getTemperature.bind(this));
}
// Subscribe to the REQUEST_STATE_UPDATE event
this.platform.eb.on(
PlatformEvent.REQUEST_STATE_UPDATE,
this.getDeviceStateUpdate.bind(this),
);
}
identify(): void {
this.log.debug('Identify! -> Who am I? I am', this.accessory.displayName);
}
// Get updated device info and update the corresponding values
private getDeviceStateUpdate() {
this.getDeviceReport()
.then((report) => {
// push the new value to HomeKit
this.outletState = report;
this.outletService
.getCharacteristic(this.platform.Characteristic.On)
.updateValue(this.outletState.relay);
this.outletService
.getCharacteristic(this.platform.Characteristic.OutletInUse)
.updateValue(this.outletState.power > 0);
if (this.temperatureService) {
this.temperatureService
.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.updateValue(this.outletState.temperature);
}
})
.catch((e) => {
this.log.error('Error while retrieving Device Report ->', e.toString());
});
}
private setOn(
value: CharacteristicValue,
callback: CharacteristicSetCallback,
) {
this.log.debug('Set Characteristic On ->', value);
this.outletState.relay = value as boolean;
this.setDeviceState(this.outletState.relay);
callback(null);
}
private getOn(callback: CharacteristicGetCallback) {
const isOn = this.outletState?.relay;
this.log.debug('Get Characteristic On ->', isOn);
callback(null, isOn);
}
private getTemperature(callback: CharacteristicGetCallback) {
const temperature: number = this.outletState?.temperature;
this.log.debug('Get Characteristic Temperature ->', temperature, '° C');
callback(null, temperature);
}
private getOutletInUse(callback: CharacteristicGetCallback) {
const inUse: boolean = this.outletState?.power > 0;
this.log.debug('Get Characteristic OutletInUse ->', inUse);
callback(null, inUse);
}
private setDeviceState(isOn: boolean) {
const relayUrl = `${this.baseUrl}/relay?state=${isOn ? '1' : '0'}`;
DingzDaHomebridgePlatform.fetch({
url: relayUrl,
token: this.device.token,
});
}
private async getDeviceReport(): Promise<MyStromSwitchReport> {
const reportUrl = `${this.baseUrl}/report`;
return await DingzDaHomebridgePlatform.fetch({
url: reportUrl,
returnBody: true,
token: this.device.token,
});
}
}