From 6e89325f13860d9b8a93fd97010c70b5e5ac5bb4 Mon Sep 17 00:00:00 2001 From: Michael Woods Date: Thu, 3 Dec 2020 01:24:25 -0500 Subject: [PATCH] convert the attribute property name. fixes #5 --- src/mqtt.js | 8 ++++---- test/mqtt.spec.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/mqtt.js b/src/mqtt.js index 3cd9a37..2963d95 100644 --- a/src/mqtt.js +++ b/src/mqtt.js @@ -163,13 +163,13 @@ class MQTT { case 'EV BATTERY LEVEL': return this.mapConfigPayload(diag, diagEl, 'battery'); case 'TIRE PRESSURE LF': - return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Left Front', "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_FRONT} | tojson }}"); + return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Left Front', `{{ {"recommendation": value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}} | tojson }}`); case 'TIRE PRESSURE LR': - return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Left Rear', "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_FRONT} | tojson }}"); + return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Left Rear', `{{ {"recommendation": value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}} | tojson }}`); case 'TIRE PRESSURE RF': - return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Right Front', "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_REAR} | tojson }}"); + return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Right Front', `{{ {"recommendation": value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}} | tojson }}`); case 'TIRE PRESSURE RR': - return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Right Rear', "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_REAR} | tojson }}"); + return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Right Rear', `{{ {"recommendation": value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}} | tojson }}`); // binary sensor case 'EV PLUG STATE': // unplugged/plugged return this.mapConfigPayload(diag, diagEl, 'plug'); diff --git a/test/mqtt.spec.js b/test/mqtt.spec.js index 2fb546b..6469be5 100644 --- a/test/mqtt.spec.js +++ b/test/mqtt.spec.js @@ -91,5 +91,19 @@ describe('MQTT', () => { }); }); }); + + 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]), { + device_class: 'pressure', + json_attributes_template: '{{ {"recommendation": value_json.tire_pressure_placard_front} | tojson }}', + name: 'Tire Pressure: Left Front', + state_topic: 'homeassistant/sensor/XXX/tire_pressure/state', + unit_of_measurement: 'kPa', + value_template: '{{ value_json.tire_pressure_lf }}' + }); + }); + }); }); });