Skip to content

Commit

Permalink
Merge pull request #163 from BigThunderSR:add-icons-to-buttons-in-mqt…
Browse files Browse the repository at this point in the history
…t-auto-discovery

Bump dependencies and add new features and enhancements
  • Loading branch information
BigThunderSR authored Apr 4, 2024
2 parents c97a30c + 9b9bf92 commit 9475dc8
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 39 deletions.
37 changes: 19 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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') });
Expand Down
91 changes: 72 additions & 19 deletions src/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
}
};

Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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,
});
Expand Down
5 changes: 3 additions & 2 deletions test/mqtt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 9475dc8

Please sign in to comment.