Skip to content

Commit

Permalink
fix: prevent the automation to undo users changes in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 20, 2023
1 parent d2bd7d8 commit bfdea92
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 9 deletions.
92 changes: 91 additions & 1 deletion deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions reactive_home/src/composeables/useLightMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
computed,
useNow,
subMilliseconds,
reactive,
} from "../dep.ts";
import type { MaybeRef } from "../lib/types.ts";
import type { HassEntity, Ref } from "../dep.ts";
Expand All @@ -21,9 +22,23 @@ export function useLightMapping({
autoEnableTime,
autoEnableTimeBrightness,
}: UseLightMappingOptions) {
const localEntity = reactive({
value: entity.value,
brightness: entity.brightness,
lastChanged: entity.lastChanged,
});

watch(
() => ({ value: entity.value, brightness: entity.brightness }),
() => ({
value: entity.value,
brightness: entity.brightness,
lastChanged: entity.lastChanged,
}),
(newEntityState, oldEntityState) => {
localEntity.value = newEntityState.value;
localEntity.brightness = newEntityState.brightness;
localEntity.lastChanged = newEntityState.lastChanged;

if (newEntityState.value !== unref(expectedValue) && isDisabled) {
if (debug) {
console.log(`automation_toggle(${entity.entity_id}): value`);
Expand Down Expand Up @@ -109,10 +124,10 @@ export function useLightMapping({
return -1;
}
if (
entity.value !== unref(expectedValue) &&
localEntity.value !== unref(expectedValue) &&
/** Just use this as clock source, not for the actual calculation, because it might be behind */ now.value
) {
return new Date().getTime() - entity.lastChanged.getTime();
return new Date().getTime() - localEntity.lastChanged.getTime();
}
return -1;
});
Expand All @@ -122,13 +137,13 @@ export function useLightMapping({
return -1;
}
if (
entity.value &&
localEntity.value &&
expectedBrightness &&
entity.brightness !== unref(expectedBrightness) &&
localEntity.brightness !== unref(expectedBrightness) &&
/** Just use this as clock source, not for the actual calculation, because it might be behind */
now.value
) {
return new Date().getTime() - entity.lastChanged.getTime();
return new Date().getTime() - localEntity.lastChanged.getTime();
}
return -1;
});
Expand All @@ -143,7 +158,7 @@ export function useLightMapping({
entity.entity_id
}): value time=${newValueTime} expected=${unref(
expectedValue
)} current=${entity.value}`
)} current=${localEntity.value}`
);
}
entity.value = unref(expectedValue);
Expand All @@ -154,7 +169,7 @@ export function useLightMapping({
if (newBrightnessTime >= 0 && expectedBrightnessValue) {
if (debug) {
console.log(
`out_of_sync(${entity.entity_id}): brightness time=${newBrightnessTime} expected=${expectedBrightnessValue} current=${entity.brightness}`
`out_of_sync(${entity.entity_id}): brightness time=${newBrightnessTime} expected=${expectedBrightnessValue} current=${localEntity.brightness}`
);
}
entity.brightness = expectedBrightnessValue;
Expand Down
70 changes: 70 additions & 0 deletions test-modules/script.schalfzimmer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import {
computed,
useBrightness,
useLightMapping,
useAsyncState,
useNewBoolean,
useNewLight,
} from "../mod.ts";
import { whenever } from "../reactive_home/src/dep.ts";

const debug = true;

const motionSensorState = await useAsyncState(
"binary_sensor.0x00124b0022ebcad9_occupancy"
);

const motionSensor = useNewBoolean(motionSensorState, debug);

const sunState = await useAsyncState("sun.sun");

const lightShouldBeOn = computed(() => {
// Sun is above horizon, no light needed
if (sunState.value.state === "above_horizon") {
return false;
}

return motionSensor.value;
});

const brightness = useBrightness({
minBrigthness: 130,
});

const light = useNewLight(
await useAsyncState("light.0x2c1165fffee4eabf"),
debug
);

const isDisabled = useNewBoolean(
await useAsyncState("input_boolean.schlafzimmer_disableauto_state"),
debug
);
const isDisabledBrightness = useNewBoolean(
await useAsyncState("input_boolean.schlafzimmer_disableauto_brightness"),
debug
);

const lightIsOff = computed(
() => !light.value && lightShouldBeOn.value === false
);

useLightMapping({
entity: light,
expectedValue: lightShouldBeOn,
expectedBrightness: brightness,
isDisabled,
isDisabledBrightness,
autoEnableTime: await useAsyncState(
"input_text.schlafzimmer_disableauto_state_time"
),
autoEnableTimeBrightness: await useAsyncState(
"input_text.schlafzimmer_disableauto_brightness_time"
),
debug,
});

whenever(lightIsOff, () => {
isDisabled.value = false;
isDisabledBrightness.value = false;
});

0 comments on commit bfdea92

Please sign in to comment.