Merge pull request #6 from nivg1992/ha_relate_entity_to_device

adding a device details to home assistant discovery message
This commit is contained in:
Masoko
2021-12-10 17:20:05 +02:00
committed by GitHub

View File

@@ -78,15 +78,27 @@ def check_uptime():
full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime" full_cmd = "awk '{print int($1/3600/24)}' /proc/uptime"
return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]) return int(subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0])
def check_model_name():
full_cmd = "cat /proc/cpuinfo | grep Model | sed 's/Model.*: //g'"
return subprocess.Popen(full_cmd, shell=True, stdout=subprocess.PIPE).communicate()[0].decode("utf-8")
def config_json(what_config): def config_json(what_config):
model_name = check_model_name()
data = { data = {
"state_topic": "", "state_topic": "",
"icon": "", "icon": "",
"name": "", "name": "",
"unique_id": "", "unique_id": "",
"unit_of_measurement": "", "unit_of_measurement": "",
"device": {
"identifiers": [hostname],
"manufacturer": "Raspberry Pi",
"model": model_name,
"name": hostname
}
} }
data["state_topic"] = config.mqtt_topic_prefix + "/" + hostname + "/" + what_config data["state_topic"] = config.mqtt_topic_prefix + "/" + hostname + "/" + what_config
data["unique_id"] = hostname + "_" + what_config data["unique_id"] = hostname + "_" + what_config
if what_config == "cpuload": if what_config == "cpuload":