From 475357e38bdc3983e8e0b660f5424c9bfdda76df Mon Sep 17 00:00:00 2001 From: Remo Vetere Date: Mon, 29 Jan 2024 22:26:41 +0100 Subject: [PATCH] Add v1.2.1 --- package.json | 2 +- src/platform.ts | 22 +++++++++++++++------- src/platformAccessory.ts | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 8812dd3..0daba63 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/platform.ts b/src/platform.ts index 5e96510..b67e42f 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -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); } } diff --git a/src/platformAccessory.ts b/src/platformAccessory.ts index 15f19d7..8cb9bc3 100644 --- a/src/platformAccessory.ts +++ b/src/platformAccessory.ts @@ -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 @@ -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. @@ -81,6 +98,7 @@ export class PlatformSwitchAccessory { value ); this.states.On = value as boolean; + await sleep(300); } /**