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

Add the ability to set state / attributes with entity proxies #8

Merged
merged 1 commit into from
Mar 23, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@digital-alchemy/hass",
"repository": "https://github.com/Digital-Alchemy-TS/hass",
"homepage": "https://docs.digital-alchemy.app/Hass",
"version": "0.3.5",
"version": "0.3.6",
"scripts": {
"build": "rm -rf dist/; tsc",
"lint": "eslint src",
Expand Down
37 changes: 37 additions & 0 deletions src/extensions/entity-manager.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,43 @@ export function EntityManager({
}
return proxyGetLogic(entity_id, property);
},
set(
_,
property: Extract<keyof ByIdProxy<ENTITY_ID>, string>,
value: unknown,
) {
if (property === "state") {
setImmediate(async () => {
logger.debug(
{ entity_id, state: value },
`emitting set state via rest`,
);
await hass.fetch.updateEntity(entity_id, {
state: value as string | number,
});
});
return true;
}
if (property === "attributes") {
if (!is.object(value)) {
logger.error(`can only provide objects as attributes`);
return false;
}
setImmediate(async () => {
logger.debug(
{ attributes: Object.keys(value), entity_id },
`updating attributes via rest`,
);
await hass.fetch.updateEntity(entity_id, { attributes: value });
});
return true;
}
logger.error(
{ entity_id, property },
`cannot set property on entity`,
);
return false;
},
}),
);
}
Expand Down
Loading