Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdaptiveLightingController fix & improvement #1038

Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/lib/HAPServer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
69 changes: 46 additions & 23 deletions src/lib/controller/AdaptiveLightingController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -314,6 +314,19 @@ export const enum AdaptiveLightingControllerEvents {
DISABLED = "disable",
}

/**
* @group Adaptive Lighting
* see {@link ActiveAdaptiveLightingTransition}.
*/
export interface AdaptiveLightingControllerUpdate {
transitionStartMillis: number;
timeMillisOffset: number;
transitionCurve: AdaptiveLightingTransitionCurveEntry[];
brightnessAdjustmentRange: BrightnessAdjustmentMultiplierRange;
updateInterval: number,
notifyIntervalThreshold: number;
}

/**
* @group Adaptive Lighting
*/
Expand All @@ -325,7 +338,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}
*
Expand All @@ -334,7 +347,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;
}

Expand Down Expand Up @@ -526,15 +539,10 @@ export class AdaptiveLightingController
}

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;

Expand All @@ -554,7 +562,7 @@ export class AdaptiveLightingController

this.didRunFirstInitializationStep = false;

this.activeTransitionCount!.sendEventNotification(0);
this.activeTransitionCount?.sendEventNotification(0);

debug("[%s] Disabling adaptive lighting", this.lightbulb.displayName);
}
Expand Down Expand Up @@ -631,16 +639,31 @@ export class AdaptiveLightingController
// ----------- PUBLIC API END -----------

private handleActiveTransitionUpdated(calledFromDeserializer = 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) {
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);
}
Expand Down Expand Up @@ -833,7 +856,7 @@ export class AdaptiveLightingController
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
),
);

Expand Down Expand Up @@ -877,7 +900,7 @@ export class AdaptiveLightingController
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);
});

Expand All @@ -898,7 +921,7 @@ export class AdaptiveLightingController
};

if (this.lastNotifiedTemperatureValue !== temperature) {
this.colorTemperatureCharacteristic!.sendEventNotification(temperature, eventContext);
this.colorTemperatureCharacteristic?.sendEventNotification(temperature, eventContext);
this.lastNotifiedTemperatureValue = temperature;
}
if (this.saturationCharacteristic && this.lastNotifiedSaturationValue !== color.saturation) {
Expand Down Expand Up @@ -1023,8 +1046,8 @@ export class AdaptiveLightingController
}

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");

Expand Down
1 change: 0 additions & 1 deletion typedoc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"out": "docs",
"includes": "src",
"exclude": [
"src/**/*.spec.ts",
"src/test-utils/*"
Expand Down
Loading