From f68c0cb2a8bb7814c1acd2bad7cc170074d164d1 Mon Sep 17 00:00:00 2001 From: BigThunderSR <17056173+BigThunderSR@users.noreply.github.com> Date: Wed, 3 Apr 2024 13:16:50 -0500 Subject: [PATCH 1/2] Minor cleanup --- src/index.js | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/index.js b/src/index.js index 3420062a..c77ace6b 100644 --- a/src/index.js +++ b/src/index.js @@ -572,6 +572,23 @@ logger.info('!-- Starting OnStar2MQTT Polling --!'); logger.info('Requesting diagnostics'); logger.debug(`GetSupported: ${v.getSupported()}`); + if (!buttonConfigsPublished) { + const sensors = [ + { name: 'oil_life', component: null, icon: 'mdi:oil-level' }, + { name: 'tire_pressure', component: 'tire_pressure_lf_message', icon: 'mdi:car-tire-alert' }, + { name: 'tire_pressure', component: 'tire_pressure_lr_message', icon: 'mdi:car-tire-alert' }, + { name: 'tire_pressure', component: 'tire_pressure_rf_message', icon: 'mdi:car-tire-alert' }, + { name: 'tire_pressure', component: 'tire_pressure_rr_message', icon: 'mdi:car-tire-alert' }, + ]; + + for (let sensor of sensors) { + const sensorMessagePayload = mqttHA.createSensorMessageConfigPayload(sensor.name, sensor.component, sensor.icon); + logger.debug(`Sensor Message Payload: ${sensor.name}, ${sensor.component}`, sensorMessagePayload); + client.publish(sensorMessagePayload.topic, JSON.stringify(sensorMessagePayload.payload), { retain: true }); + } + logger.info(`Sensor Message Configs Published!`); + } + function publishButtonConfigs() { // Only run on initial startup if (!buttonConfigsPublished) { @@ -604,27 +621,11 @@ logger.info('!-- Starting OnStar2MQTT Polling --!'); client.publish(buttonConfig, JSON.stringify(configPayload), { retain: true }); }); }); - logger.info(`Button Configs Published!`); - - const sensors = [ - { name: 'oil_life', component: null, icon: 'mdi:oil-level' }, - { name: 'tire_pressure', component: 'tire_pressure_lf_message', icon: 'mdi:car-tire-alert' }, - { name: 'tire_pressure', component: 'tire_pressure_lr_message', icon: 'mdi:car-tire-alert' }, - { name: 'tire_pressure', component: 'tire_pressure_rf_message', icon: 'mdi:car-tire-alert' }, - { name: 'tire_pressure', component: 'tire_pressure_rr_message', icon: 'mdi:car-tire-alert' }, - ]; - - for (let sensor of sensors) { - const sensorMessagePayload = mqttHA.createSensorMessageConfigPayload(sensor.name, sensor.component, sensor.icon); - logger.debug(`Sensor Message Payload: ${sensor.name}, ${sensor.component}`, sensorMessagePayload); - client.publish(sensorMessagePayload.topic, JSON.stringify(sensorMessagePayload.payload), { retain: true }); - } - logger.info(`Sensor Message Configs Published!`); buttonConfigsPublished = 'true'; + logger.info(`Button Configs Published!`); } } - - publishButtonConfigs(); + publishButtonConfigs(); const statsRes = await commands.diagnostics({ diagnosticItem: v.getSupported() }); logger.info('Diagnostic request status', { status: _.get(statsRes, 'status') }); From 9b9bf9240e6812a2462f651686808e2bb758fb00 Mon Sep 17 00:00:00 2001 From: BigThunderSR <17056173+BigThunderSR@users.noreply.github.com> Date: Wed, 3 Apr 2024 21:24:27 -0500 Subject: [PATCH 2/2] Add icons to buttons --- src/mqtt.js | 91 +++++++++++++++++++++++++++++++++++++---------- test/mqtt.spec.js | 5 +-- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/src/mqtt.js b/src/mqtt.js index 54a3882e..e0dbb199 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -47,23 +47,74 @@ 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', + Alert: { + Name: 'alert', + Icon: 'mdi:alert', + }, + AlertFlash: { + Name: 'alertFlash', + Icon: 'mdi:car-light-alert', + }, + AlertHonk: { + Name: 'alertHonk', + Icon: 'mdi:bugle', + }, + CancelAlert: { + Name: 'cancelAlert', + Icon: 'mdi:alert-minus', + }, + LockDoor: { + Name: 'lockDoor', + Icon: 'mdi:car-door-lock', + }, + UnlockDoor: { + Name: 'unlockDoor', + Icon: 'mdi:car-door-lock-open', + }, + LockTrunk: { + Name: 'lockTrunk', + Icon: 'mdi:archive-lock', + }, + UnlockTrunk: { + Name: 'unlockTrunk', + Icon: 'mdi:archive-lock-open', + }, + Start: { + Name: 'start', + Icon: 'mdi:car-key', + }, + CancelStart: { + Name: 'cancelStart', + Icon: 'mdi:car-off', + }, + GetLocation: { + Name: 'getLocation', + Icon: 'mdi:map-marker-radius', + }, + Diagnostics: { + Name: 'diagnostics', + Icon: 'mdi:car-info', + }, + EngineRPM: { + Name: 'enginerpm', + Icon: 'mdi:speedometer', + }, + ChargeOverride: { + Name: 'chargeOverride', + Icon: 'mdi:ev-station', + }, + CancelChargeOverride: { + Name: 'cancelChargeOverride', + Icon: 'mdi:battery-charging-wireless-alert', + }, + GetChargingProfile: { + Name: 'getChargingProfile', + Icon: 'mdi:battery-charging-wireless', + }, + SetChargingProfile: { + Name: 'setChargingProfile', + Icon: 'mdi:battery-sync', + }, } }; @@ -259,8 +310,9 @@ class MQTT { }, "unique_id": unique_id, "name": `Command ${button.name}`, + "icon": MQTT.CONSTANTS.BUTTONS[button.name].Icon, "command_topic": this.getCommandTopic(), - "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name] }), + "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name].Name }), "qos": 2, "enabled_by_default": false, }); @@ -304,8 +356,9 @@ class MQTT { }, "unique_id": unique_id, "name": `Command ${button.name}`, + "icon": MQTT.CONSTANTS.BUTTONS[button.name].Icon, "command_topic": this.getCommandTopic(), - "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name] }), + "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name].Name }), "qos": 2, "enabled_by_default": false, }); diff --git a/test/mqtt.spec.js b/test/mqtt.spec.js index adf39f5c..4da3fd49 100644 --- a/test/mqtt.spec.js +++ b/test/mqtt.spec.js @@ -1297,8 +1297,9 @@ describe('MQTT', () => { }, "unique_id": unique_id, "name": `Command ${button.name}`, + "icon": MQTT.CONSTANTS.BUTTONS[button.name].Icon, "command_topic": 'homeassistant/XXX/command', - "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name] }), + "payload_press": JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[button.name].Name }), "qos": 2, "enabled_by_default": false, }); @@ -1347,7 +1348,7 @@ describe('MQTT', () => { assert.strictEqual(firstPayload.unique_id, `${vehicle.vin}_Command_${firstButton.name}_Monitor`.replace(/\s+/g, '-').toLowerCase()); assert.strictEqual(firstPayload.name, `Command ${firstButton.name}`); assert.strictEqual(firstPayload.command_topic, mqtt.getCommandTopic()); - assert.strictEqual(firstPayload.payload_press, JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[firstButton.name] })); + assert.strictEqual(firstPayload.payload_press, JSON.stringify({ "command": MQTT.CONSTANTS.BUTTONS[firstButton.name].Name })); assert.strictEqual(firstPayload.qos, 2); assert.strictEqual(firstPayload.enabled_by_default, false); });