From fa01c46ea17ce989e280869170b2707c7d069187 Mon Sep 17 00:00:00 2001 From: Johann Richard <189003+johannrichard@users.noreply.github.com> Date: Fri, 4 Dec 2020 20:25:13 +0100 Subject: [PATCH] fix(pir): properly set callback url if one exists - if a callback url is set, don't overwrite, amend --- src/myStromPIRAccessory.ts | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/src/myStromPIRAccessory.ts b/src/myStromPIRAccessory.ts index aea679a..36c83cc 100644 --- a/src/myStromPIRAccessory.ts +++ b/src/myStromPIRAccessory.ts @@ -9,7 +9,7 @@ import { Mutex } from 'async-mutex'; import { DingzDaHomebridgePlatform } from './platform'; import { MyStromDeviceInfo, MyStromPIRReport } from './lib/myStromTypes'; -import { ButtonAction } from './lib/commonTypes'; +import { AccessoryActionUrl, ButtonAction } from './lib/commonTypes'; import { DeviceNotReachableError } from './lib/errors'; import { PlatformEvent } from './lib/platformEventBus'; import { DingzDaBaseAccessory } from './lib/dingzDaBaseAccessory'; @@ -150,11 +150,33 @@ export class MyStromPIRAccessory extends DingzDaBaseAccessory { // Set the callback URL (Override!) retrySlow.execute(() => { - this.platform.setButtonCallbackUrl({ - baseUrl: this.baseUrl, - token: this.device.token, - endpoints: ['pir/generic'], // Buttons need the 'generic' endpoint specifically set - }); + this.getButtonCallbackUrl() + .then((callBackUrl) => { + if (!callBackUrl?.url.includes(this.platform.getCallbackUrl())) { + this.log.warn('Update existing callback URL ->', callBackUrl); + // Set the callback URL (Override!) + this.platform.setButtonCallbackUrl({ + baseUrl: this.baseUrl, + token: this.device.token, + oldUrl: callBackUrl.url, + endpoints: ['pir/generic'], + }); + } else { + this.log.debug('Callback URL already set ->', callBackUrl?.url); + } + }) + .catch(this.handleRequestErrors.bind(this)); + }); + } + + /** + * Returns the callback URL for the device + */ + public async getButtonCallbackUrl(): Promise { + const getCallbackEndpoint = '/api/v1/action/pir/generic'; + this.log.debug('Getting the callback URL -> ', getCallbackEndpoint); + return await this.request.get(getCallbackEndpoint).then((response) => { + return response.data; }); }