diff --git a/package.json b/package.json index 5e1fb39..1c62510 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/extensions/entity-manager.extension.ts b/src/extensions/entity-manager.extension.ts index 275d5b6..3259f80 100644 --- a/src/extensions/entity-manager.extension.ts +++ b/src/extensions/entity-manager.extension.ts @@ -159,6 +159,43 @@ export function EntityManager({ } return proxyGetLogic(entity_id, property); }, + set( + _, + property: Extract, 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; + }, }), ); }