Skip to content

Instantly share code, notes, and snippets.

@langerma
Last active December 28, 2021 14:23
Show Gist options
  • Save langerma/42992dac432069c7a3d58a8c19c91fa5 to your computer and use it in GitHub Desktop.
Save langerma/42992dac432069c7a3d58a8c19c91fa5 to your computer and use it in GitHub Desktop.
homeassistant device tracking via dnsmasq dhcp
#!/bin/sh
op="${1:-op}"
mac="${2:-mac}"
ip="${3:-ip}"
hostname="${4}"
discovery="dhcp-$(echo ${mac} | sed -e s/://g)-tracker"
macvendor="$(echo ${mac} | tr -d ':' | head -c 6 | tr '[:lower:]' '[:upper:]')"
#vendor="$(grep '${macvendor}' /usr/local/bin/oui.txt | cut -d')' -f2 | tr -d '\t')"
#vendor="$(grep '${macvendor}' /usr/local/bin/oui.txt)"
vendor="$(grep $macvendor /usr/local/bin/oui.txt | cut -d')' -f2 | tr -d '\011\012\013\014\015')"
discoveryhostname="dhcp-${hostname}-tracker"
topic="homeassistant/device_tracker/dhcp/${discovery}"
#payload="${op} ${mac} ${ip} ${tstamp} (${hostname})"
if ["${hostname}" = ""];
then
hostname="${discovery}"
discoveryhostname="${discovery}"
fi
payload="{\"unique_id\":\"${discovery}\",\"state_topic\":\"${topic}/state\",\"name\":\"${discoveryhostname}\",\"payload_home\":\"home\",\"payload_not_home\":\"not_home\",\"source_type\":\"router\",\"device\":{\"identifiers\":\"${mac}\",\"name\":\"${hostname}\",\"via_device\":\"dhcp\",\"manufacturer\":\"${vendor}\",\"connections\":[[\"mac\",\"${mac}\"],[\"ip\",\"${ip}\"]]}}"
#mosquitto_pub -h "mqtt.service.consul" -u "username" -P "password" -t "${topic}" -m "${payload}" -r
mosquitto_pub -h "mqtt.service.consul" -t "${topic}/config" -m "${payload}" -r
if [ "${op}" = "add" ] || [ "${op}" = "old" ];
then
mosquitto_pub -h "mqtt.service.consul" -t "${topic}/state" -m "home" -r
else
mosquitto_pub -h "mqtt.service.consul" -t "${topic}/state" -m "not_home" -r
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment