From 9ddc22ba798c6258f226a3723d52bf5ab352d549 Mon Sep 17 00:00:00 2001 From: FireBall1725 Date: Fri, 20 May 2022 06:10:18 -0400 Subject: [PATCH 1/3] add unique id --- src/mqtt.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mqtt.js b/src/mqtt.js index 2643f3f..34c0b3b 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -169,7 +169,8 @@ class MQTT { state_topic: this.getStateTopic(diag), value_template: `{{ value_json.${MQTT.convertName(diagEl.name)} }}`, json_attributes_topic: _.isUndefined(attr) ? undefined : this.getStateTopic(diag), - json_attributes_template: attr + json_attributes_template: attr, + unique_id: `${this.vehicle.vin}-${device_class}-${name}` }; } From e17a45cdead278849112017c63e45bed2c6b2c35 Mon Sep 17 00:00:00 2001 From: FireBall1725 Date: Fri, 20 May 2022 08:51:51 -0400 Subject: [PATCH 2/3] fixed unique id --- src/mqtt.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/mqtt.js b/src/mqtt.js index 34c0b3b..2ede50e 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -154,6 +154,9 @@ class MQTT { mapBaseConfigPayload(diag, diagEl, device_class, name, attr) { name = name || MQTT.convertFriendlyName(diagEl.name); name = this.addNamePrefix(name); + // Generate the unique id from the vin and name + let unique_id = `${this.vehicle.vin}-${diagEl.name}` + unique_id = unique_id.replace(/\s+/g, '-').toLowerCase(); return { device_class, name, @@ -170,7 +173,7 @@ class MQTT { value_template: `{{ value_json.${MQTT.convertName(diagEl.name)} }}`, json_attributes_topic: _.isUndefined(attr) ? undefined : this.getStateTopic(diag), json_attributes_template: attr, - unique_id: `${this.vehicle.vin}-${device_class}-${name}` + unique_id: unique_id }; } From 2f3117ecbac206dec26d2328be4094be431070e7 Mon Sep 17 00:00:00 2001 From: FireBall1725 Date: Tue, 24 May 2022 18:44:09 -0400 Subject: [PATCH 3/3] updated the test conditions for the unique id --- test/mqtt.spec.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/mqtt.spec.js b/test/mqtt.spec.js index 92a2885..f64401a 100644 --- a/test/mqtt.spec.js +++ b/test/mqtt.spec.js @@ -92,6 +92,7 @@ describe('MQTT', () => { payload_available: 'true', payload_not_available: 'false', state_topic: 'homeassistant/sensor/XXX/ambient_air_temperature/state', + unique_id: 'xxx-ambient-air-temperature', json_attributes_topic: undefined, unit_of_measurement: '°C', value_template: '{{ value_json.ambient_air_temperature }}' @@ -112,6 +113,7 @@ describe('MQTT', () => { payload_available: 'true', payload_not_available: 'false', state_topic: 'homeassistant/sensor/XXX/ambient_air_temperature/state', + unique_id: 'xxx-ambient-air-temperature-f', json_attributes_topic: undefined, unit_of_measurement: '°F', value_template: '{{ value_json.ambient_air_temperature_f }}' @@ -146,6 +148,7 @@ describe('MQTT', () => { payload_off: false, payload_on: true, state_topic: 'homeassistant/binary_sensor/XXX/ev_charge_state/state', + unique_id: 'xxx-priority-charge-indicator', json_attributes_topic: undefined, value_template: '{{ value_json.priority_charge_indicator }}' }); @@ -178,6 +181,7 @@ describe('MQTT', () => { 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 }}'