only define attr topic if provided a template

This commit is contained in:
Michael Woods 2020-12-28 14:51:34 -05:00
parent c79621c850
commit 58388d857b
2 changed files with 3 additions and 20 deletions

View File

@ -150,7 +150,7 @@ class MQTT {
payload_not_available: 'false', payload_not_available: 'false',
state_topic: this.getStateTopic(diag), state_topic: this.getStateTopic(diag),
value_template: `{{ value_json.${MQTT.convertName(diagEl.name)} }}`, value_template: `{{ value_json.${MQTT.convertName(diagEl.name)} }}`,
json_attributes_topic: this.getStateTopic(diag), json_attributes_topic: _.isUndefined(attr) ? undefined : this.getStateTopic(diag),
json_attributes_template: attr json_attributes_template: attr
}; };
} }
@ -160,23 +160,6 @@ class MQTT {
return _.extend( return _.extend(
this.mapBaseConfigPayload(diag, diagEl, device_class, name, attr), this.mapBaseConfigPayload(diag, diagEl, device_class, name, attr),
{unit_of_measurement: diagEl.unit}); {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)} }}`,
json_attributes_template: attr
};
} }
mapBinarySensorConfigPayload(diag, diagEl, device_class, name, attr) { mapBinarySensorConfigPayload(diag, diagEl, device_class, name, attr) {

View File

@ -73,7 +73,7 @@ describe('MQTT', () => {
payload_available: 'true', payload_available: 'true',
payload_not_available: 'false', payload_not_available: 'false',
state_topic: 'homeassistant/sensor/XXX/ambient_air_temperature/state', state_topic: 'homeassistant/sensor/XXX/ambient_air_temperature/state',
json_attributes_topic: 'homeassistant/sensor/XXX/ambient_air_temperature/state', json_attributes_topic: undefined,
unit_of_measurement: '°C', unit_of_measurement: '°C',
value_template: '{{ value_json.ambient_air_temperature }}' value_template: '{{ value_json.ambient_air_temperature }}'
}); });
@ -106,7 +106,7 @@ describe('MQTT', () => {
payload_off: false, payload_off: false,
payload_on: true, payload_on: true,
state_topic: 'homeassistant/binary_sensor/XXX/ev_charge_state/state', state_topic: 'homeassistant/binary_sensor/XXX/ev_charge_state/state',
json_attributes_topic: 'homeassistant/binary_sensor/XXX/ev_charge_state/state', json_attributes_topic: undefined,
value_template: '{{ value_json.priority_charge_indicator }}' value_template: '{{ value_json.priority_charge_indicator }}'
}); });
}); });