split sensor and binary_sensor config payloads and add json attr topic

This commit is contained in:
Michael Woods 2020-12-28 14:35:38 -05:00
parent 56e077d2e5
commit c79621c850
2 changed files with 47 additions and 20 deletions

View File

@ -134,9 +134,8 @@ class MQTT {
return state;
}
mapConfigPayload(diag, diagEl, device_class, name, attr) {
mapBaseConfigPayload(diag, diagEl, device_class, name, attr) {
name = name || MQTT.convertFriendlyName(diagEl.name);
// TODO availability
return {
device_class,
name,
@ -149,8 +148,30 @@ class MQTT {
availability_topic: this.getAvailabilityTopic(),
payload_available: 'true',
payload_not_available: 'false',
payload_on: true,
payload_off: false,
state_topic: this.getStateTopic(diag),
value_template: `{{ value_json.${MQTT.convertName(diagEl.name)} }}`,
json_attributes_topic: this.getStateTopic(diag),
json_attributes_template: attr
};
}
mapSensorConfigPayload(diag, diagEl, device_class, name, attr) {
name = name || MQTT.convertFriendlyName(diagEl.name);
return _.extend(
this.mapBaseConfigPayload(diag, diagEl, device_class, name, attr),
{unit_of_measurement: diagEl.unit});
return {
device_class,
name,
device: {
identifiers: [this.vehicle.vin],
manufacturer: this.vehicle.make,
model: this.vehicle.year,
name: this.vehicle.toString()
},
availability_topic: this.getAvailabilityTopic(),
payload_available: 'true',
payload_not_available: 'false',
state_topic: this.getStateTopic(diag),
unit_of_measurement: diagEl.unit,
value_template: `{{ value_json.${MQTT.convertName(diagEl.name)} }}`,
@ -158,6 +179,13 @@ class MQTT {
};
}
mapBinarySensorConfigPayload(diag, diagEl, device_class, name, attr) {
name = name || MQTT.convertFriendlyName(diagEl.name);
return _.extend(
this.mapBaseConfigPayload(diag, diagEl, device_class, name, attr),
{payload_on: true, payload_off: false});
}
/**
*
* @param {Diagnostic} diag
@ -169,31 +197,32 @@ class MQTT {
case 'LIFETIME ENERGY USED':
case 'LIFETIME EFFICIENCY':
case 'ELECTRIC ECONOMY':
return this.mapConfigPayload(diag, diagEl, 'energy');
return this.mapSensorConfigPayload(diag, diagEl, 'energy');
case 'INTERM VOLT BATT VOLT':
case 'EV PLUG VOLTAGE':
return this.mapConfigPayload(diag, diagEl, 'voltage');
return this.mapSensorConfigPayload(diag, diagEl, 'voltage');
case 'HYBRID BATTERY MINIMUM TEMPERATURE':
case 'AMBIENT AIR TEMPERATURE':
return this.mapConfigPayload(diag, diagEl, 'temperature');
return this.mapSensorConfigPayload(diag, diagEl, 'temperature');
case 'EV BATTERY LEVEL':
return this.mapConfigPayload(diag, diagEl, 'battery');
return this.mapSensorConfigPayload(diag, diagEl, 'battery');
case 'TIRE PRESSURE LF':
return this.mapConfigPayload(diag, diagEl, 'pressure', 'Tire Pressure: Left Front', `{{ {'recommendation': value_json.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}} | tojson }}`);
return this.mapSensorConfigPayload(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.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}} | tojson }}`);
return this.mapSensorConfigPayload(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.${MQTT.convertName('TIRE_PRESSURE_PLACARD_FRONT')}} | tojson }}`);
return this.mapSensorConfigPayload(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.${MQTT.convertName('TIRE_PRESSURE_PLACARD_REAR')}} | tojson }}`);
return this.mapSensorConfigPayload(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');
return this.mapBinarySensorConfigPayload(diag, diagEl, 'plug');
case 'EV CHARGE STATE': // not_charging/charging
return this.mapConfigPayload(diag, diagEl, 'battery_charging');
return this.mapBinarySensorConfigPayload(diag, diagEl, 'battery_charging');
// binary_sensor, but no applicable device_class
case 'PRIORITY CHARGE INDICATOR': // FALSE/TRUE
case 'PRIORITY CHARGE STATUS': // NOT_ACTIVE/ACTIVE
return this.mapBinarySensorConfigPayload(diag, diagEl);
// no device class, camel case name
case 'EV RANGE':
case 'ODOMETER':
@ -202,7 +231,7 @@ class MQTT {
case 'LIFETIME MPGE':
case 'CHARGER POWER LEVEL':
default:
return this.mapConfigPayload(diag, diagEl);
return this.mapSensorConfigPayload(diag, diagEl);
}
}
}

View File

@ -72,9 +72,8 @@ describe('MQTT', () => {
name: 'Ambient Air Temperature',
payload_available: 'true',
payload_not_available: 'false',
payload_off: false,
payload_on: true,
state_topic: 'homeassistant/sensor/XXX/ambient_air_temperature/state',
json_attributes_topic: 'homeassistant/sensor/XXX/ambient_air_temperature/state',
unit_of_measurement: '°C',
value_template: '{{ value_json.ambient_air_temperature }}'
});
@ -107,7 +106,7 @@ describe('MQTT', () => {
payload_off: false,
payload_on: true,
state_topic: 'homeassistant/binary_sensor/XXX/ev_charge_state/state',
unit_of_measurement: undefined,
json_attributes_topic: 'homeassistant/binary_sensor/XXX/ev_charge_state/state',
value_template: '{{ value_json.priority_charge_indicator }}'
});
});
@ -138,9 +137,8 @@ describe('MQTT', () => {
name: 'Tire Pressure: Left Front',
payload_available: 'true',
payload_not_available: 'false',
payload_off: false,
payload_on: true,
state_topic: 'homeassistant/sensor/XXX/tire_pressure/state',
json_attributes_topic: 'homeassistant/sensor/XXX/tire_pressure/state',
unit_of_measurement: 'kPa',
value_template: '{{ value_json.tire_pressure_lf }}'
});