Skip to content

Commit

Permalink
Add v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rvetere committed Jan 29, 2024
1 parent 3f0d33f commit 475357e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"displayName": "Homebridge Virtual Button",
"name": "homebridge-virtual-button",
"version": "1.2.0",
"version": "1.2.1",
"description": "A short description about what your plugin does.",
"license": "Apache-2.0",
"repository": {
Expand Down
22 changes: 15 additions & 7 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,25 @@ export class ExampleHomebridgePlatform implements DynamicPlatformPlugin {
request: IncomingMessage,
response: ServerResponse
) {
const [_url, query] =
request.url && request.url.includes("?") ? request.url.split("?") : [];
const nameRaw = query ? query.replace("name=", "") : "";
const instanceName = nameRaw ? decodeURIComponent(nameRaw) : null;
const instance = this.instances.find((i) => {
return i.getName() === instanceName;
});

if (request.url?.includes("/toggle")) {
const [_url, query] = request.url ? request.url.split("?") : [];
const nameRaw = query ? query.replace("name=", "") : "";
const name = decodeURIComponent(nameRaw);
const instance = this.instances.find((i) => {
return i.getName() === name;
});
if (instance) {
await instance.toggleState();
} else {
this.log.error("Could not find instance for name:", name);
this.log.error("Could not find instance for name:", instanceName);
}
} else if (request.url?.includes("/setOn")) {
if (instance) {
await instance.setStateOn();
} else {
this.log.error("Could not find instance for name:", instanceName);
}
}

Expand Down
18 changes: 18 additions & 0 deletions src/platformAccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Service, PlatformAccessory, CharacteristicValue } from "homebridge";

import { ExampleHomebridgePlatform } from "./platform";

const sleep = async (ms: number) =>
new Promise((resolve) => setTimeout(resolve, ms));

/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
Expand Down Expand Up @@ -71,6 +74,20 @@ export class PlatformSwitchAccessory {
);
}

async setStateOn() {
await this.setOn(true);
this.service?.updateCharacteristic(
this.platform.Characteristic.On,
this.states?.On
);
await sleep(1500);
await this.setOn(false);
this.service?.updateCharacteristic(
this.platform.Characteristic.On,
this.states?.On
);
}

/**
* Handle "SET" requests from HomeKit
* These are sent when the user changes the state of an accessory, for example, turning on a Light bulb.
Expand All @@ -81,6 +98,7 @@ export class PlatformSwitchAccessory {
value
);
this.states.On = value as boolean;
await sleep(300);
}

/**
Expand Down

0 comments on commit 475357e

Please sign in to comment.