Skip to content

Commit

Permalink
Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jun 12, 2024
1 parent fd235eb commit 914aad2
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 37 deletions.
51 changes: 51 additions & 0 deletions VehicleService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import {
CharacteristicGetCallback,
CharacteristicValue,
HAP,
HAPStatus,
Logging,
Nullable,
Service,
} from "homebridge";
import { VehicleDataResponse } from "tesla-fleet-api/dist/types/vehicle_data.js";
import { VehicleAccessory } from "../vehicle.js";

export abstract class VehicleService {
service: Service;

constructor(
public readonly parent: VehicleAccessory,
public readonly serviceType: ServiceType
) {
this.service =
this.parent.accessory.getService(serviceType) ||
this.parent.accessory.addService(serviceType);

this.setupCharacteristics();
}

/*protected createGetter<T extends CharacteristicValue>(
getter: Getter<T>
): GetterCallback {
return (callback) => {
this.context.tesla
.getVehicleData()
.then((data) => getter.call(this, data))
.then((value) => callback(null, value))
.catch((error: Error) => callback(error));
};
}
protected createSetter<T extends CharacteristicValue>(
setter: Setter<T>
): SetterCallback {
return (value, callback) => {
setter
.call(this, value as T)
.then((writeResponse) => callback(null, writeResponse ?? undefined))
.catch((error: Error) => callback(error));
};
}*/

abstract setupCharacteristics(): void;
}
File renamed without changes.
60 changes: 32 additions & 28 deletions src/services/battery.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,56 @@
import { PlatformAccessory, Service } from "homebridge";
import { VehicleSpecific } from "tesla-fleet-api";
import { TeslaFleetApiPlatform } from "../platform.js";
import { Characteristic, Service } from "homebridge";

Check failure on line 1 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'Characteristic' is defined but never used

Check warning on line 1 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check failure on line 1 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

'Characteristic' is defined but never used

Check warning on line 1 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
import { VehicleDataResponse } from "tesla-fleet-api/dist/types/vehicle_data.js";

Check warning on line 2 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check warning on line 2 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
import { VehicleAccessory } from "../vehicle.js";

Check warning on line 3 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check warning on line 3 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote

export class BatteryService {
service: Service;

constructor(
private platform: TeslaFleetApiPlatform,
private accessory: PlatformAccessory,
private vehicle: VehicleSpecific
) {
constructor(private parent: VehicleAccessory) {
this.service =
this.accessory.getService(this.platform.Service.BatteryService) ||
this.accessory.addService(this.platform.Service.BatteryService);
this.parent.accessory.getService(this.parent.platform.Service.Battery) ||
this.parent.accessory.addService(this.parent.platform.Service.Battery);

const batteryLevel = this.service
.getCharacteristic(this.platform.Characteristic.BatteryLevel)
.onGet(this.getLevel.bind(this));
.getCharacteristic(this.parent.platform.Characteristic.BatteryLevel)
.onGet(() => this.getLevel(this.parent.accessory.context.data));

const chargingState = this.service
.getCharacteristic(this.platform.Characteristic.ChargingState)
.onGet(this.getChargingState.bind(this));
.getCharacteristic(this.parent.platform.Characteristic.ChargingState)
.onGet(() => this.getChargingState(this.parent.accessory.context.data));

const lowBattery = this.service
.getCharacteristic(this.platform.Characteristic.StatusLowBattery)
.onGet(this.getLowBattery.bind(this));
.getCharacteristic(this.parent.platform.Characteristic.StatusLowBattery)
.onGet(() => this.getLowBattery(this.parent.accessory.context.data));

/*tesla.on("vehicleDataUpdated", (data) => {
this.parent.emitter.on("vehicle_data", (data) => {

Check warning on line 25 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check warning on line 25 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
batteryLevel.updateValue(this.getLevel(data));
chargingState.updateValue(this.getChargingState(data));
lowBattery.updateValue(this.getLowBattery(data));
});*/
});
}

getLevel(): number {
// Assume 50% when not connected and no last-known state.
return this.accessory.context?.charge_state?.battery_level ?? 0;
getLevel(data: VehicleDataResponse): number {
return data?.charge_state?.battery_level ?? 50;
}

getChargingState(): number {
return this.accessory.context?.charge_state?.charging_state === "Charging"
? this.platform.Characteristic.ChargingState.CHARGING
: this.platform.Characteristic.ChargingState.NOT_CHARGING;
getChargingState(data: VehicleDataResponse): number {
switch (data?.charge_state?.charging_state) {
case "Starting":

Check warning on line 38 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check warning on line 38 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
return this.parent.platform.Characteristic.ChargingState.CHARGING;
case "Charging":

Check warning on line 40 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check warning on line 40 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
return this.parent.platform.Characteristic.ChargingState.CHARGING;
case "Disconnected":

Check warning on line 42 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check warning on line 42 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
return this.parent.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
case "NoPower":

Check warning on line 44 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

Check warning on line 44 in src/services/battery.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
return this.parent.platform.Characteristic.ChargingState.NOT_CHARGEABLE;
default:
return this.parent.platform.Characteristic.ChargingState.NOT_CHARGING;
}
}

getLowBattery(): boolean {
return this.accessory.context?.charge_state?.battery_level
? this.accessory.context.charge_state.battery_level <= 20
getLowBattery(data: VehicleDataResponse): boolean {
return data?.charge_state?.battery_level
? data.charge_state.battery_level <= 20
: false;
}
}
Empty file removed src/services/vehicle.ts
Empty file.
24 changes: 15 additions & 9 deletions src/vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import { CharacteristicValue, PlatformAccessory, Service } from "homebridge";

import VehicleSpecific from "tesla-fleet-api/dist/vehiclespecific.js";
import { VehicleSpecific } from "tesla-fleet-api";
import { VehicleDataResponse } from "tesla-fleet-api/dist/types/vehicle_data.js";
import { EventEmitter } from "./event.js";
import { TeslaFleetApiPlatform } from "./platform.js";
import { BatteryService } from "./services/battery.js";
import { REFRESH_INTERVAL } from "./settings.js";

/**
* Platform Accessory
* An instance of this class is created for each accessory your platform registers
* Each accessory may expose multiple services of different service types.
*/
export interface VehicleData {
vehicle_data(data: VehicleDataResponse): void;
}

export class VehicleAccessory {
private vehicle: VehicleSpecific;
public emitter: EventEmitter<VehicleData>;
private information: Service;

constructor(
private readonly platform: TeslaFleetApiPlatform,
private readonly accessory: PlatformAccessory
public readonly platform: TeslaFleetApiPlatform,
public readonly accessory: PlatformAccessory
) {
if (!this.platform.TeslaFleetApi?.vehicle) {
throw new Error("TeslaFleetApi not initialized");
Expand All @@ -26,6 +28,8 @@ export class VehicleAccessory {
this.accessory.context.vin
);

this.emitter = new EventEmitter();

this.information = this.accessory.getService(
this.platform.Service.AccessoryInformation
)!;
Expand All @@ -44,7 +48,7 @@ export class VehicleAccessory {
// each service must implement at-minimum the "required characteristics" for the given service type
// see https://developers.homebridge.io/#/service/Lightbulb.

new BatteryService(this.platform, this.accessory, this.vehicle);
new BatteryService(this);
}

async refresh() {
Expand All @@ -63,6 +67,8 @@ export class VehicleAccessory {
drive_state,
vehicle_state,
};
this.emitter.emit("vehicle_data", this.accessory.context.data);

this.information.updateCharacteristic(
this.platform.Characteristic.Active,
true
Expand Down

0 comments on commit 914aad2

Please sign in to comment.