Skip to content

Commit

Permalink
Fix bind bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Bre77 committed Jun 13, 2024
1 parent 9b9e6b0 commit 8a26745
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
76 changes: 42 additions & 34 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,40 +72,48 @@ export class TeslaFleetApiPlatform implements DynamicPlatformPlugin {
* must not be registered again to prevent "duplicate UUID" errors.
*/
discoverDevices() {
this.TeslaFleetApi.products_by_type().then(({ vehicles, energy_sites }) => {
vehicles.forEach((product) => {
const uuid = this.api.hap.uuid.generate(product.vin);
const cachedAccessory = this.accessories.find(
(accessory) => accessory.UUID === uuid
);
if (cachedAccessory) {
cachedAccessory.context.state = product.state;
cachedAccessory.displayName = product.display_name;
this.log.info(
"Restoring existing accessory from cache:",
cachedAccessory.displayName
const newAccessories: PlatformAccessory<VehicleContext>[] = [];
this.TeslaFleetApi.products_by_type()
.then(({ vehicles, energy_sites }) => {
vehicles.forEach((product) => {
const uuid = this.api.hap.uuid.generate(product.vin);
const cachedAccessory = this.accessories.find(
(accessory) => accessory.UUID === uuid
);
new VehicleAccessory(this, cachedAccessory);
return;
}

this.log.info("Adding new accessory:", product.display_name);
const newAccessory = new this.api.platformAccessory<VehicleContext>(
product.display_name,
uuid
);
newAccessory.context.vin = product.vin;
newAccessory.context.state = product.state;
newAccessory.displayName = product.display_name;

new VehicleAccessory(this, newAccessory);

this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [
newAccessory,
]);
});

energy_sites.forEach((product) => {});
});
if (cachedAccessory) {
cachedAccessory.context.state = product.state;
cachedAccessory.displayName = product.display_name;
this.log.info(
"Restoring existing accessory from cache:",
cachedAccessory.displayName
);
new VehicleAccessory(this, cachedAccessory);
return;
}

this.log.info("Adding new accessory:", product.display_name);
const newAccessory = new this.api.platformAccessory<VehicleContext>(
product.display_name,
uuid
);
newAccessory.context.vin = product.vin;
newAccessory.context.state = product.state;
newAccessory.displayName = product.display_name;

new VehicleAccessory(this, newAccessory);

newAccessories.push(newAccessory);
});

energy_sites.forEach((product) => {});
return newAccessories;
})
.then((newAccessories) =>
this.api.registerPlatformAccessories(
PLUGIN_NAME,
PLATFORM_NAME,
newAccessories
)
);
}
}
6 changes: 3 additions & 3 deletions src/services/battery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export class BatteryService {

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

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

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

this.parent.emitter.on("vehicle_data", () => {

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

View workflow job for this annotation

GitHub Actions / build (18.x)

Strings must use singlequote

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

View workflow job for this annotation

GitHub Actions / build (20.x)

Strings must use singlequote
batteryLevel.updateValue(this.getLevel());
Expand Down
7 changes: 4 additions & 3 deletions src/services/information.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class AccessoryInformationService {
service: Service;

constructor(private parent: VehicleAccessory) {
this.parent = parent;
this.service =
this.parent.accessory.getService(
this.parent.platform.Service.AccessoryInformation
Expand All @@ -15,7 +16,7 @@ export class AccessoryInformationService {
this.parent.platform.Service.AccessoryInformation
);

this.service // Move this to a separate service
this.service
.setCharacteristic(
this.parent.platform.Characteristic.Manufacturer,
"Tesla"
Expand All @@ -31,11 +32,11 @@ export class AccessoryInformationService {

const version = this.service
.getCharacteristic(this.parent.platform.Characteristic.FirmwareRevision)
.onGet(this.getVersion);
.onGet(this.getVersion.bind(this));

this.service
.getCharacteristic(this.parent.platform.Characteristic.Identify)
.onSet(this.setIdentify);
.onSet(this.setIdentify.bind(this));

this.parent.emitter.on("vehicle_data", () => {
version.updateValue(this.getVersion());
Expand Down
4 changes: 2 additions & 2 deletions src/vehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class VehicleAccessory {
new BatteryService(this);
}

async refresh() {
async refresh(): Promise<void> {
this.vehicle
.vehicle_data([
"charge_state",
Expand All @@ -82,7 +82,7 @@ export class VehicleAccessory {
});
}

async wake_up() {
async wake_up(): Promise<void> {
if (this.accessory.context.state === "online") {
return Promise.resolve();
}
Expand Down

0 comments on commit 8a26745

Please sign in to comment.