-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplatformAccessory.ts
200 lines (164 loc) · 7.19 KB
/
platformAccessory.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
193
194
195
196
197
198
199
200
import { CapabilityReference, Component, Device } from '@smartthings/core-sdk';
import { TargetHeaterCoolerState } from 'hap-nodejs/dist/lib/definitions';
import { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
import { DeviceAdapter } from './deviceAdapter';
import { SmartThingsPlatform } from './platform';
import { PlatformStatusInfo } from './platformStatusInfo';
const defaultUpdateInterval = 15;
const defaultMinTemperature = 16;
const defaultMaxTemperature = 30;
export class SmartThingsAirConditionerAccessory {
private service: Service;
private device: Device;
private deviceStatus: PlatformStatusInfo;
public static readonly requiredCapabilities = [
'switch',
'temperatureMeasurement',
'thermostatCoolingSetpoint',
'airConditionerMode',
];
constructor(
private readonly platform: SmartThingsPlatform,
private readonly accessory: PlatformAccessory,
private readonly deviceAdapter: DeviceAdapter,
) {
this.device = accessory.context.device as Device;
this.deviceStatus = {
mode: 'auto',
active: false,
currentHumidity: 0,
currentTemperature: this.platform.config.minTemperature ?? defaultMinTemperature,
targetTemperature: this.platform.config.minTemperature ?? defaultMinTemperature,
};
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, this.device.manufacturerName ?? 'unknown')
.setCharacteristic(this.platform.Characteristic.Model, this.device.name ?? 'unknown')
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.device.presentationId ?? 'unknown');
this.service = this.accessory.getService(this.platform.Service.HeaterCooler)
|| this.accessory.addService(this.platform.Service.HeaterCooler);
this.service.setCharacteristic(this.platform.Characteristic.Name, this.device.label ?? 'unkown');
this.service.getCharacteristic(this.platform.Characteristic.Active)
.onSet(this.setActive.bind(this))
.onGet(this.getActive.bind(this));
const temperatureProperties = {
maxValue: this.platform.config.maxTemperature ?? defaultMaxTemperature,
minValue: this.platform.config.minTemperature ?? defaultMinTemperature,
minStep: 1,
};
this.service.getCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature)
.setProps(temperatureProperties)
.onGet(this.getCoolingTemperature.bind(this))
.onSet(this.setCoolingTemperature.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature)
.setProps(temperatureProperties)
.onGet(this.getCoolingTemperature.bind(this))
.onSet(this.setCoolingTemperature.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState)
.onGet(this.getHeaterCoolerState.bind(this))
.onSet(this.setHeaterCoolerState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.getCurrentTemperature.bind(this));
if (this.hasCapability('relativeHumidityMeasurement')) {
this.platform.log.debug('Registering current relative humidity characteristic for device', this.device.deviceId);
this.service.getCharacteristic(this.platform.Characteristic.CurrentRelativeHumidity)
.onGet(this.getCurrentHumidity.bind(this));
} else {
this.platform.log.info('Current relative humidity will not be available for device', this.device.deviceId);
}
const updateInterval = this.platform.config.updateInterval ?? defaultUpdateInterval;
this.platform.log.info('Update status every', updateInterval, 'secs');
this.updateStatus();
setInterval(async () => {
await this.updateStatus();
}, updateInterval * 1000);
}
private hasCapability(id: string): boolean {
return !!this.device.components
?.filter((component: Component) => component.id === 'main')
?.flatMap((component: Component) => component.capabilities)
?.find((capabilityReference: CapabilityReference) => capabilityReference.id === id);
}
private getHeaterCoolerState():CharacteristicValue {
return this.fromSmartThingsMode(this.deviceStatus.mode);
}
private getCoolingTemperature(): CharacteristicValue {
return this.deviceStatus.targetTemperature;
}
private getActive(): CharacteristicValue {
return this.deviceStatus.active;
}
private getCurrentTemperature(): CharacteristicValue {
return this.deviceStatus.currentTemperature;
}
private getCurrentHumidity(): CharacteristicValue {
return this.deviceStatus.currentHumidity;
}
private async setActive(value: CharacteristicValue) {
const isActive = value === 1;
try {
await this.executeCommand(isActive ? 'on' : 'off', 'switch');
this.deviceStatus.active = isActive;
} catch(error) {
this.platform.log.error('Cannot set device active', error);
await this.updateStatus();
}
}
private async setHeaterCoolerState(value: CharacteristicValue) {
const mode = this.toSmartThingsMode(value);
try {
await this.executeCommand('setAirConditionerMode', 'airConditionerMode', [ mode ]);
this.deviceStatus.mode = mode;
} catch(error) {
this.platform.log.error('Cannot set device mode', error);
await this.updateStatus();
}
}
private async setCoolingTemperature(value: CharacteristicValue) {
const targetTemperature = value as number;
try {
await this.executeCommand('setCoolingSetpoint', 'thermostatCoolingSetpoint', [targetTemperature]);
this.deviceStatus.targetTemperature = targetTemperature;
} catch(error) {
this.platform.log.error('Cannot set device temperature', error);
await this.updateStatus();
}
}
private toSmartThingsMode(value: CharacteristicValue): string {
switch (value) {
case TargetHeaterCoolerState.HEAT: return 'heat';
case TargetHeaterCoolerState.COOL: return 'cool';
case TargetHeaterCoolerState.AUTO: return 'auto';
}
this.platform.log.warn('Illegal heater-cooler state', value);
return 'auto';
}
private fromSmartThingsMode(state: string): CharacteristicValue {
switch (state) {
case 'cool': return TargetHeaterCoolerState.COOL;
case 'auto': return TargetHeaterCoolerState.AUTO;
case 'heat': return TargetHeaterCoolerState.HEAT;
}
this.platform.log.warn('Received unknown heater-cooler state', state);
return TargetHeaterCoolerState.AUTO;
}
private async updateStatus() {
try {
this.deviceStatus = await this.getStatus();
} catch(error: unknown) {
this.platform.log.error('Error while fetching device status: ' + this.getErrorMessage(error));
this.platform.log.debug('Caught error', error);
}
}
private getErrorMessage(error: unknown): string {
if (error instanceof Error) {
return error.message;
}
return String(error);
}
private async executeCommand(command: string, capability: string, commandArguments?: (string | number)[]) {
await this.deviceAdapter.executeMainCommand(command, capability, commandArguments);
}
private getStatus(): Promise<PlatformStatusInfo> {
return this.deviceAdapter.getStatus();
}
}