homebridge-kasa-python 2.7.0-beta.2 → 2.7.0-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.js +1 -2
- package/dist/config.js.map +1 -1
- package/dist/devices/deviceManager.d.ts +3 -6
- package/dist/devices/deviceManager.js +25 -69
- package/dist/devices/deviceManager.js.map +1 -1
- package/dist/devices/homekitLightBulb.d.ts +11 -1
- package/dist/devices/homekitLightBulb.js +147 -13
- package/dist/devices/homekitLightBulb.js.map +1 -1
- package/dist/devices/homekitPlug.d.ts +10 -1
- package/dist/devices/homekitPlug.js +112 -7
- package/dist/devices/homekitPlug.js.map +1 -1
- package/dist/devices/homekitPowerStrip.d.ts +10 -1
- package/dist/devices/homekitPowerStrip.js +121 -8
- package/dist/devices/homekitPowerStrip.js.map +1 -1
- package/dist/devices/homekitSwitch.d.ts +11 -1
- package/dist/devices/homekitSwitch.js +118 -8
- package/dist/devices/homekitSwitch.js.map +1 -1
- package/dist/devices/index.d.ts +4 -14
- package/dist/devices/index.js +6 -215
- package/dist/devices/index.js.map +1 -1
- package/dist/devices/kasaDevices.d.ts +4 -6
- package/dist/devices/kasaDevices.js +2 -0
- package/dist/devices/kasaDevices.js.map +1 -1
- package/dist/platform.d.ts +0 -1
- package/dist/platform.js +12 -85
- package/dist/platform.js.map +1 -1
- package/dist/python/kasaApi.py +182 -232
- package/dist/python/pythonChecker.js +6 -10
- package/dist/python/pythonChecker.js.map +1 -1
- package/dist/utils.d.ts +2 -8
- package/dist/utils.js +2 -19
- package/dist/utils.js.map +1 -1
- package/package.json +1 -1
- package/requirements.txt +0 -1
|
@@ -1,19 +1,124 @@
|
|
|
1
1
|
import HomekitDevice from './index.js';
|
|
2
|
+
import { deferAndCombine } from '../utils.js';
|
|
2
3
|
export default class HomeKitDevicePlug extends HomekitDevice {
|
|
3
4
|
kasaDevice;
|
|
5
|
+
isUpdating = false;
|
|
6
|
+
previousKasaDevice;
|
|
7
|
+
getSysInfo;
|
|
4
8
|
constructor(platform, kasaDevice) {
|
|
5
9
|
super(platform, kasaDevice, 7 /* Categories.OUTLET */, 'OUTLET');
|
|
6
10
|
this.kasaDevice = kasaDevice;
|
|
7
11
|
this.log.debug(`Initializing HomeKitDevicePlug for device: ${kasaDevice.sys_info.alias}`);
|
|
8
|
-
this.
|
|
12
|
+
this.checkService();
|
|
13
|
+
this.getSysInfo = deferAndCombine(async () => {
|
|
14
|
+
if (this.deviceManager) {
|
|
15
|
+
this.previousKasaDevice = JSON.parse(JSON.stringify(this.kasaDevice));
|
|
16
|
+
this.kasaDevice.sys_info = await this.deviceManager.getSysInfo(this.deviceConfig);
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
this.log.warn('Device manager is not available');
|
|
20
|
+
}
|
|
21
|
+
}, platform.config.waitTimeUpdate);
|
|
22
|
+
this.startPolling();
|
|
9
23
|
}
|
|
10
|
-
|
|
24
|
+
checkService() {
|
|
11
25
|
const { Outlet } = this.platform.Service;
|
|
12
|
-
const
|
|
13
|
-
this.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
26
|
+
const service = this.homebridgeAccessory.getService(Outlet) ?? this.addService(Outlet, this.name);
|
|
27
|
+
this.checkCharacteristics(service);
|
|
28
|
+
return service;
|
|
29
|
+
}
|
|
30
|
+
checkCharacteristics(service) {
|
|
31
|
+
const characteristics = [
|
|
32
|
+
{
|
|
33
|
+
type: this.platform.Characteristic.On,
|
|
34
|
+
name: this.platform.getCharacteristicName(this.platform.Characteristic.On),
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
type: this.platform.Characteristic.OutletInUse,
|
|
38
|
+
name: this.platform.getCharacteristicName(this.platform.Characteristic.OutletInUse),
|
|
39
|
+
},
|
|
40
|
+
].filter(Boolean);
|
|
41
|
+
characteristics.forEach(({ type, name }) => {
|
|
42
|
+
this.getOrAddCharacteristic(service, type, name);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
getOrAddCharacteristic(service, characteristicType, characteristicName) {
|
|
46
|
+
const characteristic = service.getCharacteristic(characteristicType) ??
|
|
47
|
+
service.addCharacteristic(characteristicType);
|
|
48
|
+
characteristic.onGet(this.handleOnGet.bind(this, service, characteristicType, characteristicName));
|
|
49
|
+
if (characteristicType === this.platform.Characteristic.On) {
|
|
50
|
+
characteristic.onSet(this.handleOnSet.bind(this, service, characteristicType, characteristicName));
|
|
51
|
+
}
|
|
52
|
+
return service;
|
|
53
|
+
}
|
|
54
|
+
async handleOnGet(service, characteristicType, characteristicName) {
|
|
55
|
+
try {
|
|
56
|
+
const characteristicValue = service.getCharacteristic(characteristicType).value;
|
|
57
|
+
return characteristicValue ?? false;
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
this.log.error(`Error getting current value for characteristic ${characteristicName} for device: ${this.name}:`, error);
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
async handleOnSet(service, characteristicType, characteristicName, value) {
|
|
65
|
+
if (this.deviceManager) {
|
|
66
|
+
try {
|
|
67
|
+
this.isUpdating = true;
|
|
68
|
+
const characteristicMap = {
|
|
69
|
+
On: 'state',
|
|
70
|
+
};
|
|
71
|
+
const characteristicKey = characteristicMap[characteristicName ?? ''];
|
|
72
|
+
if (!characteristicKey) {
|
|
73
|
+
throw new Error(`Characteristic key not found for ${characteristicName}`);
|
|
74
|
+
}
|
|
75
|
+
await this.deviceManager.controlDevice(this.deviceConfig, characteristicKey, value);
|
|
76
|
+
this.kasaDevice.sys_info[characteristicKey] = value;
|
|
77
|
+
this.updateValue(service, service.getCharacteristic(characteristicType), this.name, value);
|
|
78
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.OutletInUse), this.name, value);
|
|
79
|
+
this.previousKasaDevice = JSON.parse(JSON.stringify(this.kasaDevice));
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
this.logRejection(error);
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
this.isUpdating = false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
throw new Error('Device manager is undefined.');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async updateState() {
|
|
93
|
+
if (this.isUpdating) {
|
|
94
|
+
this.log.debug('Update already in progress, skipping updateState');
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
this.isUpdating = true;
|
|
98
|
+
try {
|
|
99
|
+
await this.getSysInfo();
|
|
100
|
+
const service = this.homebridgeAccessory.getService(this.platform.Service.Outlet);
|
|
101
|
+
if (service && this.previousKasaDevice) {
|
|
102
|
+
const { state } = this.kasaDevice.sys_info;
|
|
103
|
+
const prevState = this.previousKasaDevice.sys_info;
|
|
104
|
+
if (prevState.state !== state) {
|
|
105
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.On), this.name, state ?? false);
|
|
106
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.OutletInUse), this.name, state ?? false);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
this.log.warn(`Service not found for device: ${this.name} or previous Kasa device is undefined`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
this.log.error('Error updating device state:', error);
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
this.isUpdating = false;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
startPolling() {
|
|
121
|
+
setInterval(this.updateState.bind(this), this.platform.config.discoveryOptions.pollingInterval);
|
|
17
122
|
}
|
|
18
123
|
identify() {
|
|
19
124
|
this.log.info('identify');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"homekitPlug.js","sourceRoot":"","sources":["../../src/devices/homekitPlug.ts"],"names":[],"mappings":"AAGA,OAAO,aAAa,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"homekitPlug.js","sourceRoot":"","sources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,KAAK,KAAK,EAAE,CAAC;oBAC9B,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,IAAI,KAAK,CAAC,CAAC;oBACjH,IAAI,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE,IAAI,CAAC,IAAI,EAAE,KAAK,IAAI,KAAK,CAAC,CAAC;gBAC5H,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,iCAAiC,IAAI,CAAC,IAAI,uCAAuC,CAAC,CAAC;YACnG,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;QACxD,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QAC1B,CAAC;IACH,CAAC;IAEO,YAAY;QAClB,WAAW,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,gBAAgB,CAAC,eAAe,CAAC,CAAC;IAClG,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC5B,CAAC;CACF"}
|
|
@@ -3,7 +3,16 @@ import type KasaPythonPlatform from '../platform.js';
|
|
|
3
3
|
import type { PowerStrip } from './kasaDevices.js';
|
|
4
4
|
export default class HomeKitDevicePowerStrip extends HomekitDevice {
|
|
5
5
|
protected kasaDevice: PowerStrip;
|
|
6
|
+
private isUpdating;
|
|
7
|
+
private previousKasaDevice;
|
|
8
|
+
private getSysInfo;
|
|
6
9
|
constructor(platform: KasaPythonPlatform, kasaDevice: PowerStrip);
|
|
7
|
-
private
|
|
10
|
+
private checkService;
|
|
11
|
+
private checkCharacteristics;
|
|
12
|
+
private getOrAddCharacteristic;
|
|
13
|
+
private handleOnGet;
|
|
14
|
+
private handleOnSet;
|
|
15
|
+
protected updateState(): Promise<void>;
|
|
16
|
+
private startPolling;
|
|
8
17
|
identify(): void;
|
|
9
18
|
}
|
|
@@ -1,23 +1,136 @@
|
|
|
1
1
|
import HomekitDevice from './index.js';
|
|
2
|
+
import { deferAndCombine } from '../utils.js';
|
|
2
3
|
export default class HomeKitDevicePowerStrip extends HomekitDevice {
|
|
3
4
|
kasaDevice;
|
|
5
|
+
isUpdating = false;
|
|
6
|
+
previousKasaDevice;
|
|
7
|
+
getSysInfo;
|
|
4
8
|
constructor(platform, kasaDevice) {
|
|
5
9
|
super(platform, kasaDevice, 7 /* Categories.OUTLET */, 'OUTLET');
|
|
6
10
|
this.kasaDevice = kasaDevice;
|
|
7
11
|
this.log.debug(`Initializing HomeKitDevicePowerStrip for device: ${kasaDevice.sys_info.alias}`);
|
|
8
12
|
this.kasaDevice.sys_info.children?.forEach((child, index) => {
|
|
9
|
-
this.
|
|
10
|
-
this.addOutletService(child, index);
|
|
13
|
+
this.checkService(child, index);
|
|
11
14
|
});
|
|
15
|
+
this.getSysInfo = deferAndCombine(async () => {
|
|
16
|
+
if (this.deviceManager) {
|
|
17
|
+
this.previousKasaDevice = JSON.parse(JSON.stringify(this.kasaDevice));
|
|
18
|
+
this.kasaDevice.sys_info = await this.deviceManager.getSysInfo(this.deviceConfig);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.log.warn('Device manager is not available');
|
|
22
|
+
}
|
|
23
|
+
}, platform.config.waitTimeUpdate);
|
|
24
|
+
this.startPolling();
|
|
12
25
|
}
|
|
13
|
-
|
|
26
|
+
checkService(child, index) {
|
|
14
27
|
const { Outlet } = this.platform.Service;
|
|
15
|
-
const
|
|
28
|
+
const service = this.homebridgeAccessory.getServiceById(Outlet, `outlet-${index + 1}`) ??
|
|
16
29
|
this.addService(Outlet, child.alias, `outlet-${index + 1}`);
|
|
17
|
-
this.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
30
|
+
this.checkCharacteristics(service, child);
|
|
31
|
+
return service;
|
|
32
|
+
}
|
|
33
|
+
checkCharacteristics(service, child) {
|
|
34
|
+
const characteristics = [
|
|
35
|
+
{
|
|
36
|
+
type: this.platform.Characteristic.On,
|
|
37
|
+
name: this.platform.getCharacteristicName(this.platform.Characteristic.On),
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
type: this.platform.Characteristic.OutletInUse,
|
|
41
|
+
name: this.platform.getCharacteristicName(this.platform.Characteristic.OutletInUse),
|
|
42
|
+
},
|
|
43
|
+
].filter(Boolean);
|
|
44
|
+
characteristics.forEach(({ type, name }) => {
|
|
45
|
+
this.getOrAddCharacteristic(service, type, name, child);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
getOrAddCharacteristic(service, characteristicType, characteristicName, child) {
|
|
49
|
+
const characteristic = service.getCharacteristic(characteristicType) ??
|
|
50
|
+
service.addCharacteristic(characteristicType);
|
|
51
|
+
characteristic.onGet(this.handleOnGet.bind(this, service, characteristicType, characteristicName, child));
|
|
52
|
+
if (characteristicType === this.platform.Characteristic.On) {
|
|
53
|
+
characteristic.onSet(this.handleOnSet.bind(this, service, characteristicType, characteristicName, child));
|
|
54
|
+
}
|
|
55
|
+
return service;
|
|
56
|
+
}
|
|
57
|
+
async handleOnGet(service, characteristicType, characteristicName, child) {
|
|
58
|
+
try {
|
|
59
|
+
const characteristicValue = service.getCharacteristic(characteristicType).value;
|
|
60
|
+
return characteristicValue ?? false;
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
this.log.error(`Error getting current value for characteristic ${characteristicName} for device: ${child.alias}:`, error);
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
async handleOnSet(service, characteristicType, characteristicName, child, value) {
|
|
68
|
+
if (this.deviceManager) {
|
|
69
|
+
try {
|
|
70
|
+
this.isUpdating = true;
|
|
71
|
+
const characteristicMap = {
|
|
72
|
+
On: 'state',
|
|
73
|
+
};
|
|
74
|
+
const characteristicKey = characteristicMap[characteristicName ?? ''];
|
|
75
|
+
if (!characteristicKey) {
|
|
76
|
+
throw new Error(`Characteristic key not found for ${characteristicName}`);
|
|
77
|
+
}
|
|
78
|
+
const childNumber = parseInt(child.id.slice(-1), 10);
|
|
79
|
+
await this.deviceManager.controlDevice(this.deviceConfig, characteristicKey, value, childNumber);
|
|
80
|
+
child[characteristicKey] = value;
|
|
81
|
+
const childIndex = this.kasaDevice.sys_info.children?.findIndex(c => c.id === child.id);
|
|
82
|
+
if (childIndex !== undefined && childIndex !== -1) {
|
|
83
|
+
this.kasaDevice.sys_info.children[childIndex] = { ...child };
|
|
84
|
+
}
|
|
85
|
+
this.updateValue(service, service.getCharacteristic(characteristicType), child.alias, value);
|
|
86
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.OutletInUse), child.alias, value);
|
|
87
|
+
this.previousKasaDevice = JSON.parse(JSON.stringify(this.kasaDevice));
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
this.logRejection(error);
|
|
91
|
+
}
|
|
92
|
+
finally {
|
|
93
|
+
this.isUpdating = false;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
throw new Error('Device manager is undefined.');
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
async updateState() {
|
|
101
|
+
if (this.isUpdating) {
|
|
102
|
+
this.log.debug('Update already in progress, skipping updateState');
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
this.isUpdating = true;
|
|
106
|
+
try {
|
|
107
|
+
await this.getSysInfo();
|
|
108
|
+
this.kasaDevice.sys_info.children?.forEach((child) => {
|
|
109
|
+
const childNumber = parseInt(child.id.slice(-1), 10);
|
|
110
|
+
const service = this.homebridgeAccessory.getServiceById(this.platform.Service.Outlet, `outlet-${childNumber + 1}`);
|
|
111
|
+
if (service && this.previousKasaDevice) {
|
|
112
|
+
const previousChild = this.previousKasaDevice.sys_info.children?.find(c => c.id === child.id);
|
|
113
|
+
if (previousChild) {
|
|
114
|
+
if (previousChild.state !== child.state) {
|
|
115
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.On), child.alias, child.state);
|
|
116
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.OutletInUse), child.alias, child.state);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
this.log.warn(`Service not found for child device: ${child.alias} or previous Kasa device is undefined`);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
this.log.error('Error updating device state:', error);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
this.isUpdating = false;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
startPolling() {
|
|
133
|
+
setInterval(this.updateState.bind(this), this.platform.config.discoveryOptions.pollingInterval);
|
|
21
134
|
}
|
|
22
135
|
identify() {
|
|
23
136
|
this.log.info('identify');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"homekitPowerStrip.js","sourceRoot":"","sources":["../../src/devices/homekitPowerStrip.ts"],"names":[],"mappings":"AAGA,OAAO,aAAa,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"homekitPowerStrip.js","sourceRoot":"","sources":["../../src/devices/homekitPowerStrip.ts"],"names":[],"mappings":"AAGA,OAAO,aAAa,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAI9C,MAAM,CAAC,OAAO,OAAO,uBAAwB,SAAQ,aAAa;IAOpD;IANJ,UAAU,GAAY,KAAK,CAAC;IAC5B,kBAAkB,CAAy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,KAAK,CAAC,KAAK,EAAE,KAAK,CAAC,KAAK,CAAC,CAAC;wBAC3H,CAAC;oBACH,CAAC;gBACH,CAAC;qBAAM,CAAC;oBACN,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,uCAAuC,KAAK,CAAC,KAAK,uCAAuC,CAAC,CAAC;gBAC3G,CAAC;YACH,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;QACxD,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QAC1B,CAAC;IACH,CAAC;IAEO,YAAY;QAClB,WAAW,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,gBAAgB,CAAC,eAAe,CAAC,CAAC;IAClG,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC5B,CAAC;CACF"}
|
|
@@ -3,8 +3,18 @@ import type KasaPythonPlatform from '../platform.js';
|
|
|
3
3
|
import type { Switch } from './kasaDevices.js';
|
|
4
4
|
export default class HomeKitDeviceSwitch extends HomekitDevice {
|
|
5
5
|
protected kasaDevice: Switch;
|
|
6
|
+
private isUpdating;
|
|
7
|
+
private previousKasaDevice;
|
|
8
|
+
private getSysInfo;
|
|
6
9
|
private hasBrightness;
|
|
7
10
|
constructor(platform: KasaPythonPlatform, kasaDevice: Switch);
|
|
8
|
-
private
|
|
11
|
+
private checkService;
|
|
12
|
+
private checkCharacteristics;
|
|
13
|
+
private getOrAddCharacteristic;
|
|
14
|
+
private handleOnGet;
|
|
15
|
+
private getDefaultValue;
|
|
16
|
+
private handleOnSet;
|
|
17
|
+
protected updateState(): Promise<void>;
|
|
18
|
+
private startPolling;
|
|
9
19
|
identify(): void;
|
|
10
20
|
}
|
|
@@ -1,24 +1,134 @@
|
|
|
1
1
|
import HomekitDevice from './index.js';
|
|
2
|
+
import { deferAndCombine } from '../utils.js';
|
|
2
3
|
export default class HomeKitDeviceSwitch extends HomekitDevice {
|
|
3
4
|
kasaDevice;
|
|
5
|
+
isUpdating = false;
|
|
6
|
+
previousKasaDevice;
|
|
7
|
+
getSysInfo;
|
|
4
8
|
hasBrightness;
|
|
5
9
|
constructor(platform, kasaDevice) {
|
|
6
10
|
super(platform, kasaDevice, 8 /* Categories.SWITCH */, 'SWITCH');
|
|
7
11
|
this.kasaDevice = kasaDevice;
|
|
8
12
|
this.log.debug(`Initializing HomeKitDeviceSwitch for device: ${kasaDevice.sys_info.alias}`);
|
|
9
13
|
this.hasBrightness = !!this.kasaDevice.feature_info.brightness;
|
|
10
|
-
this.
|
|
14
|
+
this.checkService();
|
|
15
|
+
this.getSysInfo = deferAndCombine(async () => {
|
|
16
|
+
if (this.deviceManager) {
|
|
17
|
+
this.previousKasaDevice = JSON.parse(JSON.stringify(this.kasaDevice));
|
|
18
|
+
this.kasaDevice.sys_info = await this.deviceManager.getSysInfo(this.deviceConfig);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.log.warn('Device manager is not available');
|
|
22
|
+
}
|
|
23
|
+
}, platform.config.waitTimeUpdate);
|
|
24
|
+
this.startPolling();
|
|
11
25
|
}
|
|
12
|
-
|
|
26
|
+
checkService() {
|
|
13
27
|
const { Switch, Lightbulb } = this.platform.Service;
|
|
14
28
|
const serviceType = this.hasBrightness ? Lightbulb : Switch;
|
|
15
|
-
const
|
|
16
|
-
this.
|
|
17
|
-
|
|
18
|
-
|
|
29
|
+
const service = this.homebridgeAccessory.getService(serviceType) ?? this.addService(serviceType, this.name);
|
|
30
|
+
this.checkCharacteristics(service);
|
|
31
|
+
return service;
|
|
32
|
+
}
|
|
33
|
+
checkCharacteristics(service) {
|
|
34
|
+
const characteristics = [
|
|
35
|
+
{
|
|
36
|
+
type: this.platform.Characteristic.On,
|
|
37
|
+
name: this.platform.getCharacteristicName(this.platform.Characteristic.On),
|
|
38
|
+
},
|
|
39
|
+
this.hasBrightness && {
|
|
40
|
+
type: this.platform.Characteristic.Brightness,
|
|
41
|
+
name: this.platform.getCharacteristicName(this.platform.Characteristic.Brightness),
|
|
42
|
+
},
|
|
43
|
+
].filter(Boolean);
|
|
44
|
+
characteristics.forEach(({ type, name }) => {
|
|
45
|
+
this.getOrAddCharacteristic(service, type, name);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
getOrAddCharacteristic(service, characteristicType, characteristicName) {
|
|
49
|
+
const characteristic = service.getCharacteristic(characteristicType) ??
|
|
50
|
+
service.addCharacteristic(characteristicType);
|
|
51
|
+
characteristic.onGet(this.handleOnGet.bind(this, service, characteristicType, characteristicName));
|
|
52
|
+
characteristic.onSet(this.handleOnSet.bind(this, service, characteristicType, characteristicName));
|
|
53
|
+
return characteristic;
|
|
54
|
+
}
|
|
55
|
+
async handleOnGet(service, characteristicType, characteristicName) {
|
|
56
|
+
try {
|
|
57
|
+
const characteristicValue = service.getCharacteristic(characteristicType).value;
|
|
58
|
+
return characteristicValue ?? this.getDefaultValue(characteristicType);
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
this.log.error(`Error getting current value for characteristic ${characteristicName} for device: ${this.name}:`, error);
|
|
62
|
+
}
|
|
63
|
+
return this.getDefaultValue(characteristicType);
|
|
64
|
+
}
|
|
65
|
+
getDefaultValue(characteristicType) {
|
|
66
|
+
if (characteristicType === this.platform.Characteristic.Brightness) {
|
|
67
|
+
return 0;
|
|
68
|
+
}
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
async handleOnSet(service, characteristicType, characteristicName, value) {
|
|
72
|
+
if (this.deviceManager) {
|
|
73
|
+
try {
|
|
74
|
+
this.isUpdating = true;
|
|
75
|
+
const characteristicMap = {
|
|
76
|
+
Brightness: 'brightness',
|
|
77
|
+
On: 'state',
|
|
78
|
+
};
|
|
79
|
+
const characteristicKey = characteristicMap[characteristicName ?? ''];
|
|
80
|
+
if (!characteristicKey) {
|
|
81
|
+
throw new Error(`Characteristic key not found for ${characteristicName}`);
|
|
82
|
+
}
|
|
83
|
+
await this.deviceManager.controlDevice(this.deviceConfig, characteristicKey, value);
|
|
84
|
+
this.kasaDevice.sys_info[characteristicKey] = value;
|
|
85
|
+
this.updateValue(service, service.getCharacteristic(characteristicType), this.name, value);
|
|
86
|
+
this.previousKasaDevice = JSON.parse(JSON.stringify(this.kasaDevice));
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
this.logRejection(error);
|
|
90
|
+
}
|
|
91
|
+
finally {
|
|
92
|
+
this.isUpdating = false;
|
|
93
|
+
}
|
|
19
94
|
}
|
|
20
|
-
|
|
21
|
-
|
|
95
|
+
else {
|
|
96
|
+
throw new Error('Device manager is undefined.');
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
async updateState() {
|
|
100
|
+
if (this.isUpdating) {
|
|
101
|
+
this.log.debug('Update already in progress, skipping updateState');
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.isUpdating = true;
|
|
105
|
+
try {
|
|
106
|
+
await this.getSysInfo();
|
|
107
|
+
const service = this.homebridgeAccessory.getService(this.platform.Service.Switch) ??
|
|
108
|
+
this.homebridgeAccessory.getService(this.platform.Service.Lightbulb);
|
|
109
|
+
if (service && this.previousKasaDevice) {
|
|
110
|
+
const { state, brightness } = this.kasaDevice.sys_info;
|
|
111
|
+
const prevState = this.previousKasaDevice.sys_info;
|
|
112
|
+
if (prevState.state !== state) {
|
|
113
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.On), this.name, state ?? false);
|
|
114
|
+
}
|
|
115
|
+
if (this.hasBrightness && prevState.brightness !== brightness) {
|
|
116
|
+
this.updateValue(service, service.getCharacteristic(this.platform.Characteristic.Brightness), this.name, brightness ?? 0);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
this.log.warn(`Service not found for device: ${this.name} or previous Kasa device is undefined`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
this.log.error('Error updating device state:', error);
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
this.isUpdating = false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
startPolling() {
|
|
131
|
+
setInterval(this.updateState.bind(this), this.platform.config.discoveryOptions.pollingInterval);
|
|
22
132
|
}
|
|
23
133
|
identify() {
|
|
24
134
|
this.log.info('identify');
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"homekitSwitch.js","sourceRoot":"","sources":["../../src/devices/homekitSwitch.ts"],"names":[],"mappings":"AAGA,OAAO,aAAa,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"homekitSwitch.js","sourceRoot":"","sources":["../../src/devices/homekitSwitch.ts"],"names":[],"mappings":"AAGA,OAAO,aAAa,MAAM,YAAY,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAI9C,MAAM,CAAC,OAAO,OAAO,mBAAoB,SAAQ,aAAa;IAQhD;IAPJ,UAAU,GAAY,KAAK,CAAC;IAC5B,kBAAkB,CAAyB;IAC3C,UAAU,CAAsB;IAChC,aAAa,CAAU;IAE/B,YACE,QAA4B,EAClB,UAAkB;QAE5B,KAAK,CACH,QAAQ,EACR,UAAU,6BAEV,QAAQ,CACT,CAAC;QAPQ,eAAU,GAAV,UAAU,CAAQ;QAQ5B,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,gDAAgD,UAAU,CAAC,QAAQ,CAAC,KAAK,EAAE,CAAC,CAAC;QAC5F,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,UAAU,CAAC;QAC/D,IAAI,CAAC,YAAY,EAAE,CAAC;QAEpB,IAAI,CAAC,UAAU,GAAG,eAAe,C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AAI,CAAC,WAAW,CAAC,OAAO,EAAE,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,UAAU,CAAC,EAAE,IAAI,CAAC,IAAI,EAAE,UAAU,IAAI,CAAC,CAAC,CAAC;gBAC5H,CAAC;YACH,CAAC;iBAAM,CAAC;gBACN,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,iCAAiC,IAAI,CAAC,IAAI,uCAAuC,CAAC,CAAC;YACnG,CAAC;QACH,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,CAAC,CAAC;QACxD,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC;QAC1B,CAAC;IACH,CAAC;IAEO,YAAY;QAClB,WAAW,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,gBAAgB,CAAC,eAAe,CAAC,CAAC;IAClG,CAAC;IAED,QAAQ;QACN,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;IAC5B,CAAC;CACF"}
|
package/dist/devices/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Categories, Characteristic, CharacteristicValue, HapStatusError, Logger, Nullable, PlatformAccessory, Service
|
|
1
|
+
import { Categories, Characteristic, CharacteristicValue, HapStatusError, Logger, Nullable, PlatformAccessory, Service } from 'homebridge';
|
|
2
2
|
import DeviceManager from './deviceManager.js';
|
|
3
3
|
import type KasaPythonPlatform from '../platform.js';
|
|
4
|
-
import type {
|
|
4
|
+
import type { DeviceConfig, KasaDevice } from './kasaDevices.js';
|
|
5
5
|
import type { KasaPythonAccessoryContext } from '../platform.js';
|
|
6
6
|
export default abstract class HomekitDevice {
|
|
7
7
|
readonly platform: KasaPythonPlatform;
|
|
@@ -12,9 +12,6 @@ export default abstract class HomekitDevice {
|
|
|
12
12
|
readonly deviceConfig: DeviceConfig;
|
|
13
13
|
protected deviceManager: DeviceManager | undefined;
|
|
14
14
|
homebridgeAccessory: PlatformAccessory<KasaPythonAccessoryContext>;
|
|
15
|
-
private isUpdating;
|
|
16
|
-
private previousKasaDevice;
|
|
17
|
-
private getSysInfo;
|
|
18
15
|
constructor(platform: KasaPythonPlatform, kasaDevice: KasaDevice, category: Categories, categoryName: string);
|
|
19
16
|
private initializeAccessory;
|
|
20
17
|
private updateAccessory;
|
|
@@ -26,14 +23,7 @@ export default abstract class HomekitDevice {
|
|
|
26
23
|
get serialNumber(): string;
|
|
27
24
|
get firmwareRevision(): string;
|
|
28
25
|
abstract identify(): void;
|
|
29
|
-
updateValue(service: Service, characteristic: Characteristic, value: Nullable<CharacteristicValue> | Error | HapStatusError, childDeviceAlias?: string): void;
|
|
30
26
|
addService(serviceConstructor: typeof this.platform.Service.Outlet, name: string, subType?: string): Service;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
private handleOnGet;
|
|
34
|
-
private getCharacteristicValue;
|
|
35
|
-
private getDefaultValue;
|
|
36
|
-
private handleOnSet;
|
|
37
|
-
protected updateState(): Promise<void>;
|
|
38
|
-
private startPolling;
|
|
27
|
+
updateValue(service: Service, characteristic: Characteristic, deviceAlias: string, value: Nullable<CharacteristicValue> | Error | HapStatusError): void;
|
|
28
|
+
logRejection(reason: unknown): void;
|
|
39
29
|
}
|