From 87de7e0742420bdd4b9953dd5e015b5ddb2602f0 Mon Sep 17 00:00:00 2001 From: Shaquu Date: Sun, 23 Jun 2024 01:22:07 +0200 Subject: [PATCH 1/6] AdaptiveLightingController on update should provide some data without the need to build it yourself --- .../controller/AdaptiveLightingController.ts | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/lib/controller/AdaptiveLightingController.ts b/src/lib/controller/AdaptiveLightingController.ts index bb901cb67..e4489f5ea 100644 --- a/src/lib/controller/AdaptiveLightingController.ts +++ b/src/lib/controller/AdaptiveLightingController.ts @@ -292,6 +292,18 @@ export const enum AdaptiveLightingControllerEvents { DISABLED = "disable", } +/** + * see {@link ActiveAdaptiveLightingTransition}. + */ +export interface AdaptiveLightingControllerUpdate { + transitionStartMillis: number; + timeMillisOffset: number; + transitionCurve: AdaptiveLightingTransitionCurveEntry[]; + brightnessAdjustmentRange: BrightnessAdjustmentMultiplierRange; + updateInterval: number, + notifyIntervalThreshold: number; +} + export declare interface AdaptiveLightingController { /** * See {@link AdaptiveLightingControllerEvents.UPDATE} @@ -299,7 +311,7 @@ export declare interface AdaptiveLightingController { * @param event * @param listener */ - on(event: "update", listener: () => void): this; + on(event: "update", listener: (update: AdaptiveLightingControllerUpdate) => void): this; /** * See {@link AdaptiveLightingControllerEvents.DISABLED} * @@ -308,7 +320,7 @@ export declare interface AdaptiveLightingController { */ on(event: "disable", listener: () => void): this; - emit(event: "update"): boolean; + emit(event: "update", update: AdaptiveLightingControllerUpdate): boolean; emit(event: "disable"): boolean; } @@ -606,7 +618,20 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ if (this.mode === AdaptiveLightingControllerMode.AUTOMATIC) { this.scheduleNextUpdate(); } else if (this.mode === AdaptiveLightingControllerMode.MANUAL) { - this.emit(AdaptiveLightingControllerEvents.UPDATE); + if (!this.activeTransition) { + throw new Error("There is no active transition!"); + } + + const update: AdaptiveLightingControllerUpdate = { + transitionStartMillis: this.activeTransition.transitionStartMillis, + timeMillisOffset: this.activeTransition.timeMillisOffset, + transitionCurve: this.activeTransition.transitionCurve, + brightnessAdjustmentRange: this.activeTransition.brightnessAdjustmentRange, + updateInterval: this.activeTransition.updateInterval, + notifyIntervalThreshold: this.activeTransition.notifyIntervalThreshold, + } + + this.emit(AdaptiveLightingControllerEvents.UPDATE, update); } else { throw new Error("Unsupported adaptive lighting controller mode: " + this.mode); } From 958853244e3280f6363a2ad7a605e20ff91f3c6f Mon Sep 17 00:00:00 2001 From: Shaquu Date: Sun, 23 Jun 2024 01:22:38 +0200 Subject: [PATCH 2/6] Fix TypeError --- .../controller/AdaptiveLightingController.ts | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/src/lib/controller/AdaptiveLightingController.ts b/src/lib/controller/AdaptiveLightingController.ts index e4489f5ea..721c90dc9 100644 --- a/src/lib/controller/AdaptiveLightingController.ts +++ b/src/lib/controller/AdaptiveLightingController.ts @@ -504,15 +504,10 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ } if (this.activeTransition) { - this.colorTemperatureCharacteristic!.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener); - this.brightnessCharacteristic!.removeListener(CharacteristicEventTypes.CHANGE, this.adjustmentFactorChangedListener); - - if (this.hueCharacteristic) { - this.hueCharacteristic.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener); - } - if (this.saturationCharacteristic) { - this.saturationCharacteristic.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener); - } + this.colorTemperatureCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener); + this.brightnessCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.adjustmentFactorChangedListener); + this.hueCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener); + this.saturationCharacteristic?.removeListener(CharacteristicEventTypes.CHANGE, this.characteristicManualWrittenChangeListener); this.activeTransition = undefined; @@ -532,7 +527,7 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ this.didRunFirstInitializationStep = false; - this.activeTransitionCount!.sendEventNotification(0); + this.activeTransitionCount?.sendEventNotification(0); debug("[%s] Disabling adaptive lighting", this.lightbulb.displayName); } @@ -609,10 +604,12 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ // ----------- PUBLIC API END ----------- private handleActiveTransitionUpdated(calledFromDeserializer: boolean = false): void { - if (!calledFromDeserializer) { - this.activeTransitionCount!.sendEventNotification(1); - } else { - this.activeTransitionCount!.value = 1; + if (this.activeTransitionCount) { + if (!calledFromDeserializer) { + this.activeTransitionCount.sendEventNotification(1); + } else { + this.activeTransitionCount.value = 1; + } } if (this.mode === AdaptiveLightingControllerMode.AUTOMATIC) { @@ -823,7 +820,7 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ this.activeTransition.brightnessAdjustmentRange.minBrightnessValue, Math.min( this.activeTransition.brightnessAdjustmentRange.maxBrightnessValue, - this.brightnessCharacteristic!.value as number // get handler is not called for optimal performance + this.brightnessCharacteristic?.value as number // get handler is not called for optimal performance ) ); @@ -867,7 +864,7 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ this.hueCharacteristic.value = color.hue; } - this.colorTemperatureCharacteristic!.handleSetRequest(temperature, undefined, context).catch(reason => { // reason is HAPStatus code + this.colorTemperatureCharacteristic?.handleSetRequest(temperature, undefined, context).catch(reason => { // reason is HAPStatus code debug("[%s] Failed to next adaptive lighting transition point: %d", this.lightbulb.displayName, reason); }); @@ -887,7 +884,7 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ }; if (this.lastNotifiedTemperatureValue !== temperature) { - this.colorTemperatureCharacteristic!.sendEventNotification(temperature, eventContext); + this.colorTemperatureCharacteristic?.sendEventNotification(temperature, eventContext); this.lastNotifiedTemperatureValue = temperature; } if (this.saturationCharacteristic && this.lastNotifiedSaturationValue !== color.saturation) { @@ -1011,8 +1008,8 @@ export class AdaptiveLightingController extends EventEmitter implements Serializ } private handleSupportedTransitionConfigurationRead(): string { - const brightnessIID = this.lightbulb!.getCharacteristic(Characteristic.Brightness).iid; - const temperatureIID = this.lightbulb!.getCharacteristic(Characteristic.ColorTemperature).iid; + const brightnessIID = this.lightbulb?.getCharacteristic(Characteristic.Brightness).iid; + const temperatureIID = this.lightbulb?.getCharacteristic(Characteristic.ColorTemperature).iid; assert(brightnessIID, "iid for brightness characteristic is undefined"); assert(temperatureIID, "iid for temperature characteristic is undefined"); From 4e746a93e98027a7b02721a16a8729958825b8d1 Mon Sep 17 00:00:00 2001 From: Shaquu Date: Sun, 23 Jun 2024 22:04:39 +0200 Subject: [PATCH 3/6] Fixed linting and test issues --- src/lib/HAPServer.spec.ts | 1 - src/lib/controller/AdaptiveLightingController.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/lib/HAPServer.spec.ts b/src/lib/HAPServer.spec.ts index 641edd3c3..3d30ec300 100644 --- a/src/lib/HAPServer.spec.ts +++ b/src/lib/HAPServer.spec.ts @@ -618,7 +618,6 @@ describe(IsKnownHAPStatusError, () => { .filter(error => error !== 0); // filter out HAPStatus.SUCCESS for (const error of errorValues) { - // @ts-expect-error: type mismatch const result = IsKnownHAPStatusError(error); if (!result) { fail("IsKnownHAPStatusError does not return true for error code " + error); diff --git a/src/lib/controller/AdaptiveLightingController.ts b/src/lib/controller/AdaptiveLightingController.ts index 7231ec447..ea94375b4 100644 --- a/src/lib/controller/AdaptiveLightingController.ts +++ b/src/lib/controller/AdaptiveLightingController.ts @@ -660,7 +660,7 @@ export class AdaptiveLightingController brightnessAdjustmentRange: this.activeTransition.brightnessAdjustmentRange, updateInterval: this.activeTransition.updateInterval, notifyIntervalThreshold: this.activeTransition.notifyIntervalThreshold, - } + }; this.emit(AdaptiveLightingControllerEvents.UPDATE, update); } else { From 60d8dd70fd2463e58c4d231bf30ab2a26e71015f Mon Sep 17 00:00:00 2001 From: Shaquu Date: Sun, 23 Jun 2024 22:27:39 +0200 Subject: [PATCH 4/6] Typedoc fixes --- src/lib/controller/AdaptiveLightingController.ts | 3 ++- typedoc.json | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/controller/AdaptiveLightingController.ts b/src/lib/controller/AdaptiveLightingController.ts index ea94375b4..a7dc5e73a 100644 --- a/src/lib/controller/AdaptiveLightingController.ts +++ b/src/lib/controller/AdaptiveLightingController.ts @@ -302,7 +302,7 @@ export const enum AdaptiveLightingControllerMode { export const enum AdaptiveLightingControllerEvents { /** * This event is called once a HomeKit controller enables Adaptive Lighting - * or a HomeHub sends a updated transition schedule for the next 24 hours. + * or a HomeHub sends an updated transition schedule for the next 24 hours. * This is also called on startup when AdaptiveLighting was previously enabled. */ UPDATE = "update", @@ -315,6 +315,7 @@ export const enum AdaptiveLightingControllerEvents { } /** + * @group Adaptive Lighting * see {@link ActiveAdaptiveLightingTransition}. */ export interface AdaptiveLightingControllerUpdate { diff --git a/typedoc.json b/typedoc.json index 55fc721f5..3d295225d 100644 --- a/typedoc.json +++ b/typedoc.json @@ -1,6 +1,5 @@ { "out": "docs", - "includes": "src", "exclude": [ "src/**/*.spec.ts", "src/test-utils/*" From 484476a158d8f13ece6bab50b1a64fe0d7271520 Mon Sep 17 00:00:00 2001 From: Shaquu Date: Mon, 24 Jun 2024 23:48:32 +0200 Subject: [PATCH 5/6] Updated examples --- src/accessories/Light-AdaptiveLighting_accessory.ts | 11 +++++++++++ src/lib/controller/AdaptiveLightingController.ts | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/src/accessories/Light-AdaptiveLighting_accessory.ts b/src/accessories/Light-AdaptiveLighting_accessory.ts index 42c53221f..2b52cee82 100644 --- a/src/accessories/Light-AdaptiveLighting_accessory.ts +++ b/src/accessories/Light-AdaptiveLighting_accessory.ts @@ -8,6 +8,7 @@ import { Service, uuid, } from ".."; +import util from "util"; /** * This example light gives an example how a light with AdaptiveLighting (in AUTOMATIC mode) support @@ -113,4 +114,14 @@ const adaptiveLightingController = new AdaptiveLightingController(lightbulbServi // look into the docs for more information controllerMode: AdaptiveLightingControllerMode.AUTOMATIC, }); + +// Requires AdaptiveLightingControllerMode.MANUAL to be set as a controllerMode +adaptiveLightingController.on('update', () => { + console.log("Adaptive Lighting updated"); +}).on('update', (update) => { + console.log("Adaptive Lighting schedule updated to " + util.inspect(update)); +}).on('disable', () => { + console.log("Adaptive Lighting disabled"); +}) + accessory.configureController(adaptiveLightingController); diff --git a/src/lib/controller/AdaptiveLightingController.ts b/src/lib/controller/AdaptiveLightingController.ts index a7dc5e73a..a9354ee9f 100644 --- a/src/lib/controller/AdaptiveLightingController.ts +++ b/src/lib/controller/AdaptiveLightingController.ts @@ -334,6 +334,7 @@ export interface AdaptiveLightingControllerUpdate { export declare interface AdaptiveLightingController { /** * See {@link AdaptiveLightingControllerEvents.UPDATE} + * Also see {@link AdaptiveLightingControllerUpdate} * * @param event * @param listener @@ -347,6 +348,9 @@ export declare interface AdaptiveLightingController { */ on(event: "disable", listener: () => void): this; + /** + * See {@link AdaptiveLightingControllerUpdate} + */ emit(event: "update", update: AdaptiveLightingControllerUpdate): boolean; emit(event: "disable"): boolean; } From f3d253205563481e1e20d842c8b3ce61c37cd75d Mon Sep 17 00:00:00 2001 From: Shaquu Date: Tue, 25 Jun 2024 00:10:05 +0200 Subject: [PATCH 6/6] Eslint fixes --- src/accessories/Light-AdaptiveLighting_accessory.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/accessories/Light-AdaptiveLighting_accessory.ts b/src/accessories/Light-AdaptiveLighting_accessory.ts index 2b52cee82..6888c970b 100644 --- a/src/accessories/Light-AdaptiveLighting_accessory.ts +++ b/src/accessories/Light-AdaptiveLighting_accessory.ts @@ -116,12 +116,12 @@ const adaptiveLightingController = new AdaptiveLightingController(lightbulbServi }); // Requires AdaptiveLightingControllerMode.MANUAL to be set as a controllerMode -adaptiveLightingController.on('update', () => { +adaptiveLightingController.on("update", () => { console.log("Adaptive Lighting updated"); -}).on('update', (update) => { +}).on("update", (update) => { console.log("Adaptive Lighting schedule updated to " + util.inspect(update)); -}).on('disable', () => { +}).on("disable", () => { console.log("Adaptive Lighting disabled"); -}) +}); accessory.configureController(adaptiveLightingController);