Skip to content

Commit

Permalink
fix(lights): preserve last level control state
Browse files Browse the repository at this point in the history
fixes #217
  • Loading branch information
t0bst4r committed Nov 30, 2024
1 parent b3991e2 commit 6991d9b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ export class LevelControlServer extends Base {

private update({ state }: HomeAssistantEntityInformation) {
applyPatchState(this.state, {
currentLevel: this.validValue(this.state.config.getValue(state)) ?? null,
currentLevel:
this.validValue(this.state.config.getValue(state)) ??
this.state.currentLevel,
minLevel:
this.validValue(this.state.config.getMinValue?.(state)) ?? undefined,
maxLevel:
Expand Down
15 changes: 15 additions & 0 deletions packages/backend/src/matter/behaviors/on-off-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "@home-assistant-matter-hub/common";
import { HomeAssistantEntityBehavior } from "../custom-behaviors/home-assistant-entity-behavior.js";
import { applyPatchState } from "../../utils/apply-patch-state.js";
import { LevelControlServer } from "./level-control-server.js";

export interface OnOffConfig {
isOn?: (state: HomeAssistantEntityState) => boolean;
Expand Down Expand Up @@ -33,6 +34,20 @@ export class OnOffServer extends Base {
}

override async on() {
if (this.agent.has(LevelControlServer)) {
const levelControl = this.agent.get(LevelControlServer);
const currentLevel = levelControl.state.currentLevel;
if (currentLevel != undefined) {
await levelControl.moveToLevelWithOnOff({
level: currentLevel,
transitionTime: null,
optionsMask: {},
optionsOverride: {},
});
return;
}
}

const homeAssistant = this.agent.get(HomeAssistantEntityBehavior);
if (this.isOn(homeAssistant.entity.state)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const lightLevelControlConfig: LevelControlConfig = {
if (brightness != null) {
return (brightness / 255) * 254;
}
return 0;
return null;
},
moveToLevel: {
action: "light.turn_on",
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/clusters/level-control.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface LevelControlClusterState {
currentLevel: number;
currentLevel: number | null;
minLevel?: number;
maxLevel?: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ export interface LevelControlStateProps {

export const LevelControlState = ({ state }: LevelControlStateProps) => {
const percentage = useMemo(() => {
if (state.currentLevel == null) {
return null;
}
const min = state.minLevel ?? 0;
const max = state.maxLevel ?? 254;
return (state.currentLevel - min) / (max - min);
}, [state]);
return <>{Math.round(percentage * 100)} %</>;

if (percentage == null) {
return <></>;
} else {
return <>{Math.round(percentage * 100)} %</>;
}
};

0 comments on commit 6991d9b

Please sign in to comment.