From a05cbbc90cafce1ec3f1c6c11490a1056a0144df Mon Sep 17 00:00:00 2001 From: BigThunderSR <17056173+BigThunderSR@users.noreply.github.com> Date: Thu, 4 Apr 2024 11:19:25 -0500 Subject: [PATCH] Add checks for incorrect button names/commands --- src/commands.js | 4 ++++ src/mqtt.js | 20 +++++++++++++++++--- test/commands.spec.js | 9 +++++++++ test/mqtt.spec.js | 2 +- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/commands.js b/src/commands.js index 3ee5008a..2a108854 100644 --- a/src/commands.js +++ b/src/commands.js @@ -56,6 +56,10 @@ class Commands { } } + static getFunctionNames() { + return Object.getOwnPropertyNames(this.prototype).filter(name => typeof this.prototype[name] === 'function' && name !== 'constructor'); + } + constructor(onstar) { this.onstar = onstar; } diff --git a/src/mqtt.js b/src/mqtt.js index e0dbb199..7011609d 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -1,5 +1,5 @@ const _ = require('lodash'); -//const Buttons = require('./buttons'); +const commands = require('./commands'); /** * Supports Home Assistant MQTT Discovery (https://www.home-assistant.io/docs/mqtt/discovery/) @@ -80,11 +80,11 @@ class MQTT { Icon: 'mdi:archive-lock-open', }, Start: { - Name: 'start', + Name: 'startVehicle', Icon: 'mdi:car-key', }, CancelStart: { - Name: 'cancelStart', + Name: 'cancelStartVehicle', Icon: 'mdi:car-off', }, GetLocation: { @@ -118,6 +118,18 @@ class MQTT { } }; + static validateButtonNames() { + const buttonNames = Object.values(MQTT.CONSTANTS.BUTTONS).map(button => button.Name); + const commandNames = commands.getFunctionNames(); + + buttonNames.forEach(buttonName => { + if (!commandNames.includes(buttonName)) { + //console.log(`Button name "${buttonName}" does not match any command in commands.js`); + throw new Error(`Button name "${buttonName}" does not match any command in commands.js`); + } + }); + } + constructor(vehicle, prefix = 'homeassistant', namePrefix) { this.prefix = prefix; this.vehicle = vehicle; @@ -804,4 +816,6 @@ class MQTT { } } +MQTT.validateButtonNames(); + module.exports = MQTT; diff --git a/test/commands.spec.js b/test/commands.spec.js index 58169fcc..228c7a8b 100644 --- a/test/commands.spec.js +++ b/test/commands.spec.js @@ -28,6 +28,15 @@ describe('Commands', () => { commands = new Commands(onstarMock); }); + it('should return an array of function names', () => { + const functionNames = Commands.getFunctionNames(); + assert(Array.isArray(functionNames)); + assert(functionNames.length > 0); + functionNames.forEach(name => { + assert(typeof commands[name] === 'function'); + }); + }); + it('should call getAccountVehicles method', async () => { const result = await commands.getAccountVehicles(); assert.strictEqual(result, undefined); diff --git a/test/mqtt.spec.js b/test/mqtt.spec.js index 4da3fd49..4b2e8c85 100644 --- a/test/mqtt.spec.js +++ b/test/mqtt.spec.js @@ -1518,4 +1518,4 @@ describe('MQTT', () => { assert.deepStrictEqual(result, expected); }); }); -}); +}); \ No newline at end of file