From 346f79ac097e53044b6c3e38f253b89b9e5bb317 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 21 Jul 2024 12:40:44 +1000 Subject: [PATCH] backup reserve --- package-lock.json | 8 +++--- package.json | 2 +- rebase.sh | 1 - src/energy-services/backupreserve.ts | 25 +++++++++++++++++++ .../{changefromgrid.ts => chargefromgrid.ts} | 0 src/energy.ts | 4 ++- 6 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 src/energy-services/backupreserve.ts rename src/energy-services/{changefromgrid.ts => chargefromgrid.ts} (100%) diff --git a/package-lock.json b/package-lock.json index 86c2c5a..1f5c698 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.2.9", "license": "Apache-2.0", "dependencies": { - "tesla-fleet-api": "^0.1.3" + "tesla-fleet-api": "^0.2.0" }, "devDependencies": { "@types/node": "^20.12.13", @@ -3155,9 +3155,9 @@ } }, "node_modules/tesla-fleet-api": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/tesla-fleet-api/-/tesla-fleet-api-0.1.3.tgz", - "integrity": "sha512-KOfn2wavWQUPF1q0xA86IwIfVw9J8UqQRaFN2A1G/f4DLNLmK0OWQnoqRQ668mv+eKmlaNMyJuprKena49/L1g==", + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/tesla-fleet-api/-/tesla-fleet-api-0.2.0.tgz", + "integrity": "sha512-5MN7YQKBYrMfG0lW5xmRid+40pSGkAtXgHC//exy5sfK3J+2SHdgNgzD4qAAZM0xBSoeUjRZbTIQqysHVfoxLw==", "license": "Apache-2.0" }, "node_modules/text-table": { diff --git a/package.json b/package.json index d6d6008..f3850aa 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,6 @@ "typescript": "^5.4.5" }, "dependencies": { - "tesla-fleet-api": "^0.1.3" + "tesla-fleet-api": "^0.2.0" } } diff --git a/rebase.sh b/rebase.sh index b727dcb..e7ce178 100755 --- a/rebase.sh +++ b/rebase.sh @@ -2,7 +2,6 @@ set -e git checkout dev npm run prepublishOnly -npm version patch git push git checkout teslemetry diff --git a/src/energy-services/backupreserve.ts b/src/energy-services/backupreserve.ts new file mode 100644 index 0000000..98b7e2a --- /dev/null +++ b/src/energy-services/backupreserve.ts @@ -0,0 +1,25 @@ +import { EnergyAccessory } from "../energy.js"; +import { debounce } from "../utils/debounce.js"; +import { BaseService } from "./base.js"; + +export class BackupReserve extends BaseService { + constructor(parent: EnergyAccessory) { + super(parent, parent.platform.Service.Switch, "Backup Reserve", "backup_reserve"); + + const on = this.service + .getCharacteristic(this.parent.platform.Characteristic.On); + + const level = this.service + .getCharacteristic(this.parent.platform.Characteristic.Brightness) + .onSet( + debounce((value) => this.energy.backup(value) + .then(() => level.updateValue(value)) + .catch((e) => this.log.error(`${this.name} vehicle backup failed: ${e}`)), + 3000)); + + this.parent.emitter.on("site_info", (data) => { + on.updateValue(data.backup_reserve_percent > 0); + level.updateValue(data.backup_reserve_percent); + }); + } +} diff --git a/src/energy-services/changefromgrid.ts b/src/energy-services/chargefromgrid.ts similarity index 100% rename from src/energy-services/changefromgrid.ts rename to src/energy-services/chargefromgrid.ts diff --git a/src/energy.ts b/src/energy.ts index 7445318..46e491a 100644 --- a/src/energy.ts +++ b/src/energy.ts @@ -8,7 +8,8 @@ import { SiteInfoResponse } from "tesla-fleet-api/dist/types/site_info.js"; import { Autonomous } from "./energy-services/autonomous.js"; -import { ChargeFromGrid } from "./energy-services/changefromgrid.js"; +import { BackupReserve } from "./energy-services/backupreserve.js"; +import { ChargeFromGrid } from "./energy-services/chargefromgrid.js"; import { ExportBattery } from "./energy-services/exportbattery.js"; import { StormWatch } from "./energy-services/stormwatch.js"; import { TeslaFleetApiPlatform } from "./platform.js"; @@ -51,6 +52,7 @@ export class EnergyAccessory { new ExportBattery(this); } if (this.accessory.context.battery && this.accessory.context.grid) { + new BackupReserve(this); new StormWatch(this); new Autonomous(this); }