Skip to content

Commit

Permalink
Update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
BigThunderSR committed Mar 2, 2024
1 parent 8b6cf1d commit 77b8a05
Show file tree
Hide file tree
Showing 2 changed files with 314 additions and 0 deletions.
57 changes: 57 additions & 0 deletions test/diagnostic.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
]
}
Expand Down
257 changes: 257 additions & 0 deletions test/mqtt.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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"
});
});
});


});
});

0 comments on commit 77b8a05

Please sign in to comment.