From c1209e43a8949117768fedce4e3b2c944495174c Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 10 Jun 2024 20:46:30 +1000 Subject: [PATCH] Small improvements --- package-lock.json | 8 ++++---- package.json | 2 +- src/index.ts | 4 ++-- src/platform.ts | 39 +++++++++++++++++++++++---------------- src/vehicle.ts | 12 ++++-------- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2767cf5..8e3e44e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.0.1", "license": "Apache-2.0", "dependencies": { - "tesla-fleet-api": "^0.0.7" + "tesla-fleet-api": "^0.0.8" }, "devDependencies": { "@types/node": "^20.12.13", @@ -3139,9 +3139,9 @@ } }, "node_modules/tesla-fleet-api": { - "version": "0.0.7", - "resolved": "https://registry.npmjs.org/tesla-fleet-api/-/tesla-fleet-api-0.0.7.tgz", - "integrity": "sha512-3J8bQ2wTSmZ/nH2VfzZjJVhAGQ3zUpX/FFFsLfhCo8oQUT82rJjzifjYh3UkGYC2HXtTQOgXpwHkheJP7kQ1Vw==" + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/tesla-fleet-api/-/tesla-fleet-api-0.0.8.tgz", + "integrity": "sha512-y83Pkp2mpGU+nolixwdgXYcrOdcefYvGE43pMed5pkrlPTM/ouFTT91UbizIfWy2E5XKI1u6I4DYJMORoQ51Ww==" }, "node_modules/text-table": { "version": "0.2.0", diff --git a/package.json b/package.json index 39f2402..9b2a113 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,6 @@ "typescript": "^5.4.5" }, "dependencies": { - "tesla-fleet-api": "^0.0.7" + "tesla-fleet-api": "^0.0.8" } } diff --git a/src/index.ts b/src/index.ts index d204259..2353817 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,11 @@ import { API } from "homebridge"; import { TeslaFleetApiPlatform } from "./platform.js"; -import { PLATFORM_NAME } from "./settings.js"; +import { PLATFORM_NAME, PLUGIN_NAME } from "./settings.js"; /** * This method registers the platform with Homebridge */ export default (api: API) => { - api.registerPlatform(PLATFORM_NAME, TeslaFleetApiPlatform); + api.registerPlatform(PLUGIN_NAME, PLATFORM_NAME, TeslaFleetApiPlatform); }; diff --git a/src/platform.ts b/src/platform.ts index 559999c..5204715 100644 --- a/src/platform.ts +++ b/src/platform.ts @@ -77,28 +77,35 @@ export class TeslaFleetApiPlatform implements DynamicPlatformPlugin { // Vehicles if ("vin" in product) { const uuid = this.api.hap.uuid.generate(product.vin); - const existingAccessory = this.accessories.find( + const cachedAccessory = this.accessories.find( (accessory) => accessory.UUID === uuid ); - if (existingAccessory) { + if (cachedAccessory) { + cachedAccessory.context.state = product.state; + cachedAccessory.displayName = product.display_name; this.log.info( "Restoring existing accessory from cache:", - existingAccessory.displayName + cachedAccessory.displayName ); - new VehicleAccessory(this, existingAccessory); - } else { - this.log.info("Adding new accessory:", product.display_name); - const accessory = new this.api.platformAccessory( - product.display_name, - uuid - ); - accessory.context.product = product; - new VehicleAccessory(this, accessory); - - this.api.registerPlatformAccessories(PLUGIN_NAME, PLATFORM_NAME, [ - accessory, - ]); + new VehicleAccessory(this, cachedAccessory); + continue; } + + this.log.info("Adding new accessory:", product.display_name); + const newAccessory = new this.api.platformAccessory( + 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, + ]); } } }); diff --git a/src/vehicle.ts b/src/vehicle.ts index ce69076..e5f015a 100644 --- a/src/vehicle.ts +++ b/src/vehicle.ts @@ -31,7 +31,7 @@ export class VehicleAccessory { } this.VehicleSpecific = this.platform.TeslaFleetApi.vehicle.specific( - this.accessory.context.product.vin + this.accessory.context.vin ); this.accessory @@ -39,11 +39,11 @@ export class VehicleAccessory { .setCharacteristic(this.platform.Characteristic.Manufacturer, "Tesla") .setCharacteristic( this.platform.Characteristic.Model, - "this.VehicleSpecific.model" + this.VehicleSpecific.model ) .setCharacteristic( this.platform.Characteristic.SerialNumber, - this.accessory.context.product.vin + this.accessory.context.vin ); // get the LightBulb service if it exists, otherwise create a new LightBulb service @@ -68,11 +68,6 @@ export class VehicleAccessory { .onSet(this.setOn.bind(this)) // SET - bind to the `setOn` method below .onGet(this.getOn.bind(this)); // GET - bind to the `getOn` method below - // register handlers for the Brightness Characteristic - this.service - .getCharacteristic(this.platform.Characteristic.Brightness) - .onSet(this.setBrightness.bind(this)); // SET - bind to the 'setBrightness` method below - /** * Creating multiple services of the same type. * @@ -143,6 +138,7 @@ export class VehicleAccessory { async setOn(value: CharacteristicValue) { // implement your own code to turn your device on/off this.exampleStates.On = value as boolean; + await this.VehicleSpecific.flash_lights(); this.platform.log.debug("Set Characteristic On ->", value); }