Skip to content

Commit

Permalink
Add diagnostic element message as attributes to applicable sensors an…
Browse files Browse the repository at this point in the history
…d update tests to account for the changes
  • Loading branch information
BigThunderSR committed Mar 1, 2024
1 parent af30fed commit b62553e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 19 deletions.
8 changes: 7 additions & 1 deletion src/diagnostic.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ class DiagnosticElement {
* @param {DiagnosticElement} element
*/
static convert(element) {
const {name, unit, value} = element;
const {name, message, unit, value} = element;
const convertedUnit = Measurement.convertUnit(unit);
return new DiagnosticElement({
name: DiagnosticElement.convertName(name, convertedUnit),
message: message,
unit: convertedUnit,
value: Measurement.convertValue(value, unit)
})
Expand All @@ -52,13 +53,18 @@ class DiagnosticElement {
*/
constructor(ele) {
this._name = ele.name;
this._message = ele.message;
this.measurement = new Measurement(ele.value, ele.unit);
}

get name() {
return this._name;
}

get message() {
return this._message;
}

get value() {
return this.measurement.value;
}
Expand Down
38 changes: 24 additions & 14 deletions src/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@ const _ = require('lodash');
* - homeassistant/sensor/VIN/TIRE_PRESSURE/state -- Diagnostic
* - payload: {
* TIRE_PRESSURE_LF: 244.0,
* TIRE_PRESSURE_LF_MESSAGE: "GREEN",
* TIRE_PRESSURE_LR: 240.0,
* TIRE_PRESSURE_LR_MESSAGE: "GREEN",
* TIRE_PRESSURE_PLACARD_FRONT: 262.0,
* TIRE_PRESSURE_PLACARD_FRONT_MESSAGE: "na",
* TIRE_PRESSURE_PLACARD_REAR: 262.0,
* TIRE_PRESSURE_PLACARD_REAR_MESSAGE: "na",
* TIRE_PRESSURE_RF: 240.0,
* TIRE_PRESSURE_RF_MESSAGE: "GREEN",
* TIRE_PRESSURE_RR: 236.0,
* TIRE_PRESSURE_LF_MESSAGE: "YELLOW"
* }
* - homeassistant/sensor/VIN/TIRE_PRESSURE_LF/config -- Diagnostic Element
* - payload: {
Expand All @@ -24,7 +30,7 @@ const _ = require('lodash');
* state_topic: "homeassistant/sensor/VIN/TIRE_PRESSURE/state",
* unit_of_measurement: "kPa",
* value_template: "{{ value_json.TIRE_PRESSURE_LF }}",
* json_attributes_template: "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_FRONT} | tojson }}"
* json_attributes_template: "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_FRONT, 'message': value_json.TIRE_PRESSURE_LF_MESSAGE} | tojson }}",
* }
* - homeassistant/sensor/VIN/TIRE_PRESSURE_RR/config -- Diagnostic Element
* - payload: {
Expand All @@ -34,7 +40,7 @@ const _ = require('lodash');
* state_topic: "homeassistant/sensor/VIN/TIRE_PRESSURE/state",
* unit_of_measurement: "kPa",
* value_template: "{{ value_json.TIRE_PRESSURE_RR }}",
* json_attributes_template: "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_REAR} | tojson }}"
* json_attributes_template: "{{ {'recommendation': value_json.TIRE_PRESSURE_PLACARD_REAR, 'message': value_json.TIRE_PRESSURE_RR_MESSAGE} | tojson }}"
* }
*/
class MQTT {
Expand Down Expand Up @@ -127,7 +133,7 @@ class MQTT {
const state = {};
_.forEach(diag.diagnosticElements, e => {
// massage the binary_sensor values
let value;
let value;
switch (e.name) {
case 'EV PLUG STATE': // unplugged/plugged
value = e.value === 'plugged';
Expand All @@ -149,6 +155,7 @@ class MQTT {
break;
}
state[MQTT.convertName(e.name)] = value;
state[`${MQTT.convertName(e.name)}_message`] = e.message;
});
return state;
}
Expand Down Expand Up @@ -218,21 +225,21 @@ class MQTT {
case 'EV BATTERY LEVEL':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'battery');
case 'TIRE PRESSURE LF':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Front', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Front', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_LF_MESSAGE')}} | tojson }}`);
case 'TIRE PRESSURE LF PSI':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Front PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT_PSI')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Front PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT_PSI')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_LF_PSI_MESSAGE')}} | tojson }}`);
case 'TIRE PRESSURE LR':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Rear', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Rear', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_LR_MESSAGE')}} | tojson }}`);
case 'TIRE PRESSURE LR PSI':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Rear PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR_PSI')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Left Rear PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR_PSI')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_LR_PSI_MESSAGE')}} | tojson }}`);
case 'TIRE PRESSURE RF':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Front', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Front', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_RF_MESSAGE')}} | tojson }}`);
case 'TIRE PRESSURE RF PSI':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Front PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT_PSI')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Front PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT_PSI')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_RF_PSI_MESSAGE')}} | tojson }}`);
case 'TIRE PRESSURE RR':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Rear', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Rear', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_RR_MESSAGE')}} | tojson }}`);
case 'TIRE PRESSURE RR PSI':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Rear PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR_PSI')}} | tojson }}`);
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'pressure', 'Tire Pressure: Right Rear PSI', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR_PSI')}, 'message': value_json.${MQTT.convertName('TIRE_PRESSURE_RR_PSI_MESSAGE')}} | tojson }}`);
// binary sensor
case 'EV PLUG STATE': // unplugged/plugged
return this.mapBinarySensorConfigPayload(diag, diagEl, undefined, 'plug');
Expand All @@ -254,18 +261,21 @@ class MQTT {
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'distance');
case 'LIFETIME FUEL USED':
case 'LIFETIME FUEL USED GAL':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'volume');
return this.mapSensorConfigPayload(diag, diagEl, 'total_increasing', 'volume');
case 'FUEL AMOUNT':
case 'FUEL AMOUNT GAL':
case 'FUEL CAPACITY':
case 'FUEL CAPACITY GAL':
case 'FUEL LEVEL IN GAL':
case 'FUEL LEVEL IN GAL GAL':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', 'volume_storage');
// no device class, has message
case 'OIL LIFE':
return this.mapSensorConfigPayload(diag, diagEl, 'measurement', undefined, 'Oil Life', `{{ {'message': value_json.${MQTT.convertName('OIL_LIFE_MESSAGE')}} | tojson }}`);
// no device class, camel case name
case 'LAST TRIP ELECTRIC ECON':
case 'LIFETIME MPGE':
case 'CHARGER POWER LEVEL':
case 'LIFETIME MPGE':
case 'CHARGER POWER LEVEL':
default:
return this.mapSensorConfigPayload(diag, diagEl, 'measurement');
}
Expand Down
12 changes: 12 additions & 0 deletions test/diagnostic.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,18 @@
"message": "na"
}
]
},
{
"name": "OIL LIFE",
"diagnosticElement": [
{
"name": "OIL LIFE",
"status": "NA",
"message": "GREEN",
"value": "21.9",
"unit": "%"
}
]
}
]
}
Expand Down
80 changes: 76 additions & 4 deletions test/mqtt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('MQTT', () => {
model: 2020,
name: '2020 foo bar'
},
//message: 'na',
state_class: 'measurement',
device_class: 'temperature',
json_attributes_template: undefined,
Expand All @@ -108,6 +109,7 @@ describe('MQTT', () => {
model: 2020,
name: '2020 foo bar'
},
//message: 'na',
state_class: 'measurement',
device_class: 'temperature',
json_attributes_template: undefined,
Expand All @@ -124,7 +126,11 @@ describe('MQTT', () => {
it('should generate state payloads', () => {
assert.deepStrictEqual(mqtt.getStatePayload(d), {
ambient_air_temperature: 15,
ambient_air_temperature_f: 59
ambient_air_temperature_f: 59,
ambient_air_temperature_f_message: 'na',
ambient_air_temperature_message: 'na'
//ambient_air_temperature: 15,
//ambient_air_temperature_f: 59
});
});
});
Expand All @@ -141,7 +147,8 @@ describe('MQTT', () => {
manufacturer: 'foo',
model: 2020,
name: '2020 foo bar'
},
},
//message: 'na',
state_class: undefined,
device_class: undefined,
json_attributes_template: undefined,
Expand All @@ -159,8 +166,14 @@ describe('MQTT', () => {
it('should generate state payloads', () => {
assert.deepStrictEqual(mqtt.getStatePayload(d), {
ev_charge_state: false,
ev_charge_state_message: 'charging_complete',
priority_charge_indicator: false,
priority_charge_status: false
priority_charge_indicator_message: 'na',
priority_charge_status: false,
priority_charge_status_message: 'na'
//ev_charge_state: false,
//priority_charge_indicator: false,
//priority_charge_status: false
});
});
});
Expand All @@ -178,9 +191,10 @@ describe('MQTT', () => {
model: 2020,
name: '2020 foo bar'
},
//message: 'YELLOW',
state_class: 'measurement',
device_class: 'pressure',
json_attributes_template: "{{ {'recommendation': value_json.tire_pressure_placard_front} | tojson }}",
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',
Expand All @@ -192,5 +206,63 @@ 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[5]), {
availability_topic: 'homeassistant/XXX/available',
device: {
identifiers: [
'XXX'
],
manufacturer: 'foo',
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 }}",
name: 'Tire Pressure: Right Rear',
payload_available: 'true',
payload_not_available: 'false',
state_topic: 'homeassistant/sensor/XXX/tire_pressure/state',
unique_id: 'xxx-tire-pressure-rr',
json_attributes_topic: 'homeassistant/sensor/XXX/tire_pressure/state',
unit_of_measurement: 'kPa',
value_template: '{{ value_json.tire_pressure_rr }}'
});
});
});

describe('attributes', () => {
beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[10]')));
it('should generate payloads with an attribute', () => {
assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[0]), {
availability_topic: 'homeassistant/XXX/available',
device: {
identifiers: [
'XXX'
],
manufacturer: 'foo',
model: 2020,
name: '2020 foo bar'
},
//message: 'YELLOW',
state_class: 'measurement',
device_class: undefined,
json_attributes_template: "{{ {'message': value_json.oil_life_message} | tojson }}",
name: 'Oil Life',
payload_available: 'true',
payload_not_available: 'false',
state_topic: 'homeassistant/sensor/XXX/oil_life/state',
unique_id: 'xxx-oil-life',
json_attributes_topic: 'homeassistant/sensor/XXX/oil_life/state',
unit_of_measurement: '%',
value_template: '{{ value_json.oil_life }}'
});
});
});
});
});

0 comments on commit b62553e

Please sign in to comment.