homebridge-tessie
Version:
Connect Homebridge to your Tessie account.
62 lines • 2.49 kB
JavaScript
import { Autonomous } from "./energy-services/autonomous.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 { REFRESH_INTERVAL } from "./settings.js";
import { EventEmitter } from "./utils/event.js";
export class EnergyAccessory {
platform;
accessory;
energy;
emitter;
constructor(platform, accessory) {
this.platform = platform;
this.accessory = accessory;
if (!this.platform.TeslaFleetApi?.energy) {
throw new Error("TeslaFleetApi not initialized");
}
this.energy = this.platform.TeslaFleetApi.energy.specific(this.accessory.context.id);
this.emitter = new EventEmitter();
// Create services
if (this.accessory.context.battery && this.accessory.context.grid && this.accessory.context.solar) {
new ChargeFromGrid(this);
new ExportBattery(this);
}
if (this.accessory.context.battery && this.accessory.context.grid) {
new BackupReserve(this);
new StormWatch(this);
new Autonomous(this);
}
// Get data and schedule refresh
this.refresh();
setInterval(() => this.refresh(), REFRESH_INTERVAL);
}
async refresh() {
this.energy
.site_info()
.then((data) => {
this.emitter.emit("site_info", data);
})
.catch(({ status, data }) => {
if (data?.error) {
this.platform.log.warn(`${this.accessory.displayName} site_info return status ${status}: ${data.error}`);
return;
}
this.platform.log.error(`${this.accessory.displayName} site_info return status ${status}: ${data}`);
});
this.energy
.live_status()
.then((data) => {
this.emitter.emit("live_status", data);
})
.catch(({ status, data }) => {
if (data?.error) {
this.platform.log.warn(`${this.accessory.displayName} live_status return status ${status}: ${data.error}`);
return;
}
this.platform.log.error(`${this.accessory.displayName} live_status return status ${status}: ${data}`);
});
}
}
//# sourceMappingURL=energy.js.map