Skip to content

Commit

Permalink
Test stateless switch
Browse files Browse the repository at this point in the history
  • Loading branch information
OMikkel committed Dec 17, 2023
1 parent 1acca53 commit 202c422
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 28 deletions.
52 changes: 26 additions & 26 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,37 @@
"name": {
"title": "Device Name",
"type": "string",
"placeholder": "Ceiling Light"
"placeholder": "Ceiling Light"
},
"id": {
"title": "Device ID",
"type": "string",
"placeholder": "2300178"
"placeholder": "2300178"
},
"type": {
"title": "Device Type",
"type": "string",
"enum": ["light", "switch"]
},
"metadata": {
"title": "Metadata",
"type": "object",
"properties": {
"manufacturer": {
"title": "Manufacturer",
"type": "string",
"placeholder": "LK IHC"
},
"model": {
"title": "Model",
"type": "string"
},
"serial": {
"title": "Serial Number",
"type": "string"
}
}
}
"type": {
"title": "Device Type",
"type": "string",
"enum": ["light", "switch", "button"]
},
"metadata": {
"title": "Metadata",
"type": "object",
"properties": {
"manufacturer": {
"title": "Manufacturer",
"type": "string",
"placeholder": "LK IHC"
},
"model": {
"title": "Model",
"type": "string"
},
"serial": {
"title": "Serial Number",
"type": "string"
}
}
}
}
}
}
Expand Down
13 changes: 11 additions & 2 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { SOAPRequest } from "./lib/request";
import { PLATFORM_NAME, PLUGIN_NAME } from "./settings";
import { BasePlatformAccessory } from "./platforms/index";
import { StatelessSwitchlatformAccessory } from "./platforms/stateless-switch";

export class HomebridgeIHC implements DynamicPlatformPlugin {
public readonly Service: typeof Service = this.api.hap.Service;
Expand Down Expand Up @@ -63,8 +64,12 @@ export class HomebridgeIHC implements DynamicPlatformPlugin {
// new SwitchPlatformAccessory(this, existingAccessory, request)
// break
// }
if (existingAccessory.context.device.type === "button") {
new StatelessSwitchlatformAccessory(this, existingAccessory, request);
} else {
new BasePlatformAccessory(this, existingAccessory, request);
}

new BasePlatformAccessory(this, existingAccessory, request);
} else {
this.log.info("Adding new accessory:", device.name);

Expand All @@ -82,7 +87,11 @@ export class HomebridgeIHC implements DynamicPlatformPlugin {
// break
// }

new BasePlatformAccessory(this, accessory, request);
if (accessory.context.device.type === "button") {
new StatelessSwitchlatformAccessory(this, accessory, request);
} else {
new BasePlatformAccessory(this, accessory, request);
}

this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [accessory]);
}
Expand Down
60 changes: 60 additions & 0 deletions src/platforms/stateless-switch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Service, PlatformAccessory, CharacteristicValue } from "homebridge";
import { HomebridgeIHC } from "../platform";
import { SOAPRequest } from "../lib/request";

type State = {
value: boolean;
typeString: string;
resourceID: number;
isValueRuntime: boolean;
};

export class StatelessSwitchlatformAccessory {
service: Service;
state: State = {
value: false,
typeString: "",
resourceID: -1,
isValueRuntime: false
};

constructor(
private readonly platform: HomebridgeIHC,
private readonly accessory: PlatformAccessory,
private readonly request: SOAPRequest
) {
this.request = request;
const accessoryService = this.accessory.getService(this.platform.Service.AccessoryInformation)!;
accessoryService.setCharacteristic(this.platform.Characteristic.Manufacturer, this.accessory.context?.metadata?.manufacturer || "LK IHC");
if (this.accessory.context?.metadata?.model) {
accessoryService.setCharacteristic(this.platform.Characteristic.Model, this.accessory.context?.metadata?.model);
}
if (this.accessory.context?.metadata?.serial) {
accessoryService.setCharacteristic(this.platform.Characteristic.SerialNumber, this.accessory.context?.metadata?.serial);
}
this.service = this.accessory.getService(this.platform.Service.StatelessProgrammableSwitch) || this.accessory.addService(this.platform.Service.StatelessProgrammableSwitch);
this.service.setCharacteristic(this.platform.Characteristic.Name, this.accessory.context.device?.name || "Unknown device");

this.service.getCharacteristic(this.platform.Characteristic.ProgrammableSwitchEvent)
.onGet(this.getState.bind(this)); // GET - bind to the `getOn` method below

this.getState();
}

// async setState(value: CharacteristicValue) {
// this.state.value = value as boolean;

// await this.request.setResourceValue(this.state, value as boolean);

// this.platform.log.debug("Set Characteristic On ->", value);
// }

async getState() {
this.platform.log.debug("Triggered GET ProgrammableSwitchEvent");

// set this to a valid value for ProgrammableSwitchEvent
const currentValue = this.platform.Characteristic.ProgrammableSwitchEvent.SINGLE_PRESS;

return currentValue;
}
}

0 comments on commit 202c422

Please sign in to comment.