From b0b5834d8b853f9b19679c03f156929448c38309 Mon Sep 17 00:00:00 2001 From: Byackee Date: Wed, 17 Jan 2024 16:35:00 +0100 Subject: [PATCH] Add files via upload Add device_class & unit_of_measurements --- presearch-discovery.py | 53 +++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/presearch-discovery.py b/presearch-discovery.py index 00f29ba..727abbf 100644 --- a/presearch-discovery.py +++ b/presearch-discovery.py @@ -17,12 +17,31 @@ # Generate a Client ID with the publish prefix. client_id = 'presearch' username = 'mqtt' -password = 'xxxxxx' +password = 'xxxx' ############### MODIFY VARIABLE END################ -token = sys.argv[1] +token = sys.argv[1] +#liste pour définir les devices_class & les unit_of_measurement pour chaque type +liste = { + "connected": ["connectivity", "none"], + "blocked": ["problem", "none"], + "in_current_state_since": ["none", "timestamp"], + "minutes_in_current_state": ["none", "min"], + "total_requests" : ["none", "none"], + "successful_requests": ["none", "none"], + "avg_success_rate": ["none", "none"], + "avg_success_rate_score": ["none", "none"], + "avg_reliability_score": ["none", "none"], + "avg_staked_capacity_percent": ["", "%"], + "avg_utilization_percent": ["none", "%"], + "total_pre_earned": ["none", "none"], + "rewardable_requests": ["none", "none"] + } + + + def connect_mqtt(): - def on_connect(client, userdata, flags, rc): + def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to MQTT Broker!") else: @@ -50,31 +69,33 @@ def parse(client): # Create metrics per node for node_pub, node in nodes.items(): node_id = uuid.uuid5(uuid.NAMESPACE_DNS, node_pub).hex.upper() - print(node) - #boucle pour binary_sensor data = ["connected", "blocked"] for i in data: - #Payload for status connection - discoveryTopic = f"homeassistant/binary_sensor/presearch/{node_id}_{i}/config"; - payload = '{"unique_id": "' + f"{node_id}_{i}" + '" , ' + '"name": "' + node["meta"]["description"] + '.' + i + '", "stat_t": "' + f"presearch_nodes/{node_id}/status/{i}" +'", ' + '"payload_on": true, "payload_off": false, "device": {"identifiers": ["'+ f"{g_deviceModel}_{node_id}"'"], "name": "' + node["meta"]["description"] + '", "model": "' + f"{g_deviceModel}" + '", "manufacturer": "' + f"{g_manufacturer}" + '", "sw_version": "' + f"{g_swVersion}" '+" }}' - client.publish(discoveryTopic,payload,0,retain=True) + for x, valeurs in liste.items(): + if x == i: + #Payload for status connection + discoveryTopic = f"homeassistant/binary_sensor/presearch/{node_id}_{i}/config"; + payload = '{"unique_id": "' + f"{node_id}_{i}" + '" , ' + '"name": "' + node["meta"]["description"] + '.' + i + '", "stat_t": "' + f"presearch_nodes/{node_id}/status/{i}" +'", ' + '"devices_class": "' + valeurs[0] + '", "payload_on": true, "payload_off": false, "device": {"identifiers": ["'+ f"{g_deviceModel}_{node_id}"'"], "name": "' + node["meta"]["description"] + '", "model": "' + f"{g_deviceModel}" + '", "manufacturer": "' + f"{g_manufacturer}" + '", "sw_version": "' + f"{g_swVersion}" '+" }}' + client.publish(discoveryTopic,payload,0,retain=True) + #print(payload) #boucle pour stats data = ["in_current_state_since", "minutes_in_current_state", "total_requests", "successful_requests", "avg_success_rate", "avg_success_rate_score", "avg_reliability_score", "avg_staked_capacity_percent", "avg_utilization_percent", "total_pre_earned", "rewardable_requests"] for s in data: - #Payload for stats/successful_requests - discoveryTopic = f"homeassistant/sensor/presearch/{node_id}_{s}/config"; - payload = '{"unique_id": "' + f"{node_id}_{s}" + '" , ' + '"name": "' + node["meta"]["description"] + '.' + s + '", ' +' "stat_t": "' + f"presearch_nodes/{node_id}/stats/{s}" +'", ' + '"device": {"identifiers": ["'+ f"{g_deviceModel}_{node_id}"'"], "name": "' + node["meta"]["description"] + '", "model": "' + f"{g_deviceModel}" + '", "manufacturer": "' + f"{g_manufacturer}" + '", "sw_version": "' + f"{g_swVersion}" '+" }}' - client.publish(discoveryTopic,payload,0,retain=True) - + for x, valeurs in liste.items(): + if x == s: + #Payload for stats/successful_requests + discoveryTopic = f"homeassistant/sensor/presearch/{node_id}_{s}/config"; + payload = '{"unique_id": "' + f"{node_id}_{s}" + '" , ' + '"name": "' + node["meta"]["description"] + '.' + s + '", ' +' "stat_t": "' + f"presearch_nodes/{node_id}/stats/{s}" +'", ' + '"unit_of_measurement": "' + valeurs[0] + '", "device": {"identifiers": ["' + f"{g_deviceModel}_{node_id}"'"], "name": "' + node["meta"]["description"] + '", "model": "' + f"{g_deviceModel}" + '", "manufacturer": "' + f"{g_manufacturer}" + '", "sw_version": "' + f"{g_swVersion}" + '" }}' + client.publish(discoveryTopic,payload,0,retain=True) + #print(payload) def run(): client = connect_mqtt() parse(client) - if __name__ == '__main__': - run() + run() \ No newline at end of file