From b506e4b4356c8dbe60411fad7642ee0fd427c732 Mon Sep 17 00:00:00 2001 From: BigThunderSR <17056173+BigThunderSR@users.noreply.github.com> Date: Tue, 26 Mar 2024 19:54:32 -0500 Subject: [PATCH] Revert "Added MQTT button auto discovery for HA" to republish in new branch for tracking This reverts commit 4d181d111dd1ccfe5b1417d4505277b1a24c403f. --- src/commands.js | 3 +- src/index.js | 20 +----- src/mqtt.js | 73 +------------------ src/vehicle.js | 9 --- test/mqtt.spec.js | 163 +++++-------------------------------------- test/vehicle.spec.js | 14 ---- 6 files changed, 23 insertions(+), 259 deletions(-) diff --git a/src/commands.js b/src/commands.js index da7194ff..974df08c 100644 --- a/src/commands.js +++ b/src/commands.js @@ -135,7 +135,8 @@ class Commands { } async diagnostics({ diagnosticItem = [ - Commands.CONSTANTS.DIAGNOSTICS.AMBIENT_AIR_TEMPERATURE, + Commands.CONSTANTS.DIAGNOSTICS.AMBIENT_AIR_TEMPERATURE, + Commands.CONSTANTS.DIAGNOSTICS.ENGINE_RPM, Commands.CONSTANTS.DIAGNOSTICS.LAST_TRIP_DISTANCE, Commands.CONSTANTS.DIAGNOSTICS.ODOMETER, Commands.CONSTANTS.DIAGNOSTICS.TIRE_PRESSURE, diff --git a/src/index.js b/src/index.js index 156d62b4..8dd77ef4 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,6 @@ const MQTT = require('./mqtt'); const Commands = require('./commands'); const logger = require('./logger'); const fs = require('fs'); -//const Buttons = require('./buttons'); //const CircularJSON = require('circular-json'); @@ -517,21 +516,6 @@ logger.info('Starting OnStar2MQTT Polling'); const v = vehicle; logger.info('Requesting diagnostics'); logger.debug(`GetSupported: ${v.getSupported()}`); - - // Get supported commands - logger.info(`GetSupportedCommands: ${v.getSupportedCommands()}`); - // Get button configs and payloads - const { buttonConfigs, configPayloads } = mqttHA.createButtonConfigPayload(v); - // Publish button config and payload for each button - buttonConfigs.forEach((buttonConfig, index) => { - const configPayload = configPayloads[index]; - logger.warn(`Button Config Topic: ${JSON.stringify(buttonConfig)}`); - logger.debug(`Button Config Payload: ${JSON.stringify(configPayload)}`); - // Publish configPayload as the payload to the respective MQTT topic - logger.debug(`Publishing Button Config: ${buttonConfig} Payload: ${JSON.stringify(configPayload)}`); - client.publish(buttonConfig, JSON.stringify(configPayload), { retain: true }); - }); - const statsRes = await commands.diagnostics({ diagnosticItem: v.getSupported() }); logger.info('Diagnostic request status', { status: _.get(statsRes, 'status') }); const stats = _.map( @@ -575,12 +559,10 @@ logger.info('Starting OnStar2MQTT Polling'); client.publish(topic, JSON.stringify(state), { retain: true }) ); } - await Promise.all(publishes); - + //client.publish(pollingStatusTopicState, JSON.stringify({"ok":{"message":"Data Polled Successfully"}}), {retain: false}) const completionTimestamp = new Date().toISOString(); logger.debug(`Completion Timestamp: ${completionTimestamp}`); - client.publish(pollingStatusTopicState, JSON.stringify({"ok":{"message":"Data Polled Successfully"}}), {retain: false}); client.publish(pollingStatusTopicState, JSON.stringify({ "error": { diff --git a/src/mqtt.js b/src/mqtt.js index 6f98bcf9..3a100ad4 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -1,5 +1,4 @@ const _ = require('lodash'); -//const Buttons = require('./buttons'); /** * Supports Home Assistant MQTT Discovery (https://www.home-assistant.io/docs/mqtt/discovery/) @@ -45,28 +44,6 @@ const _ = require('lodash'); * } */ class MQTT { - static CONSTANTS = { - BUTTONS: { - Alert: 'alert', - AlertFlash: 'alertFlash', - AlertHonk: 'alertHonk', - CancelAlert: 'cancelAlert', - LockDoor: 'lockDoor', - UnlockDoor: 'unlockDoor', - LockTrunk: 'lockTrunk', - UnlockTrunk: 'unlockTrunk', - Start: 'start', - CancelStart: 'cancelStart', - GetLocation: 'getLocation', - Diagnostics: 'diagnostics', - EngineRPM: 'enginerpm', - ChargeOverride: 'chargeOverride', - CancelChargeOverride: 'cancelChargeOverride', - GetChargingProfile: 'getChargingProfile', - SetChargingProfile: 'setChargingProfile', - } - }; - constructor(vehicle, prefix = 'homeassistant', namePrefix) { this.prefix = prefix; this.vehicle = vehicle; @@ -137,54 +114,6 @@ class MQTT { return `${this.prefix}/device_tracker/${this.instance}/config`; } - //getButtonConfigTopic() { - // return `${this.prefix}/button/${this.instance}/${this.buttonName}/config`; - //} - - createButtonConfigPayload(vehicle) { - const buttonInstances = []; - const buttonConfigs = []; - const configPayloads = []; - - for (const buttonName in MQTT.CONSTANTS.BUTTONS) { - const buttonConfig = `${this.prefix}/button/${this.instance}/${MQTT.convertName(buttonName)}/config`; - const button = { - name: buttonName, - config: buttonConfig - }; - - button.vehicle = vehicle; - buttonInstances.push(button); - - let unique_id = `${vehicle.vin}_Command_${button.name}`; - unique_id = unique_id.replace(/\s+/g, '-').toLowerCase(); - - configPayloads.push({ - "device": { - "identifiers": [vehicle.vin], - "manufacturer": vehicle.make, - "model": vehicle.year + ' ' + vehicle.model, - "name": vehicle.toString() - }, - "availability": { - "topic": this.getAvailabilityTopic(), - "payload_available": 'true', - "payload_not_available": 'false', - }, - "unique_id": unique_id, - "name": `Command ${button.name}`, - "command_topic": this.getCommandTopic(), - "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name] }), - "qos": 2, - "enabled_by_default": false, - }); - - buttonConfigs.push(buttonConfig); - } - - return { buttonInstances, buttonConfigs, configPayloads }; - } - /** * * @param {DiagnosticElement} diag @@ -260,7 +189,7 @@ class MQTT { device: { identifiers: [this.vehicle.vin], manufacturer: this.vehicle.make, - model: this.vehicle.year + ' ' + this.vehicle.model, + model: this.vehicle.year, name: this.vehicle.toString() }, availability_topic: this.getAvailabilityTopic(), diff --git a/src/vehicle.js b/src/vehicle.js index 4e507cbd..23f8b7d6 100644 --- a/src/vehicle.js +++ b/src/vehicle.js @@ -13,8 +13,6 @@ class Vehicle { ); this.supportedDiagnostics = _.get(diagCmd, 'commandData.supportedDiagnostics.supportedDiagnostic'); - - this.supportedCommands = _.get(vehicle, 'commands.command'); } isSupported(diag) { @@ -31,13 +29,6 @@ class Vehicle { toString() { return `${this.year} ${this.make} ${this.model}`; } - - getSupportedCommands(commandList = []) { - this.supportedCommands.forEach(command => { - commandList.push(command.name); - }); - return commandList; - } } module.exports = Vehicle; \ No newline at end of file diff --git a/test/mqtt.spec.js b/test/mqtt.spec.js index a8809193..1c43d42f 100644 --- a/test/mqtt.spec.js +++ b/test/mqtt.spec.js @@ -99,7 +99,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, //message: 'na', @@ -122,7 +122,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, //message: 'na', @@ -161,7 +161,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: 'total_increasing', @@ -183,7 +183,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: 'total_increasing', @@ -219,7 +219,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, //message: 'na', @@ -252,7 +252,7 @@ describe('MQTT', () => { }); }); -/* describe('attributes', () => { + describe('attributes', () => { beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[8]'))); it('should generate payloads with an attribute', () => { assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[0]), { @@ -262,7 +262,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, //message: 'YELLOW', @@ -279,86 +279,11 @@ describe('MQTT', () => { value_template: '{{ value_json.tire_pressure_lf }}' }); }); - }); */ + }); describe('attributes', () => { beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[8]'))); - it('should generate payloads with an attribute for left front tire', () => { - assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[0]), { - availability_topic: 'homeassistant/XXX/available', - device: { - identifiers: [ - 'XXX' - ], - manufacturer: 'foo', - model: '2020 bar', - name: '2020 foo bar' - }, - - state_class: 'measurement', - device_class: 'pressure', - json_attributes_template: "{{ {'recommendation': value_json.tire_pressure_placard_front, 'message': value_json.tire_pressure_lf_message} | tojson }}", - name: 'Tire Pressure: Left Front', - payload_available: 'true', - payload_not_available: 'false', - state_topic: 'homeassistant/sensor/XXX/tire_pressure/state', - unique_id: 'xxx-tire-pressure-lf', - json_attributes_topic: 'homeassistant/sensor/XXX/tire_pressure/state', - unit_of_measurement: 'kPa', - value_template: '{{ value_json.tire_pressure_lf }}' - }); - }); - it('should generate payloads with an attribute for right front tire', () => { - assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[4]), { - availability_topic: 'homeassistant/XXX/available', - device: { - identifiers: [ - 'XXX' - ], - manufacturer: 'foo', - model: '2020 bar', - name: '2020 foo bar' - }, - - state_class: 'measurement', - device_class: 'pressure', - json_attributes_template: "{{ {'recommendation': value_json.tire_pressure_placard_front, 'message': value_json.tire_pressure_rf_message} | tojson }}", - name: 'Tire Pressure: Right Front', - payload_available: 'true', - payload_not_available: 'false', - state_topic: 'homeassistant/sensor/XXX/tire_pressure/state', - unique_id: 'xxx-tire-pressure-rf', - json_attributes_topic: 'homeassistant/sensor/XXX/tire_pressure/state', - unit_of_measurement: 'kPa', - value_template: '{{ value_json.tire_pressure_rf }}' - }); - }); - it('should generate payloads with an attribute for left rear tire', () => { - assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[1]), { - availability_topic: 'homeassistant/XXX/available', - device: { - identifiers: [ - 'XXX' - ], - manufacturer: 'foo', - model: '2020 bar', - name: '2020 foo bar' - }, - - state_class: 'measurement', - device_class: 'pressure', - json_attributes_template: "{{ {'recommendation': value_json.tire_pressure_placard_rear, 'message': value_json.tire_pressure_lr_message} | tojson }}", - name: 'Tire Pressure: Left Rear', - payload_available: 'true', - payload_not_available: 'false', - state_topic: 'homeassistant/sensor/XXX/tire_pressure/state', - unique_id: 'xxx-tire-pressure-lr', - json_attributes_topic: 'homeassistant/sensor/XXX/tire_pressure/state', - unit_of_measurement: 'kPa', - value_template: '{{ value_json.tire_pressure_lr }}' - }); - }); - it('should generate payloads with an attribute for right rear tire', () => { + it('should generate payloads with an attribute', () => { assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[5]), { availability_topic: 'homeassistant/XXX/available', device: { @@ -366,10 +291,10 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, - + //message: 'YELLOW', state_class: 'measurement', device_class: 'pressure', json_attributes_template: "{{ {'recommendation': value_json.tire_pressure_placard_rear, 'message': value_json.tire_pressure_rr_message} | tojson }}", @@ -395,7 +320,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, //message: 'YELLOW', @@ -424,7 +349,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: 'measurement', @@ -446,7 +371,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: 'measurement', @@ -468,7 +393,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: 'measurement', @@ -514,7 +439,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: 'measurement', @@ -550,7 +475,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: 'total_increasing', @@ -586,7 +511,7 @@ describe('MQTT', () => { 'XXX' ], manufacturer: 'foo', - model: '2020 bar', + model: 2020, name: '2020 foo bar' }, state_class: undefined, @@ -611,56 +536,6 @@ describe('MQTT', () => { }); }); - describe('createButtonConfigPayload', () => { - it('should generate button config payloads', () => { - const vehicle = new Vehicle({make: 'foo', model: 'bar', vin: 'XXX', year: 2020}); - const mqtt = new MQTT(vehicle); - - const expectedButtonInstances = []; - const expectedButtonConfigs = []; - const expectedConfigPayloads = []; - - for (const buttonName in MQTT.CONSTANTS.BUTTONS) { - const buttonConfig = `homeassistant/button/XXX/${MQTT.convertName(buttonName)}/config`; - const button = { - name: buttonName, - config: buttonConfig, - vehicle: vehicle - }; - - expectedButtonInstances.push(button); - expectedButtonConfigs.push(buttonConfig); - - let unique_id = `${vehicle.vin}_Command_${button.name}`; - unique_id = unique_id.replace(/\s+/g, '-').toLowerCase(); - - expectedConfigPayloads.push({ - "device": { - "identifiers": [vehicle.vin], - "manufacturer": vehicle.make, - "model": vehicle.year + ' ' + vehicle.model, - "name": vehicle.toString() - }, - "availability": { - "topic": mqtt.getAvailabilityTopic(), - "payload_available": 'true', - "payload_not_available": 'false', - }, - "unique_id": unique_id, - "name": `${vehicle.toString()} Command ${button.name}`, - "command_topic": mqtt.getCommandTopic(), - "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name] }), - "qos": 2, - "enabled_by_default": false, - }); - } - - const result = mqtt.createButtonConfigPayload(vehicle); - assert.deepStrictEqual(result.buttonInstances, expectedButtonInstances); - assert.deepStrictEqual(result.buttonConfigs, expectedButtonConfigs); - assert.deepStrictEqual(result.configPayloads, expectedConfigPayloads); - }); - }); - + }); }); diff --git a/test/vehicle.spec.js b/test/vehicle.spec.js index 64bb130f..bb8facd8 100644 --- a/test/vehicle.spec.js +++ b/test/vehicle.spec.js @@ -38,18 +38,4 @@ describe('Vehicle', () => { it('should toString() correctly', () => { assert.strictEqual(v.toString(), '2020 Chevrolet Bolt EV') }); - - it('should return the list of supported commands', () => { - const supported = v.getSupportedCommands(); - assert.ok(_.isArray(supported)); - assert.strictEqual(supported.length, 29); - }); - - it('should return the list of supported commands with provided command list', () => { - const commandList = []; - const supported = v.getSupportedCommands(commandList); - assert.ok(_.isArray(supported)); - assert.strictEqual(supported.length, 29); - assert.deepStrictEqual(supported, commandList); - }); });