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

fix(lights): auto adjust min and max mireds when value is out of bounds #346

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
25 changes: 17 additions & 8 deletions packages/backend/src/matter/behaviors/color-control-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ import { applyPatchState } from "../../utils/apply-patch-state.js";

const FeaturedBase = Base.with("ColorTemperature", "HueSaturation");

export interface ColorControlConfig {
expandMinMaxTemperature?: boolean;
}

export class ColorControlServerBase extends FeaturedBase {
declare state: ColorControlServerBase.State;

Expand All @@ -25,8 +29,13 @@ export class ColorControlServerBase extends FeaturedBase {

private update(entity: HomeAssistantEntityInformation) {
const attributes = entity.state.attributes as LightDeviceAttributes;
const minKelvin = attributes.min_color_temp_kelvin ?? 1500;
const maxKelvin = attributes.max_color_temp_kelvin ?? 8000;
const currentKelvin = attributes.color_temp_kelvin;
let minKelvin = attributes.min_color_temp_kelvin ?? 1500;
let maxKelvin = attributes.max_color_temp_kelvin ?? 8000;
if (this.state.config?.expandMinMaxTemperature == true) {
minKelvin = Math.min(minKelvin, currentKelvin ?? Infinity);
maxKelvin = Math.max(maxKelvin, currentKelvin ?? -Infinity);
}
const [hue, saturation] = this.getMatterColor(entity.state) ?? [0, 0];
applyPatchState(this.state, {
colorMode: this.getMatterColorMode(attributes.color_mode),
Expand All @@ -49,12 +58,10 @@ export class ColorControlServerBase extends FeaturedBase {
),
startUpColorTemperatureMireds:
ColorConverter.temperatureKelvinToMireds(
attributes.color_temp_kelvin ?? maxKelvin,
currentKelvin ?? maxKelvin,
),
colorTemperatureMireds: attributes.color_temp_kelvin
? ColorConverter.temperatureKelvinToMireds(
attributes.color_temp_kelvin,
)
colorTemperatureMireds: currentKelvin
? ColorConverter.temperatureKelvinToMireds(currentKelvin)
: undefined,
}
: {}),
Expand Down Expand Up @@ -153,7 +160,9 @@ export class ColorControlServerBase extends FeaturedBase {
}

export namespace ColorControlServerBase {
export class State extends FeaturedBase.State {}
export class State extends FeaturedBase.State {
config?: ColorControlConfig;
}
}

export class ColorControlServer extends ColorControlServerBase.for(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ export const ColorTemperatureLightType = Device.with(
HomeAssistantEntityBehavior,
LightOnOffServer,
LightLevelControlServer,
ColorControlServer.with("ColorTemperature"),
ColorControlServer.with("ColorTemperature").set({
config: {
expandMinMaxTemperature: true,
},
}),
);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import { LightLevelControlServer } from "./light-level-control-server.js";

export const ExtendedColorLightType = (supportsTemperature: boolean) => {
const colorControlServer = supportsTemperature
? ColorControlServer.with("HueSaturation", "ColorTemperature")
? ColorControlServer.with("HueSaturation", "ColorTemperature").set({
config: {
expandMinMaxTemperature: true,
},
})
: ColorControlServer.with("HueSaturation");
return Device.with(
IdentifyServer,
Expand Down
Loading