Skip to content

Commit

Permalink
fix: useLightMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 18, 2023
1 parent 3280ae8 commit e9f361f
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 16 deletions.
23 changes: 16 additions & 7 deletions reactive_home/src/composeables/useLightMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function useLightMapping({
if (isDisabled?.value) {
return -1;
}
if (entity.value !== unref(expectedValue)) {
return now.value.getTime() - entity.lastChanged.getTime();
if (
entity.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 -1;
});
Expand All @@ -106,9 +109,11 @@ export function useLightMapping({
if (
entity.value &&
expectedBrightness &&
entity.brightness !== unref(expectedBrightness)
entity.brightness !== unref(expectedBrightness) &&
/** Just use this as clock source, not for the actual calculation, because it might be behind */
now.value
) {
return now.value.getTime() - entity.lastChanged.getTime();
return new Date().getTime() - entity.lastChanged.getTime();
}
return -1;
});
Expand Down Expand Up @@ -139,7 +144,8 @@ export function useLightMapping({
}
entity.brightness = expectedBrightnessValue;
}
}
},
{ immediate: true }
);
}

Expand Down Expand Up @@ -172,7 +178,6 @@ export function parseAutoEnableTimeFactory(
if (typeof value === "object") {
value = value.state;
}
console.log({ value, input });
return parse(value, "ms");
});
}
Expand All @@ -194,13 +199,17 @@ export function shouldReEnableSinceFactory(
return -1;
}

/** Just use this as clock source, not for the actual calculation, because it might be behind */
const now = unref(nowSource);
if (!now) {
return -1;
}

const resetTime = unref(resetTimeSource);

const lastChanged = entity.lastChanged;

const diffDate = subMilliseconds(now, resetTime);
const diffDate = subMilliseconds(new Date(), resetTime);

if (lastChanged < diffDate) {
return diffDate.getTime() - lastChanged.getTime();
Expand Down
18 changes: 9 additions & 9 deletions test-modules/deno.lock

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

89 changes: 89 additions & 0 deletions test-modules/script.arbeitszimmer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {
computed,
useLightMapping,
useNewLight,
useAsyncState,
useNewBoolean,
useNewStateForDuration,
} from "../mod.ts";

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

const epo = useNewBoolean(await useAsyncState("binary_sensor.occupancy_2"));

const motionEntrenceState = await useAsyncState(
"binary_sensor.arbeitszimmer_eingang_bewegungsmelder_occupancy"
);

const motionEntrence = useNewBoolean(motionEntrenceState);

const motionTableState = await useAsyncState(
"binary_sensor.arbeitszimmer_schreibtisch_bewegungsmelder_occupancy"
);

const motionTable = useNewBoolean(motionTableState);

const motionEntrenceOnFor40Sec = useNewStateForDuration(
motionEntrenceState,
"on",
40,
"off"
);

const motionTableOnFor40Sec = useNewStateForDuration(
motionTableState,
"on",
40,
"off"
);

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

// EP1 detects something, there is definitely someone
if (epo.value) {
return true;
}

const pirDetected = motionEntrence.value || motionTable.value;

const pir40s = motionEntrenceOnFor40Sec.value || motionTableOnFor40Sec.value;

// If the motion sensor is on for over 20s, but the EP1 not, then turn the lights off
if (pirDetected && !(pir40s && !epo.value)) {
return false;
}

return pirDetected;
});

const isDisabled = useNewBoolean(
await useAsyncState("input_boolean.arbeitszimmer_disableauto_state")
);

const autoEnableTime = await useAsyncState(
"input_text.arbeitszimmer_disableauto_state_time"
);

const kugel1 = useNewLight(await useAsyncState("light.0x2c1165fffed484a7"));

console.log({ ss: lightShouldBeOn.value, kugel1: kugel1.value });

useLightMapping({
entity: kugel1,
expectedValue: lightShouldBeOn,
isDisabled,
autoEnableTime,
debug: true,
});

useLightMapping({
entity: useNewLight(await useAsyncState("light.0xdc8e95fffee16f80")),
expectedValue: lightShouldBeOn,
isDisabled,
autoEnableTime,
debug: true,
});

0 comments on commit e9f361f

Please sign in to comment.