Skip to content

Commit

Permalink
fix: infinity sync bug
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 18, 2023
1 parent 3b4b077 commit f2efd24
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 6 deletions.
4 changes: 4 additions & 0 deletions reactive_home/src/composeables/useLightMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function useLightMapping({
if (debug) {
console.log(`automation_toggle(${newEntityState.entity_id}): value`);
}
isDisabled.lastChanged = new Date();
isDisabled.value = true;
}

Expand All @@ -42,6 +43,7 @@ export function useLightMapping({
`automation_toggle(${newEntityState.entity_id}): brightness`
);
}
isDisabledBrightness.lastChanged = new Date();
isDisabledBrightness.value = true;
}
});
Expand Down Expand Up @@ -75,6 +77,7 @@ export function useLightMapping({
`isDisabled(${entity.entity_id}): reset time=${newShouldReEnableSince}`
);
}
isDisabled.lastChanged = new Date();
isDisabled.value = false;
}
if (newShouldReEnableSinceBrightness >= 0 && isDisabledBrightness) {
Expand All @@ -83,6 +86,7 @@ export function useLightMapping({
`isDisabledBrightness(${entity.entity_id}): reset time=${newShouldReEnableSinceBrightness}`
);
}
isDisabledBrightness.lastChanged = new Date();
isDisabledBrightness.value = false;
}
},
Expand Down
14 changes: 12 additions & 2 deletions reactive_home/src/composeables/useNewBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
watchPausable,
extendRef,
computed,
nextTick,
} from "../dep.ts";
import type { MessageBase, HassEntity, UnwrapNestedRefs } from "../dep.ts";
import { connection } from "../hass/connection.ts";
Expand Down Expand Up @@ -50,6 +51,7 @@ export function useNewBoolean(state: FullfilledUseState, debug = false) {
const { pause, resume } = watchPausable(
localValue,
(newLocalValue: typeof localValue.value) => {
console.log({ newLocalValue, entity: state.value.entity_id });
updateHASSState(newLocalValue);
}
);
Expand All @@ -59,7 +61,7 @@ export function useNewBoolean(state: FullfilledUseState, debug = false) {
// Incoming state changes from hass
watch(
() => state.value,
(newEntityState) => {
async (newEntityState) => {
const contextIndex = skipContexts.findIndex(
(value) => value === newEntityState.context.id
);
Expand All @@ -71,12 +73,20 @@ export function useNewBoolean(state: FullfilledUseState, debug = false) {
pause();
localValue.value = stringBoolToBool(newEntityState.state);
lastChanged.value = newEntityState.last_changed;
await nextTick();
resume();
}
);

const extendObject = {
lastChanged: computed(() => new Date(lastChanged.value)),
lastChanged: computed({
get() {
return new Date(lastChanged.value);
},
set(newValue) {
lastChanged.value = newValue.toISOString();
},
}),
};

return extendRef(localValue, extendObject) as typeof localValue &
Expand Down
12 changes: 10 additions & 2 deletions reactive_home/src/composeables/useNewLight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { FullfilledUseState } from "./useState.ts";
import { useDebounceFn, reactive, watch, watchPausable } from "../dep.ts";
import {
useDebounceFn,
reactive,
watch,
watchPausable,
nextTick,
} from "../dep.ts";
import type { MessageBase, HassEntity } from "../dep.ts";
import { connection } from "../hass/connection.ts";
import { stringBoolToBool } from "../lib/util.ts";
Expand Down Expand Up @@ -73,9 +79,10 @@ export function useNewLight(state: FullfilledUseState, debug = false) {
// Incoming state changes from hass
watch(
() => state.value,
(newEntityState) => {
async (newEntityState) => {
pause();
localValues.lastChanged = new Date(newEntityState.last_changed);
await nextTick();
resume();

const contextIndex = skipContexts.findIndex(
Expand All @@ -89,6 +96,7 @@ export function useNewLight(state: FullfilledUseState, debug = false) {
pause();
localValues.value = stringBoolToBool(newEntityState.state);
localValues.brightness = getBrightnessFromAttribute(newEntityState);
await nextTick();
resume();
}
);
Expand Down
2 changes: 1 addition & 1 deletion reactive_home/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export {
watchPausable,
} from "npm:@vueuse/[email protected]";

export { watch } from "npm:@vue/[email protected]";
export { watch, nextTick } from "npm:@vue/[email protected]";

export { config as dotenvConfig } from "https://deno.land/x/[email protected]/mod.ts";

Expand Down
17 changes: 17 additions & 0 deletions test-modules/lx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useState, computed } from "../mod.ts";

/**
*
* @param entity Entity id
* @returns Computed lumen float value
*/
export function useLumen(entity: string) {
const state = useState(entity);

return computed(() => {
if (!state.value) {
return null;
}
return parseFloat(state.value.state);
});
}
5 changes: 4 additions & 1 deletion test-modules/script.arbeitszimmer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {

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

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

const motionEntrenceState = await useAsyncState(
"binary_sensor.arbeitszimmer_eingang_bewegungsmelder_occupancy"
Expand Down
69 changes: 69 additions & 0 deletions test-modules/script.flur.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
computed,
useBrightness,
useAsyncState,
useNewBoolean,
useNewLight,
useLightMapping,
watch,
} from "../mod.ts";
import { useLumen } from "./lx.ts";

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

const motion1 = await useAsyncState(
"binary_sensor.0x00124b0025091116_occupancy"
);
const motion2 = await useAsyncState(
"binary_sensor.0x00124b0025090f28_occupancy"
);

const lx = useLumen("sensor.0xe0798dfffebf23d7_illuminance_lux");

const lightState = await useAsyncState("light.flur_deckenlicht");

const state = useNewLight(lightState, true);

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

return motion1.value?.state === "on" || motion2.value?.state === "on";
});

watch(
lightShouldBeOn,
(lightShouldBeOn) => {
console.log({ lightShouldBeOn });
},
{ immediate: true }
);

const brightness = useBrightness({
maxBrigthness: 140,
});

useLightMapping({
entity: state,
expectedValue: lightShouldBeOn,
expectedBrightness: brightness,
isDisabled: useNewBoolean(
await useAsyncState("input_boolean.flur_disableauto_state"),
true
),
isDisabledBrightness: useNewBoolean(
await useAsyncState("input_boolean.flur_disableauto_brightness"),
true
),
autoEnableTime: await useAsyncState("input_text.flur_disableauto_state_time"),
autoEnableTimeBrightness: await useAsyncState(
"input_text.flur_disableauto_brightness_time"
),
debug: true,
});

0 comments on commit f2efd24

Please sign in to comment.