Skip to content

Commit

Permalink
Handle failures at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jul 18, 2024
1 parent 8421d50 commit 9dc04b2
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 94 deletions.
158 changes: 80 additions & 78 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<VehicleContext | EnergyContext>[] = [];
if (scopes.includes("vehicle_device_data")) {
//const newVehicleAccessories: PlatformAccessory<VehicleContext>[] = [];
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<VehicleContext> | 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<VehicleContext>(
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<VehicleContext | EnergyContext>[] = [];
if (scopes.includes("vehicle_device_data")) {
//const newVehicleAccessories: PlatformAccessory<VehicleContext>[] = [];
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<VehicleContext> | 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<VehicleContext>(
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<EnergyContext>[] = [];
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<EnergyContext> | 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<EnergyContext>(
product.site_name,
uuid,
Categories.OTHER
)
newAccessories.push(accessory);
if (scopes.includes("energy_device_data")) {
//const newEnergyAccessories: PlatformAccessory<EnergyContext>[] = [];
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<EnergyContext> | 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<EnergyContext>(
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);
});
});
}

Expand Down
16 changes: 0 additions & 16 deletions src/vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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 }) => {
Expand Down

0 comments on commit 9dc04b2

Please sign in to comment.