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
|
import type { Service, PlatformAccessory, CharacteristicValue } from 'homebridge';
import type { Device } from './device.js';
import { Platform } from './platform.js';
import { WindLevel } from './device.js';
export class Accessory {
private service: Service;
constructor(
private readonly platform: Platform,
private readonly accessory: PlatformAccessory,
private readonly device: Device,
) {
this.accessory.getService(this.platform.Service.AccessoryInformation)!
.setCharacteristic(this.platform.Characteristic.Manufacturer, 'Xiaomi')
.setCharacteristic(this.platform.Characteristic.Model, 'xiaomi.aircondition.ma2')
.setCharacteristic(this.platform.Characteristic.SerialNumber, this.device.deviceId().toString());
this.service = this.accessory.getService(this.platform.Service.HeaterCooler)
|| this.accessory.addService(this.platform.Service.HeaterCooler);
this.service.setCharacteristic(this.platform.Characteristic.Name, this.accessory.context.config.name);
this.service.getCharacteristic(this.platform.Characteristic.Active)
.onSet(this.setActive.bind(this))
.onGet(this.getActive.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CurrentHeaterCoolerState)
.onGet(this.getCurrentHeaterCoolerState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.TargetHeaterCoolerState)
.onSet(this.setTargetHeaterCoolerState.bind(this))
.onGet(this.getTargetHeaterCoolerState.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CurrentTemperature)
.onGet(this.getCurrentTemperature.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.CoolingThresholdTemperature)
.onSet(this.setTargetTemperature.bind(this))
.onGet(this.getTargetTemperature.bind(this))
.setProps({
minValue: 16,
maxValue: 31,
minStep: 0.5,
});
this.service.getCharacteristic(this.platform.Characteristic.HeatingThresholdTemperature)
.onSet(this.setTargetTemperature.bind(this))
.onGet(this.getTargetTemperature.bind(this))
.setProps({
minValue: 16,
maxValue: 31,
minStep: 0.5,
});
this.service.getCharacteristic(this.platform.Characteristic.RotationSpeed)
.onSet(this.setRotationSpeed.bind(this))
.onGet(this.getRotationSpeed.bind(this));
this.service.getCharacteristic(this.platform.Characteristic.SwingMode)
.onSet(this.setSwingMode.bind(this))
.onGet(this.getSwingMode.bind(this));
this.polling();
}
async polling() {
await this.device.refresh();
this.platform.log.debug('Polling state from device: ', this.device.getRawState());
setTimeout(this.polling.bind(this), 60000);
}
async setActive(value: CharacteristicValue) {
if (value === this.platform.Characteristic.Active.ACTIVE) {
await this.device.turnOn();
return;
}
await this.device.turnOff();
}
async getActive(): Promise<CharacteristicValue> {
return this.device.isOn()
? this.platform.Characteristic.Active.ACTIVE
: this.platform.Characteristic.Active.INACTIVE;
}
async getCurrentHeaterCoolerState(): Promise<CharacteristicValue> {
if (this.device.isOff()) {
return this.platform.Characteristic.CurrentHeaterCoolerState.INACTIVE;
}
if (this.device.isCooling()) {
return this.device.temperature() > this.device.targetTemperature()
? this.platform.Characteristic.CurrentHeaterCoolerState.COOLING
: this.platform.Characteristic.CurrentHeaterCoolerState.IDLE;
}
if (this.device.isHeating()) {
return this.device.temperature() < this.device.targetTemperature()
? this.platform.Characteristic.CurrentHeaterCoolerState.HEATING
: this.platform.Characteristic.CurrentHeaterCoolerState.IDLE;
}
return this.platform.Characteristic.CurrentHeaterCoolerState.COOLING;
}
async setTargetHeaterCoolerState(value: CharacteristicValue) {
switch (value) {
case this.platform.Characteristic.TargetHeaterCoolerState.HEAT:
await this.device.heating();
break;
case this.platform.Characteristic.TargetHeaterCoolerState.COOL:
await this.device.cooling();
break;
default:
await this.device.auto();
break;
}
}
async getTargetHeaterCoolerState(): Promise<CharacteristicValue> {
if (this.device.isCooling()) {
return this.platform.Characteristic.TargetHeaterCoolerState.COOL;
}
if (this.device.isHeating()) {
return this.platform.Characteristic.TargetHeaterCoolerState.HEAT;
}
return this.platform.Characteristic.TargetHeaterCoolerState.AUTO;
}
getCurrentTemperature(): CharacteristicValue {
return this.device.temperature();
}
async setTargetTemperature(value: CharacteristicValue) {
await this.device.temperatureSetTo(value as number);
}
getTargetTemperature(): CharacteristicValue {
return this.device.targetTemperature();
}
async setRotationSpeed(value: CharacteristicValue) {
const level = Math.ceil(value as number / 100 * WindLevel.Level7);
await this.device.windLevelSetTo(level);
}
getRotationSpeed(): CharacteristicValue {
const level = this.device.windLevel();
const speed = level * Math.ceil(100 / WindLevel.Level7);
return speed > 100 ? 100 : speed;
}
async setSwingMode(value: CharacteristicValue) {
if (value === this.platform.Characteristic.SwingMode.SWING_ENABLED) {
await this.device.startSwinging();
} else {
await this.device.stopSwinging();
}
}
getSwingMode() {
return this.device.swinging();
}
}
|