Skip to content

Commit

Permalink
fix: only detect external change for utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 19, 2023
1 parent 954f14d commit 4d6021b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 70 deletions.
65 changes: 33 additions & 32 deletions reactive_home/src/composeables/useLightMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,43 @@ export function useLightMapping({
autoEnableTime,
autoEnableTimeBrightness,
}: UseLightMappingOptions) {
watch(entity, (newEntityState, oldEntityState) => {
if (newEntityState.value !== unref(expectedValue) && isDisabled) {
if (debug) {
console.log(`automation_toggle(${newEntityState.entity_id}): value`);
}
isDisabled.lastChanged = new Date();
isDisabled.value = true;
} else {
if (debug) {
console.log(
`automation_toggle(${
newEntityState.entity_id
}): value - no toggle newEntityState=${
newEntityState.value
} expectedValue=${unref(expectedValue)} isDisabled=${!!isDisabled}`
);
watch(
() => ({ value: entity.value, brightness: entity.brightness }),
(newEntityState, oldEntityState) => {
if (newEntityState.value !== unref(expectedValue) && isDisabled) {
if (debug) {
console.log(`automation_toggle(${entity.entity_id}): value`);
}
isDisabled.lastChanged = new Date();
isDisabled.value = true;
} else {
if (debug) {
console.log(
`automation_toggle(${
entity.entity_id
}): value - no toggle newEntityState=${
newEntityState.value
} expectedValue=${unref(expectedValue)} isDisabled=${!!isDisabled}`
);
}
}
}

if (
expectedBrightness &&
newEntityState.value &&
isDisabledBrightness &&
unref(expectedBrightness) !== newEntityState.brightness &&
/** Skip initial value, because it might be different. Let it sync first */
oldEntityState.value
) {
if (debug) {
console.log(
`automation_toggle(${newEntityState.entity_id}): brightness`
);
if (
expectedBrightness &&
newEntityState.value &&
isDisabledBrightness &&
unref(expectedBrightness) !== newEntityState.brightness &&
/** Skip initial value, because it might be different. Let it sync first */
oldEntityState.value
) {
if (debug) {
console.log(`automation_toggle(${entity.entity_id}): brightness`);
}
isDisabledBrightness.lastChanged = new Date();
isDisabledBrightness.value = true;
}
isDisabledBrightness.lastChanged = new Date();
isDisabledBrightness.value = true;
}
});
);

const autoEnableTimeParsed = parseAutoEnableTimeFactory(autoEnableTime);

Expand Down
38 changes: 19 additions & 19 deletions reactive_home/src/composeables/useNewBoolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,42 @@ export function useNewBoolean(state: FullfilledUseState, debug = false) {
}, 50);

const localValue = ref(stringBoolToBool(state.value.state));
let skipNextWatch = false;
const lastChanged = ref(state.value.last_changed);

// Local state changes
watch(localValue, (newLocalValue) => {
if (skipNextWatch) {
skipNextWatch = false;
return;
}
if (debug) {
console.log(`call(${state.value.entity_id}): updateHASSState`);
}
updateHASSState(newLocalValue);
// External change hook
const exposedValue = computed({
get() {
return localValue.value;
},
set(newValue) {
localValue.value = newValue;
lastChanged.value = new Date().toISOString();
if (debug) {
console.log(`call(${state.value.entity_id}): updateHASSState`);
}
updateHASSState(newValue);
},
});

// Incoming state changes from hass
watch(
() => state.value,
(newEntityState) => {
// If this is a change which we have send to HASS, then we can skip it
const contextIndex = skipContexts.findIndex(
(value) => value === newEntityState.context.id
);

if (contextIndex > -1) {
skipContexts.splice(contextIndex, 1);
return;
}
if (localValue.value !== stringBoolToBool(newEntityState.state)) {
skipNextWatch = true;
localValue.value = stringBoolToBool(newEntityState.state);
}

localValue.value = stringBoolToBool(newEntityState.state);
lastChanged.value = newEntityState.last_changed;
}
);

const extendObject = {
const extendedMeta = {
lastChanged: computed({
get() {
return new Date(lastChanged.value);
Expand All @@ -78,8 +78,8 @@ export function useNewBoolean(state: FullfilledUseState, debug = false) {
}),
};

return extendRef(localValue, extendObject) as typeof localValue &
UnwrapNestedRefs<typeof extendObject>;
return extendRef(exposedValue, extendedMeta) as typeof exposedValue &
UnwrapNestedRefs<typeof extendedMeta>;
}

export type UseNewBooleanEntity = ReturnType<typeof useNewBoolean>;
64 changes: 45 additions & 19 deletions reactive_home/src/composeables/useNewLight.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { FullfilledUseState } from "./useState.ts";
import { useDebounceFn, reactive, watch } from "../dep.ts";
import { useDebounceFn, reactive, watch, computed, extendRef } 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 @@ -55,27 +55,48 @@ export function useNewLight(state: FullfilledUseState, debug = false) {
brightness: getBrightnessFromAttribute(state.value),
entity_id: state.value.entity_id,
lastChanged: new Date(state.value.last_changed),
lock: false,
});

let skipNextWatch = false;

// Local state changes
watch(
() => {
return { value: localValues.value, brightness: localValues.brightness };
const exposedValue = computed({
get() {
return localValues.value;
},
(newLocalValues) => {
if (skipNextWatch) {
skipNextWatch = false;
return;
set(newValue) {
localValues.value = newValue;
localValues.lastChanged = new Date();
if (debug) {
console.log(
`call(${state.value.entity_id}): updateHASSState (via value change)`
);
}
updateHASSState(newValue, localValues.brightness);
},
});

const exposedBrightness = computed({
get() {
return localValues.brightness;
},
set(newValue) {
localValues.brightness = newValue;
localValues.lastChanged = new Date();
if (debug) {
console.log(`call(${state.value.entity_id}): updateHASSState`);
console.log(
`call(${state.value.entity_id}): updateHASSState (via brightness change)`
);
}
updateHASSState(newLocalValues.value, newLocalValues.brightness);
}
);
updateHASSState(localValues.value, newValue);
},
});

const exposedLastChanged = computed({
get() {
return localValues.lastChanged;
},
set(newValue) {
localValues.lastChanged = newValue;
},
});

// Incoming state changes from hass
watch(
Expand All @@ -101,20 +122,25 @@ export function useNewLight(state: FullfilledUseState, debug = false) {
skipContexts.splice(contextIndex, 1);
return;
}

if (localValues.value !== stringBoolToBool(newEntityState.state)) {
skipNextWatch = true;
localValues.value = stringBoolToBool(newEntityState.state);
}
if (
localValues.brightness !== getBrightnessFromAttribute(newEntityState)
) {
skipNextWatch = true;
localValues.brightness = getBrightnessFromAttribute(newEntityState);
}
}
);

return localValues;
const extendObject = {
brightness: exposedBrightness,
entity_id: state.value.entity_id,
lastChanged: exposedLastChanged,
};

return extendRef(exposedValue, extendObject);
}

export type UseNewLightEntity = ReturnType<typeof useNewLight>;

0 comments on commit 4d6021b

Please sign in to comment.