Skip to content

Commit

Permalink
Merge pull request #8 from Digital-Alchemy-TS/entity_proxy_set
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoe authored Mar 23, 2024
2 parents 5d92129 + 560ac4b commit 17a3386
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
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

0 comments on commit 17a3386

Please sign in to comment.