Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Add device_class & unit_of_measurements
  • Loading branch information
byackee authored Jan 17, 2024
1 parent b31f75d commit b0b5834
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions presearch-discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()

0 comments on commit b0b5834

Please sign in to comment.