Skip to content

Commit

Permalink
Add unique_id to device_tracker auto-discovery config
Browse files Browse the repository at this point in the history
  • Loading branch information
BigThunderSR committed Apr 1, 2024
1 parent 65b455c commit f044e7b
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,24 +431,29 @@ const configureMQTT = async (commands, client, mqttHA) => {
const topic = mqttHA.getStateTopic({ name: command });
const deviceTrackerConfigTopic = mqttHA.getDeviceTrackerConfigTopic();
const vehicle = mqttHA.vehicle.toString();
// Done - create device_tracker entity. (Was - MQTT device tracker doesn't support lat/lon and mqtt_json)
// Now has discovery

const locationData = {
latitude: parseFloat(location.lat),
longitude: parseFloat(location.long),
speed: parseFloat(speed.value),
direction: parseFloat(direction.value)
};

const deviceTrackerConfig = {
"json_attributes_topic": topic,
"name": vehicle,
"unique_id": MQTT.convertName(vehicle) + '_device_tracker',
};

logger.debug(vehicle)
client.publish(topic,
JSON.stringify({
latitude: parseFloat(location.lat),
longitude: parseFloat(location.long),
speed: parseFloat(speed.value),
direction: parseFloat(direction.value)
}), { retain: true })
client.publish(deviceTrackerConfigTopic,
JSON.stringify({
"json_attributes_topic": topic,
"name": vehicle
}), { retain: true })

client.publish(topic, JSON.stringify(locationData), { retain: true })

client.publish(deviceTrackerConfigTopic, JSON.stringify(deviceTrackerConfig), { retain: true })
.then(() => {
logger.warn(`Published device_tracker config to topic: ${deviceTrackerConfigTopic}`);
logger.warn(`Published location to topic: ${topic}`);
logger.debug("Device Tracker Config:", deviceTrackerConfig);
})
}
}
Expand Down

0 comments on commit f044e7b

Please sign in to comment.