Skip to content

Commit

Permalink
Add energy services
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jul 3, 2024
1 parent 3a95607 commit 863485c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 30 deletions.
18 changes: 18 additions & 0 deletions src/energy-services/autonomous.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EnergyAccessory } from "../energy.js";
import { BaseService } from "./base.js";

export class Autonomous extends BaseService {
constructor(parent: EnergyAccessory) {
super(parent, parent.platform.Service.Switch, "Autonomous Mode", "autonomous");

const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
await this.energy.operation(value ? "autonomous" : "self_consumption").then(() => on.updateValue(value));
});

this.parent.emitter.on("site_info", (data) => {
on.updateValue(data.default_real_mode === "autonomous");
});
}
}
29 changes: 3 additions & 26 deletions src/energy-services/battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,9 @@ export class BatteryService extends BaseService {
.getCharacteristic(this.parent.platform.Characteristic.StatusLowBattery);

this.parent.emitter.on("live_status", (data) => {
batteryLevel.updateValue(this.getLevel(data));
chargingState.updateValue(this.getChargingState(data));
lowBattery.updateValue(this.getLowBattery(data));
batteryLevel.updateValue(data.percentage_charged ?? 50);
chargingState.updateValue((data.battery_power ?? 0) < 0 ? 1 : 0);
lowBattery.updateValue((data.percentage_charged ?? 50) <= 20);
});
}

getLevel(data): number {
return data.charge_state?.battery_level ?? 50;
}

getChargingState(data): number {
switch (data.charge_state?.charging_state) {
case "Starting":
return this.parent.platform.Characteristic.ChargingState.CHARGING;
case "Charging":
return this.parent.platform.Characteristic.ChargingState.CHARGING;
case "Disconnected":
return this.parent.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
case "NoPower":
return this.parent.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
default:
return this.parent.platform.Characteristic.ChargingState.NOT_CHARGING;
}
}

getLowBattery(data): boolean {
return this.getLevel(data) <= 20;
}
}
5 changes: 3 additions & 2 deletions src/energy-services/changefromgrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { BaseService } from "./base.js";

export class ChargeFromGrid extends BaseService {
constructor(parent: EnergyAccessory) {
super(parent, parent.platform.Service.Switch, "Charge From Grid", "chargefromgrid");
super(parent, parent.platform.Service.Switch, "Charge From Grid", "charge_from_grid");

const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
if (typeof value === "boolean") {
await this.energy.grid_import_export().then(() => on.updateValue(value));
// This switch is the inverse of the API value
await this.energy.grid_import_export(!value).then(() => on.updateValue(value));
}
});

Expand Down
18 changes: 18 additions & 0 deletions src/energy-services/exportbattery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EnergyAccessory } from "../energy.js";
import { BaseService } from "./base.js";

export class ExportBattery extends BaseService {
constructor(parent: EnergyAccessory) {
super(parent, parent.platform.Service.Switch, "Export To Grid", "export_to_grid");

const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
await this.energy.grid_import_export(null, value ? "battery_ok" : "pv_only").then(() => on.updateValue(value));
});

this.parent.emitter.on("site_info", (data) => {
on.updateValue(data.components.customer_preferred_export_rule === "battery_ok");
});
}
}
18 changes: 18 additions & 0 deletions src/energy-services/stormwatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { EnergyAccessory } from "../energy.js";
import { BaseService } from "./base.js";

export class StormWatch extends BaseService {
constructor(parent: EnergyAccessory) {
super(parent, parent.platform.Service.Switch, "Storm Watch", "storm_watch");

const on = this.service
.getCharacteristic(this.parent.platform.Characteristic.On)
.onSet(async (value) => {
await this.energy.storm_mode(!!value).then(() => on.updateValue(value));
});

this.parent.emitter.on("site_info", (data) => {
on.updateValue(data.user_settings.storm_mode_enabled);
});
}
}
12 changes: 10 additions & 2 deletions src/energy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
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 { ExportBattery } from "./energy-services/exportbattery.js";
import { StormWatch } from "./energy-services/stormwatch.js";
import { TeslaFleetApiPlatform } from "./platform.js";
import { REFRESH_INTERVAL } from "./settings.js";
import { EventEmitter } from "./utils/event.js";
import { ChargeFromGrid } from "./energy-services/changefromgrid.js";

export type EnergyContext = {
id: number;
Expand Down Expand Up @@ -43,8 +46,13 @@ export class EnergyAccessory {
this.emitter = new EventEmitter();

// Create services
if (this.accessory.context.battery) {
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 StormWatch(this);
new Autonomous(this);
}

// Get data and schedule refresh
Expand Down

0 comments on commit 863485c

Please sign in to comment.