Skip to content

Commit

Permalink
chore: code push
Browse files Browse the repository at this point in the history
  • Loading branch information
baranwang committed Feb 14, 2024
1 parent 109e381 commit 910e70a
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 19 deletions.
49 changes: 34 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/accessories/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Service } from 'homebridge';
import type { AqaraHomebridgePlatform } from '../platform';

export class BaseAccessory {
public services: Service[] = [];

constructor(readonly platform: AqaraHomebridgePlatform, readonly accessory: AqaraPlatformAccessory) {
const { deviceInfo } = this.accessory.context;
const { Characteristic, Service } = this.platform;
this.accessory
.getService(Service.AccessoryInformation)!
.setCharacteristic(Characteristic.Manufacturer, 'Aqara')
.setCharacteristic(Characteristic.Model, deviceInfo.model)
.setCharacteristic(Characteristic.SerialNumber, deviceInfo.did.split('.').pop()!.toUpperCase());
}

protected generateServices<T extends typeof Service>(services: T[]) {
this.services = services.map(
service => this.accessory.getService(service as any) || this.accessory.addService(service as any),
);
}

protected getResourceValue(resourceId: string) {
return this.platform.aqaraApi.getResourceValue(this.accessory.context.deviceInfo.did, [resourceId]);
}

protected setResourceValue(resourceId: string, value: string) {
return this.platform.aqaraApi.setResourceValue(this.accessory.context.deviceInfo.did, [{ resourceId, value }]);
}
}
25 changes: 25 additions & 0 deletions src/accessories/lumi.airer.acn02.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { BaseAccessory } from './base';

export class LumiAirerAcn02 extends BaseAccessory {

constructor(platform, accessory) {
super(platform, accessory);

this.init();

}

init() {
const { Lightbulb, WindowCovering } = this.platform.Service;
this.generateServices([Lightbulb, WindowCovering]);

this.services[0]
.getCharacteristic(this.platform.Characteristic.On)
.onGet(this.getLightbulbOn.bind(this));
}

getLightbulbOn() {
this.getResourceValue('14.2.85');
return true;
}
}
15 changes: 15 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,19 @@ export class AqaraApi {
}
return result;
}

getResourceValue(subjectId: string, resourceIds: string[]) {
return this.request('query.resource.value', {
subjectId,
resourceIds,
});
}

setResourceValue(subjectId: string, resources: Array<{ resourceId: string; value: string }>) {
return this.request('write.resource.device', {
subjectId,
resources,
});
}

}
11 changes: 7 additions & 4 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
type AqaraPlatformAccessory = import('homebridge').PlatformAccessory<AqaraPlatformAccessoryContext>;

interface AqaraPlatformAccessoryContext {
deviceInfo: Aqara.DeviceInfo;
}

declare namespace Aqara {
interface AppConfig {
appId: string;
Expand Down Expand Up @@ -41,16 +47,13 @@ declare namespace Aqara {
createTime: number;
/** 更新时间 */
updateTime: number;
/** 设备信息列表 */
subInfos: any[];
/** 设备数量 */
totalCount: number;
/** 设备名称 */
deviceName: string;
}

interface QueryDeviceInfoResponse {
data: DeviceInfo[];
/** 设备数量 */
totalCount: number;
}
}

0 comments on commit 910e70a

Please sign in to comment.