-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
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; | ||
} |
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 GitHub Actions / build (18.x)
Check warning on line 1 in src/services/battery.ts GitHub Actions / build (18.x)
Check failure on line 1 in src/services/battery.ts GitHub Actions / build (20.x)
|
||
import { VehicleDataResponse } from "tesla-fleet-api/dist/types/vehicle_data.js"; | ||
Check warning on line 2 in src/services/battery.ts GitHub Actions / build (18.x)
|
||
import { VehicleAccessory } from "../vehicle.js"; | ||
Check warning on line 3 in src/services/battery.ts GitHub Actions / build (18.x)
|
||
|
||
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 GitHub Actions / build (18.x)
|
||
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 GitHub Actions / build (18.x)
|
||
return this.parent.platform.Characteristic.ChargingState.CHARGING; | ||
case "Charging": | ||
Check warning on line 40 in src/services/battery.ts GitHub Actions / build (18.x)
|
||
return this.parent.platform.Characteristic.ChargingState.CHARGING; | ||
case "Disconnected": | ||
Check warning on line 42 in src/services/battery.ts GitHub Actions / build (18.x)
|
||
return this.parent.platform.Characteristic.ChargingState.NOT_CHARGEABLE; | ||
case "NoPower": | ||
Check warning on line 44 in src/services/battery.ts GitHub Actions / build (18.x)
|
||
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; | ||
} | ||
} |