Skip to content

Commit

Permalink
fix: some sync issues
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 8, 2023
1 parent 2e71bd3 commit c169b84
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 62 deletions.
30 changes: 30 additions & 0 deletions deno.lock

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

56 changes: 23 additions & 33 deletions reactive_home/src/composeables/mapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,22 @@ export function mapState(config: MapStateConfig) {

// Detect human overwrite
watch(
() => entityHassState.value,
(newValue, oldValue) => {
[
() => entityHassState.computedBool,
() => entityHassState.computedLightPct,
],
([newValue, newLightValue], [oldValue, oldLightValue]) => {
// Skip initial state
if (!oldValue) {
return;
}

if (!newValue?.state) {
if (!oldValue || oldLightValue < 0 || newLightValue < 0) {
return;
}

if (stringBoolToBool(newValue.state!) !== unref(config.desiredState)) {
if (newValue !== unref(config.desiredState)) {
if (config.debug) {
console.log(
console.error(
`State was overwritten by a humen for ${
config.entity
}. Expected ${stringBoolToBool(newValue.state!)} and got ${unref(
}. Expected ${stringBoolToBool(newValue)} and got ${unref(
config.desiredState
)}`
);
Expand All @@ -73,21 +72,15 @@ export function mapState(config: MapStateConfig) {
}
}

if (
config.desiredBrightness &&
newValue.attributes?.brightness &&
stringBoolToBool(newValue.state!)
) {
if (
unref(config.desiredBrightness) !== newValue.attributes.brightness
) {
if (config.desiredBrightness && newValue) {
if (unref(config.desiredBrightness) !== newLightValue) {
if (config.debug) {
console.log(
console.error(
`Brightness was overwritten by a humen for ${
config.entity
}. Expected ${unref(config.desiredBrightness)} and got ${
newValue.attributes.brightness
}`
}. Expected ${unref(
config.desiredBrightness
)} and got ${newLightValue}`
);
}
if (overwriteBrightnessEntityHassState) {
Expand Down Expand Up @@ -191,17 +184,14 @@ export function mapState(config: MapStateConfig) {
() => unref(config.desiredState),
() => entityHassState.bool,
],
() => {
([stateOutOfSync, brightnessOutOfSync, desiredState, bool]) => {
if (config.debug) {
console.log(
`debug(${config.entity}): stateOutOfSync=${
stateOutOfSync.value
} brightnessOutOfSync=${
brightnessOutOfSync.value
} desiredState=${stringBoolToBool(
unref(config.desiredState)
)} entityHassState=${entityHassState.bool}`
);
console.log(`debug(${config.entity}): `, {
stateOutOfSync,
brightnessOutOfSync,
desiredState,
bool,
});
}
}
);
Expand Down Expand Up @@ -230,7 +220,7 @@ export function mapState(config: MapStateConfig) {
console.log(
`entityHassState.lightPct(${config.entity}) = ${unref(
config.desiredBrightness
)}`
)} (current === ${entityHassState.lightPct})`
);
}
if (config.desiredBrightness) {
Expand Down
43 changes: 27 additions & 16 deletions reactive_home/src/composeables/useLight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useState } from "./useState.ts";
import { ref, watch } from "../dep.ts";
import { ref, watch, computed } from "../dep.ts";
import { extendRef, watchDebounced } from "../dep.ts";
import { auth, createApiCall } from "../hass/connection.ts";
import { stringBoolToBool } from "../lib/util.ts";
Expand All @@ -12,7 +12,9 @@ export function useLight(entity: string, debug = false) {
async function set(newValue: boolean, light = 255) {
const payload = JSON.stringify({
entity_id: entity,
...(newValue ? { brightness: light } : {}),
...(newValue
? { brightness: light < 0 ? (newValue ? 255 : 0) : light + 1 }
: {}),
});
const callServiceUrl = createApiCall(
`/api/services/${state.value?.entity_id?.split(".").at(0)}/turn_${
Expand All @@ -36,7 +38,10 @@ export function useLight(entity: string, debug = false) {
}

const bool: Ref<boolean> = ref(false);
const lightPct = ref(1);
const lightPct = ref(-1);

const computedLightPct = computed(() => lightPct.value);
const computedBool = computed(() => bool.value);

let skipNextWatch = false;

Expand All @@ -49,33 +54,39 @@ export function useLight(entity: string, debug = false) {
(newValue?.attributes?.brightness &&
newValue?.attributes.brightness !== lightPct.value)
) {
if (newValue.state) {
if (newValue.state && bool.value !== stringBoolToBool(newValue.state)) {
skipNextWatch = true;
// console.log("New bool", stringBoolToBool(newValue.state));
bool.value = stringBoolToBool(newValue.state);
}
if (newValue.attributes?.brightness) {
if (
newValue.attributes?.brightness &&
lightPct.value !== newValue.attributes.brightness
) {
skipNextWatch = true;
// console.log("New lightPct", newValue.attributes.brightness);
lightPct.value = newValue.attributes.brightness;
}
}
}
);

watchDebounced(
[() => bool.value, () => lightPct.value],
([boolVal, lightVal]: [boolean, number]) => {
if (skipNextWatch) {
skipNextWatch = false;
return;
}
set(boolVal as boolean, lightVal as number);
},
{ debounce: 100 }
);
watch([computedBool, computedLightPct], ([boolVal, lightVal]) => {
if (skipNextWatch) {
// console.log("skipNextWatch was true");
skipNextWatch = false;
return;
} else {
// console.log("No skipNextWatch", { boolVal, lightVal });
}
set(boolVal as boolean, lightVal as number);
});

const extend = {
bool,
lightPct,
computedLightPct,
computedBool,
};

return extendRef(state, extend) as typeof state &
Expand Down
8 changes: 6 additions & 2 deletions reactive_home/src/dep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export {
onScopeDispose,
} from "npm:@vue/[email protected]";

export type { Ref, UnwrapNestedRefs, ComputedRef } from "npm:@vue/[email protected]";
export type {
Ref,
UnwrapNestedRefs,
ComputedRef,
} from "npm:@vue/[email protected]";

export {
useNow,
Expand All @@ -20,7 +24,7 @@ export {
watchDebounced,
} from "npm:@vueuse/[email protected]";

export { watch } from "npm:@vue-reactivity/watch@0.2.0";
export { watch } from "npm:@vue/runtime-core@3.2.45";

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

Expand Down
1 change: 1 addition & 0 deletions test-modules/deno.lock

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

31 changes: 20 additions & 11 deletions test-modules/script.wohnzimmer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
useBrightness,
} from "../reactive_home/src/mod.ts";

// const epoWohnzimmer = useState("input_boolean.wohnzimmer_simulate_epo");
const epoWohnzimmer = useState("binary_sensor.occupancy");
const epoWohnzimmer = useState("input_boolean.wohnzimmer_simulate_epo");
// const epoWohnzimmer = useState("binary_sensor.occupancy");
const motionWohnzimmer = useState("binary_sensor.0x00124b00250907a0_occupancy");
const motionWohnzimmerOnFor20Sec = useStateForDuration(
"binary_sensor.0x00124b00250907a0_occupancy",
Expand Down Expand Up @@ -44,18 +44,13 @@ const lightShouldBeOn = computed(() => {
return motionWohnzimmer.value?.state === "on";
});

// mapBooleanToLight(lightShouldBeOn, "light.0x8cf681fffe01c005", {
// reSyncAfter: 1000 * 60 * 60,
// debug: true,
// });

// watch(lightShouldBeOn, (newValue) => console.log(newValue));

const brightness = useBrightness();

watch(
() => brightness.value,
(newBrightness) => console.log({ newBrightness }),
[() => lightShouldBeOn.value, () => brightness.value],
([newValue, newBrightness]) => {
console.log({ newValue, newBrightness });
},
{ immediate: true }
);

Expand All @@ -71,3 +66,17 @@ mapState({
"input_text.wohnzimmer_disableauto_state_brightness"
),
});

// mapState({
// entity: "light.kueche",
// desiredState: lightShouldBeOn,
// desiredBrightness: brightness,
// debug: true,
// overwriteEntity: "input_boolean.kuche_deckenlicht_disableauto_state",
// overwriteBrightnessEntity:
// "input_boolean.kuche_deckenlicht_disableauto_brightness",
// overwriteReset: useState("input_text.wohnzimmer_disableauto_statetime"),
// overwriteBrightnessReset: useState(
// "input_text.wohnzimmer_disableauto_state_brightness"
// ),
// });

0 comments on commit c169b84

Please sign in to comment.