Skip to content

Commit

Permalink
fix: some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNoim committed Jan 9, 2023
1 parent 0f2762f commit 9ca2fa6
Show file tree
Hide file tree
Showing 7 changed files with 352 additions and 59 deletions.
239 changes: 238 additions & 1 deletion deno.lock

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

15 changes: 14 additions & 1 deletion reactive_home/src/composeables/mapState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,23 @@ export function mapState(config: MapStateConfig) {
],
([newValue, newLightValue], [oldValue, oldLightValue]) => {
// Skip initial state
if (!oldValue || oldLightValue < 0 || newLightValue < 0) {
if (typeof oldValue !== "boolean" || typeof oldLightValue !== "number") {
if (config.debug) {
console.log(`computed_change(${config.entity}): skip `, {
newValue,
newLightValue,
});
}
return;
}

if (config.debug) {
console.log(`computed_change(${config.entity}): `, {
newValue,
newLightValue,
});
}

if (newValue !== unref(config.desiredState)) {
if (config.debug) {
console.error(
Expand Down
40 changes: 18 additions & 22 deletions reactive_home/src/composeables/useBoolean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState } from "./useState.ts";
import { computed, extendRef, refAutoReset } from "../dep.ts";
import { auth, createApiCall } from "../hass/connection.ts";
import type { Ref, UnwrapNestedRefs } from "../dep.ts";
import { connection } from "../hass/connection.ts";
import type { Ref, UnwrapNestedRefs, MessageBase, HassEntity } from "../dep.ts";

/**
* Switch device helper function. If a device supports only on and off, this can help manage this device as simple boolean.
Expand All @@ -12,30 +12,26 @@ import type { Ref, UnwrapNestedRefs } from "../dep.ts";
export function useBoolean(entity: string, debug = false) {
const state = useState(entity);

const localBool: Ref<null | boolean> = refAutoReset(null, 5000);

async function set(newValue: boolean) {
const callServiceUrl = createApiCall(
`/api/services/${state.value?.entity_id?.split(".").at(0)}/turn_${
newValue ? "on" : "off"
}`
);
const payload = {
type: "call_service",
domain: (state.value?.entity_id ?? entity).split(".").at(0),
service: `turn_${newValue ? "on" : "off"}`,
target: {
entity_id: entity,
},
} satisfies MessageBase;

if (debug) {
console.log(`call_service(${entity}): `, {
callServiceUrl: callServiceUrl,
});
console.log(`call_service(${entity}): `, payload);
}
await fetch(callServiceUrl, {
method: "POST",
headers: {
"content-type": "application/json",
Authorization: `Bearer ${auth.accessToken}`,
},
body: JSON.stringify({
entity_id: entity,
}),
});
}

const localBool: Ref<null | boolean> = refAutoReset(null, 5000);
await connection.sendMessagePromise(payload);

localBool.value = null;
}

const bool: Ref<boolean> = computed({
get() {
Expand Down
Loading

0 comments on commit 9ca2fa6

Please sign in to comment.