Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved compat with synapse switches #11

Merged
merged 2 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions package-lock.json

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

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@digital-alchemy/automation",
"repository": "https://github.com/Digital-Alchemy-TS/automation",
"homepage": "https://docs.digital-alchemy.app/Automation",
"version": "0.3.9",
"version": "0.3.10",
"scripts": {
"build": "rm -rf dist/; tsc",
"lint": "eslint src",
Expand All @@ -25,9 +25,9 @@
},
"license": "MIT",
"dependencies": {
"@digital-alchemy/core": "^0.3.16",
"@digital-alchemy/hass": "^0.3.30",
"@digital-alchemy/synapse": "^0.3.6",
"@digital-alchemy/core": "^0.3.17",
"@digital-alchemy/hass": "^0.3.31",
"@digital-alchemy/synapse": "^0.3.8",
"dayjs": "^1.11.10",
"prom-client": "^15.1.1"
},
Expand Down
79 changes: 42 additions & 37 deletions src/extensions/managed-switch.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {

import { ManagedSwitchOptions, PickASwitch } from "../helpers";

export function ManagedSwitch({ logger, hass, scheduler }: TServiceParams) {
export function ManagedSwitch({
logger,
hass,
scheduler,
lifecycle,
}: TServiceParams) {
/**
* Logic runner for the state enforcer
*/
Expand Down Expand Up @@ -54,49 +59,49 @@ export function ManagedSwitch({ logger, hass, scheduler }: TServiceParams) {
shouldBeOn,
onUpdate = [],
}: ManagedSwitchOptions) {
logger.trace(
{ context, entity_id, name: ManageSwitch },
`setting up managed switch`,
);
const entityList = is.array(entity_id) ? entity_id : [entity_id];
lifecycle.onReady(() => {
logger.trace(
{ context, entity_id, name: ManageSwitch },
`setting up managed switch`,
);
const entityList = is.array(entity_id) ? entity_id : [entity_id];

// * Check if there should be a change
const update = async () => {
const expected = shouldBeOn();
if (!is.boolean(expected)) {
if (!is.undefined(expected)) {
logger.error(
{ context, entity_id, expected, name: ManageSwitch },
`Invalid value from switch manage function`,
);
// * Check if there should be a change
const update = async () => {
const expected = shouldBeOn();
if (!is.boolean(expected)) {
if (!is.undefined(expected)) {
logger.error(
{ context, entity_id, expected, name: ManageSwitch },
`Invalid value from switch manage function`,
);
return;
}
return;
}
return;
}
await updateEntities(expected, entityList, context);
};
await updateEntities(expected, entityList, context);
};

// * Always run on a schedule
scheduler.cron({ exec: async () => await update(), schedule });
// * Always run on a schedule
scheduler.cron({ exec: async () => await update(), schedule });

// * Update when relevant things update
if (!is.empty(onUpdate)) {
[onUpdate].flat().forEach(i => {
if (is.object(i) && !("entity_id" in i)) {
const onUpdate = i.onUpdate;
if (!is.function(onUpdate)) {
// * Update when relevant things update
if (!is.empty(onUpdate)) {
[onUpdate].flat().forEach(i => {
if (is.object(i) && !("entity_id" in i)) {
const onUpdate = i.onUpdate;
if (!is.function(onUpdate)) {
return;
}
i.onUpdate(async () => await update());
return;
}
i.onUpdate(async () => {
await update();
});
return;
}
hass.entity
.byId(is.object(i) ? i.entity_id : i)
.onUpdate(async () => await update());
});
}
hass.entity
.byId(is.object(i) ? i.entity_id : i)
.onUpdate(async () => await update());
});
}
});
}
return ManageSwitch;
}
10 changes: 9 additions & 1 deletion src/helpers/managed-switch.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import { PICK_ENTITY } from "@digital-alchemy/hass";
export type PickASwitch =
| PICK_ENTITY<"switch">
| { entity_id: PICK_ENTITY<"switch"> };

type EntityUpdate =
| PICK_ENTITY
| { entity_id: PICK_ENTITY }
| {
onUpdate: (callback: () => TBlackHole) => void;
state: unknown;
/**
* Hass entity compat
*/
state?: unknown;
/**
* Synapse entity compat
*/
is_on?: boolean;
name: string;
};

Expand Down
Loading