From 77b8a05421e047eebaecf671e028d55484af7483 Mon Sep 17 00:00:00 2001 From: BigThunderSR <17056173+BigThunderSR@users.noreply.github.com> Date: Fri, 1 Mar 2024 21:10:57 -0600 Subject: [PATCH] Update test cases --- test/diagnostic.sample.json | 57 ++++++++ test/mqtt.spec.js | 257 ++++++++++++++++++++++++++++++++++++ 2 files changed, 314 insertions(+) diff --git a/test/diagnostic.sample.json b/test/diagnostic.sample.json index 94e2de25..1647a0a2 100644 --- a/test/diagnostic.sample.json +++ b/test/diagnostic.sample.json @@ -280,6 +280,63 @@ "unit": "%" } ] + }, + { + "name": "FUEL TANK INFO", + "diagnosticElement": [ + { + "name": "FUEL AMOUNT", + "status": "NA", + "message": "na", + "value": "19.98", + "unit": "L" + }, + { + "name": "FUEL CAPACITY", + "status": "NA", + "message": "na", + "value": "60.0", + "unit": "L" + }, + { + "name": "FUEL LEVEL", + "status": "NA", + "message": "na", + "value": "33.3", + "unit": "%" + }, + { + "name": "FUEL LEVEL IN GAL", + "status": "NA", + "message": "na", + "value": "19.98", + "unit": "L" + } + ] + }, + { + "name": "LIFETIME FUEL ECON", + "diagnosticElement": [ + { + "name": "LIFETIME FUEL ECON", + "status": "NA", + "message": "na", + "value": "11.86", + "unit": "kmpl" + } + ] + }, + { + "name": "LIFETIME FUEL USED", + "diagnosticElement": [ + { + "name": "LIFETIME FUEL USED", + "status": "NA", + "message": "na", + "value": "4476.94", + "unit": "L" + } + ] } ] } diff --git a/test/mqtt.spec.js b/test/mqtt.spec.js index 108b66c1..5b83c720 100644 --- a/test/mqtt.spec.js +++ b/test/mqtt.spec.js @@ -135,6 +135,64 @@ describe('MQTT', () => { }); }); + describe('sensor', () => { + beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[7]'))); + it('should generate config payloads', () => { + assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[0]), { + availability_topic: 'homeassistant/XXX/available', + device: { + identifiers: [ + 'XXX' + ], + manufacturer: 'foo', + model: 2020, + name: '2020 foo bar' + }, + state_class: 'measurement', + device_class: 'distance', + json_attributes_template: undefined, + name: 'Odometer', + payload_available: 'true', + payload_not_available: 'false', + state_topic: 'homeassistant/sensor/XXX/odometer/state', + unique_id: 'xxx-odometer', + json_attributes_topic: undefined, + unit_of_measurement: 'km', + value_template: '{{ value_json.odometer }}' + }); + assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[1]), { + availability_topic: 'homeassistant/XXX/available', + device: { + identifiers: [ + 'XXX' + ], + manufacturer: 'foo', + model: 2020, + name: '2020 foo bar' + }, + state_class: 'measurement', + device_class: 'distance', + json_attributes_template: undefined, + name: 'Odometer Mi', + payload_available: 'true', + payload_not_available: 'false', + state_topic: 'homeassistant/sensor/XXX/odometer/state', + unique_id: 'xxx-odometer-mi', + json_attributes_topic: undefined, + unit_of_measurement: 'mi', + value_template: '{{ value_json.odometer_mi }}' + }); + }); + it('should generate state payloads', () => { + assert.deepStrictEqual(mqtt.getStatePayload(d), { + odometer: 6013.8, + odometer_message: "na", + odometer_mi: 3736.8, + odometer_mi_message: "na" + }); + }); + }); + describe('binary_sensor', () => { // TODO maybe not needed, payloads not diff beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[3]'))); it('should generate config payloads', () => { @@ -264,5 +322,204 @@ describe('MQTT', () => { }); }); }); + + describe('sensor', () => { + beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[11]'))); + it('should generate config payloads', () => { + assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[0]), { + availability_topic: 'homeassistant/XXX/available', + device: { + identifiers: [ + 'XXX' + ], + manufacturer: 'foo', + model: 2020, + name: '2020 foo bar' + }, + state_class: 'measurement', + device_class: 'volume_storage', + json_attributes_template: undefined, + name: 'Fuel Amount', + payload_available: 'true', + payload_not_available: 'false', + state_topic: 'homeassistant/sensor/XXX/fuel_tank_info/state', + unique_id: 'xxx-fuel-amount', + json_attributes_topic: undefined, + unit_of_measurement: 'L', + value_template: '{{ value_json.fuel_amount }}' + }); + assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[1]), { + availability_topic: 'homeassistant/XXX/available', + device: { + identifiers: [ + 'XXX' + ], + manufacturer: 'foo', + model: 2020, + name: '2020 foo bar' + }, + state_class: 'measurement', + device_class: 'volume_storage', + json_attributes_template: undefined, + name: 'Fuel Capacity', + payload_available: 'true', + payload_not_available: 'false', + state_topic: 'homeassistant/sensor/XXX/fuel_tank_info/state', + unique_id: 'xxx-fuel-capacity', + json_attributes_topic: undefined, + unit_of_measurement: 'L', + value_template: '{{ value_json.fuel_capacity }}' + }); + assert.deepStrictEqual(mqtt.getConfigPayload(d, d.diagnosticElements[2]), { + availability_topic: 'homeassistant/XXX/available', + device: { + identifiers: [ + 'XXX' + ], + manufacturer: 'foo', + model: 2020, + name: '2020 foo bar' + }, + state_class: 'measurement', + device_class: undefined, + json_attributes_template: undefined, + name: 'Fuel Level', + payload_available: 'true', + payload_not_available: 'false', + state_topic: 'homeassistant/sensor/XXX/fuel_tank_info/state', + unique_id: 'xxx-fuel-level', + json_attributes_topic: undefined, + unit_of_measurement: '%', + value_template: '{{ value_json.fuel_level }}' + }); + }); + it('should generate state payloads', () => { + assert.deepStrictEqual(mqtt.getStatePayload(d), { + fuel_amount: 19.98, + fuel_amount_gal: 5.3, + fuel_amount_gal_message: "na", + fuel_amount_message: "na", + fuel_capacity: 60, + fuel_capacity_gal: 15.9, + fuel_capacity_gal_message: "na", + fuel_capacity_message: "na", + fuel_level: 33.3, + fuel_level_in_gal: 19.98, + fuel_level_in_gal_gal: 5.3, + fuel_level_in_gal_gal_message: "na", + fuel_level_in_gal_message: "na", + fuel_level_message: "na" + }); + }); + }); + + describe('attributes', () => { + beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[12]'))); + 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' + }, + state_class: 'measurement', + device_class: undefined, + json_attributes_template: undefined, + name: 'Lifetime Fuel Econ', + payload_available: 'true', + payload_not_available: 'false', + state_topic: 'homeassistant/sensor/XXX/lifetime_fuel_econ/state', + unique_id: 'xxx-lifetime-fuel-econ', + json_attributes_topic: undefined, + unit_of_measurement: 'km/L', + value_template: '{{ value_json.lifetime_fuel_econ }}' + }); + }); + it('should generate state payloads', () => { + assert.deepStrictEqual(mqtt.getStatePayload(d), { + lifetime_fuel_econ: 11.86, + lifetime_fuel_econ_message: "na", + lifetime_fuel_econ_mpg: 27.9, + lifetime_fuel_econ_mpg_message: "na" + }); + }); + }); + + describe('attributes', () => { + beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[13]'))); + 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' + }, + state_class: 'total_increasing', + device_class: 'volume', + json_attributes_template: undefined, + name: 'Lifetime Fuel Used', + payload_available: 'true', + payload_not_available: 'false', + state_topic: 'homeassistant/sensor/XXX/lifetime_fuel_used/state', + unique_id: 'xxx-lifetime-fuel-used', + json_attributes_topic: undefined, + unit_of_measurement: 'L', + value_template: '{{ value_json.lifetime_fuel_used }}' + }); + }); + it('should generate state payloads', () => { + assert.deepStrictEqual(mqtt.getStatePayload(d), { + lifetime_fuel_used: 4476.94, + lifetime_fuel_used_gal: 1182.7, + lifetime_fuel_used_gal_message: "na", + lifetime_fuel_used_message: "na" + }); + }); + }); + + describe('attributes', () => { + beforeEach(() => d = new Diagnostic(_.get(apiResponse, 'commandResponse.body.diagnosticResponse[4]'))); + 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' + }, + state_class: undefined, + device_class: 'plug', + json_attributes_template: undefined, + name: 'Ev Plug State', + payload_available: 'true', + payload_not_available: 'false', + payload_off: false, + payload_on: true, + state_topic: 'homeassistant/binary_sensor/XXX/ev_plug_state/state', + unique_id: 'xxx-ev-plug-state', + json_attributes_topic: undefined, + value_template: '{{ value_json.ev_plug_state }}' + }); + }); + it('should generate state payloads', () => { + assert.deepStrictEqual(mqtt.getStatePayload(d), { + ev_plug_state: true, + ev_plug_state_message: "plugged" + }); + }); + }); + + }); });