diff --git a/src/platform.ts b/src/platform.ts index 4c427fe..959e088 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -9,9 +9,9 @@ import { Service, } from "homebridge"; +import { EnergyAccessory, EnergyContext } from "./energy.js"; import { PLATFORM_NAME, PLUGIN_NAME } from "./settings.js"; import { VehicleAccessory, VehicleContext } from "./vehicle.js"; -import { EnergyAccessory, EnergyContext } from "./energy.js"; import { Teslemetry } from "tesla-fleet-api"; @@ -54,87 +54,89 @@ export class TeslaFleetApiPlatform implements DynamicPlatformPlugin { log.debug("Executed didFinishLaunching callback"); // run the method to discover / register your devices as accessories - const { scopes } = await this.TeslaFleetApi.metadata(); - - await this.TeslaFleetApi.products_by_type() - .then(async ({ vehicles, energy_sites }) => { - const newAccessories: PlatformAccessory[] = []; - if (scopes.includes("vehicle_device_data")) { - //const newVehicleAccessories: PlatformAccessory[] = []; - vehicles.forEach(async (product) => { - this.TeslaFleetApi.vehicle!; - const uuid = this.api.hap.uuid.generate(`${PLATFORM_NAME}:${product.vin}`); - let accessory = this.accessories.find( - (accessory) => accessory.UUID === uuid - ) as PlatformAccessory | undefined; - - if (accessory) { - this.log.debug( - "Restoring existing accessory from cache:", - accessory.displayName - ); - } else { - this.log.debug("Adding new accessory:", product.display_name); - accessory = new this.api.platformAccessory( - product.display_name, - uuid, - Categories.OTHER - ); - newAccessories.push(accessory); + this.TeslaFleetApi.metadata() + .then(({ scopes }) => + this.TeslaFleetApi.products_by_type() + .then(async ({ vehicles, energy_sites }) => { + const newAccessories: PlatformAccessory[] = []; + if (scopes.includes("vehicle_device_data")) { + //const newVehicleAccessories: PlatformAccessory[] = []; + vehicles.forEach(async (product) => { + this.TeslaFleetApi.vehicle!; + const uuid = this.api.hap.uuid.generate(`${PLATFORM_NAME}:${product.vin}`); + let accessory = this.accessories.find( + (accessory) => accessory.UUID === uuid + ) as PlatformAccessory | undefined; + + if (accessory) { + this.log.debug( + "Restored existing accessory from cache:", + accessory.displayName + ); + } else { + this.log.debug("Adding new accessory:", product.display_name); + accessory = new this.api.platformAccessory( + product.display_name, + uuid, + Categories.OTHER + ); + newAccessories.push(accessory); + } + + accessory.context.vin = product.vin; + accessory.context.state = product.state; + accessory.displayName = product.display_name; + + new VehicleAccessory(this, accessory); + }); } - accessory.context.vin = product.vin; - accessory.context.state = product.state; - accessory.displayName = product.display_name; - - new VehicleAccessory(this, accessory); - }); - } - - if (scopes.includes("energy_device_data")) { - //const newEnergyAccessories: PlatformAccessory[] = []; - energy_sites.forEach((product) => { - this.TeslaFleetApi.energy!; - const uuid = this.api.hap.uuid.generate(`${PLATFORM_NAME}:${product.id}`); - let accessory = this.accessories.find( - (accessory) => accessory.UUID === uuid - - ) as PlatformAccessory | undefined; - - if (accessory) { - this.log.debug( - "Restoring existing accessory from cache:", - accessory.displayName - ); - } else { - this.log.debug("Adding new accessory:", product.site_name); - accessory = new this.api.platformAccessory( - product.site_name, - uuid, - Categories.OTHER - ) - newAccessories.push(accessory); + if (scopes.includes("energy_device_data")) { + //const newEnergyAccessories: PlatformAccessory[] = []; + energy_sites.forEach((product) => { + this.TeslaFleetApi.energy!; + const uuid = this.api.hap.uuid.generate(`${PLATFORM_NAME}:${product.id}`); + let accessory = this.accessories.find( + (accessory) => accessory.UUID === uuid + + ) as PlatformAccessory | undefined; + + if (accessory) { + this.log.debug( + "Restoring existing accessory from cache:", + accessory.displayName + ); + } else { + this.log.debug("Adding new accessory:", product.site_name); + accessory = new this.api.platformAccessory( + product.site_name, + uuid, + Categories.OTHER + ); + newAccessories.push(accessory); + } + + accessory.context.id = product.energy_site_id; + accessory.context.battery = product.components.battery; + accessory.context.grid = product.components.grid; + accessory.context.solar = product.components.solar; + accessory.displayName = product.site_name; + + new EnergyAccessory(this, accessory); + }); } - accessory.context.id = product.energy_site_id; - accessory.context.battery = product.components.battery; - accessory.context.grid = product.components.grid; - accessory.context.solar = product.components.solar; - accessory.displayName = product.site_name; - - new EnergyAccessory(this, accessory); - }) - } - - return newAccessories - - }).then((newAccessories) => { - this.api.registerPlatformAccessories( - PLUGIN_NAME, - PLATFORM_NAME, - newAccessories - ) - }) + return newAccessories; + + })).then((newAccessories) => { + this.api.registerPlatformAccessories( + PLUGIN_NAME, + PLATFORM_NAME, + newAccessories + ); + }, (error) => { + this.log.error(error?.data?.error ?? error); + }); }); } diff --git a/src/vehicle.ts b/src/vehicle.ts index f33c4a7..ceac45a 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -2,13 +2,7 @@ import { PlatformAccessory } from "homebridge"; import { VehicleSpecific } from "tesla-fleet-api"; import { - ChargeState, - ClimateState, - DriveState, - GUISettings, - VehicleConfig, VehicleDataResponse, - VehicleState, } from "tesla-fleet-api/dist/types/vehicle_data"; import { TeslaFleetApiPlatform } from "./platform.js"; import { REFRESH_INTERVAL } from "./settings.js"; @@ -31,12 +25,6 @@ import { WindowService } from "./vehicle-services/windows.js"; export type VehicleContext = { vin: string; state: string; - /*charge_state: ChargeState; - climate_state: ClimateState; - drive_state: DriveState; - gui_settings: GUISettings; - vehicle_config: VehicleConfig; - vehicle_state: VehicleState;*/ }; export interface VehicleDataEvent { @@ -100,10 +88,6 @@ export class VehicleAccessory { ]) .then((data) => { this.accessory.context.state = data.state; - /*this.accessory.context.charge_state = data.charge_state; - this.accessory.context.climate_state = data.climate_state; - this.accessory.context.drive_state = data.drive_state; - this.accessory.context.vehicle_state = data.vehicle_state;*/ this.emitter.emit("vehicle_data", data); }) .catch(({ status, data }) => {