v1
This commit is contained in:
70
main.py
70
main.py
@@ -1,6 +1,7 @@
|
|||||||
# ajouter cette ligne en bas du fichier : sudo visudo
|
# ajouter cette ligne en bas du fichier : sudo visudo
|
||||||
# gilles ALL=(ALL) NOPASSWD: /sbin/shutdown
|
# gilles ALL=(ALL) NOPASSWD: /sbin/shutdown
|
||||||
|
|
||||||
|
# ajouter le hostname du computer
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
@@ -10,6 +11,9 @@ import subprocess
|
|||||||
import threading
|
import threading
|
||||||
import psutil
|
import psutil
|
||||||
import pynvml
|
import pynvml
|
||||||
|
import socket
|
||||||
|
|
||||||
|
hostname = socket.gethostname()
|
||||||
|
|
||||||
# Initialisation de pynvml
|
# Initialisation de pynvml
|
||||||
pynvml.nvmlInit()
|
pynvml.nvmlInit()
|
||||||
@@ -61,9 +65,9 @@ def get_cpu_usage():
|
|||||||
|
|
||||||
|
|
||||||
device_info = {
|
device_info = {
|
||||||
"identifiers": ["aorus"],
|
"identifiers": [f"{hostname}"],
|
||||||
"name": "Aorus",
|
"name": "Mqtt_pilot",
|
||||||
"manufacturer": "GigaByte",
|
"manufacturer": "Black",
|
||||||
"model": "desktop",
|
"model": "desktop",
|
||||||
"sw_version": "1.0.0",
|
"sw_version": "1.0.0",
|
||||||
"suggested_area": "salon",
|
"suggested_area": "salon",
|
||||||
@@ -72,12 +76,12 @@ device_info = {
|
|||||||
|
|
||||||
# Configuration des entités
|
# Configuration des entités
|
||||||
shutdown_entity = {
|
shutdown_entity = {
|
||||||
"name": "shutdown_aorus",
|
"name": f"shutdown_{hostname}",
|
||||||
"type": "switch",
|
"type": "switch",
|
||||||
"unique_id": "shutdown_aorus_18:c0:4d:b5:65:74",
|
"unique_id": f"shutdown_{hostname}_18:c0:4d:b5:65:74",
|
||||||
"command_topic": "pilot/aorus/shutdown/set",
|
"command_topic": f"pilot/{hostname}/shutdown/available",
|
||||||
"state_topic": "pilot/aorus/shutdown",
|
"state_topic": f"pilot/{hostname}/shutdown",
|
||||||
"availability_topic": "pilot/aorus/shutdown/available",
|
"availability_topic": f"pilot/{hostname}/shutdown/available",
|
||||||
"device_class": "switch",
|
"device_class": "switch",
|
||||||
"payload_on": "ON",
|
"payload_on": "ON",
|
||||||
"payload_off": "OFF",
|
"payload_off": "OFF",
|
||||||
@@ -88,13 +92,13 @@ shutdown_entity = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# battery_entity = {
|
# battery_entity = {
|
||||||
# "name": "battery_aorus",
|
# "name": f"battery_{hostname}",
|
||||||
# "type": "sensor",
|
# "type": "sensor",
|
||||||
# "unique_id": "battery_aorus_18:c0:4d:b5:65:74",
|
# "unique_id": f"battery_{hostname}_18:c0:4d:b5:65:74",
|
||||||
# "state_topic": "pilot/aorus/battery",
|
# "state_topic": f"pilot/{hostname}/battery",
|
||||||
# "unit_of_measurement": "%",
|
# "unit_of_measurement": "%",
|
||||||
# "device_class": "battery",
|
# "device_class": "battery",
|
||||||
# "availability_topic": "pilot/aorus/battery/available",
|
# "availability_topic": f"pilot/{hostname}/battery/available",
|
||||||
# "payload_available": "online",
|
# "payload_available": "online",
|
||||||
# "payload_not_available": "offline",
|
# "payload_not_available": "offline",
|
||||||
# "icon": "mdi:battery",
|
# "icon": "mdi:battery",
|
||||||
@@ -102,11 +106,11 @@ shutdown_entity = {
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
cpu_temp_entity = {
|
cpu_temp_entity = {
|
||||||
"name": "cpu_temp_aorus",
|
"name": f"cpu_temp_{hostname}",
|
||||||
"type": "sensor",
|
"type": "sensor",
|
||||||
"unique_id": "cpu_temp_aorus_18:c0:4d:b5:65:74",
|
"unique_id": f"cpu_temp_{hostname}_18:c0:4d:b5:65:74",
|
||||||
"state_topic": "pilot/aorus/cpu_temp",
|
"state_topic": f"pilot/{hostname}/cpu_temp",
|
||||||
"availability_topic": "pilot/aorus/cpu_temp/available",
|
"availability_topic": f"pilot/{hostname}/cpu_temp/available",
|
||||||
"device_class": "temperature",
|
"device_class": "temperature",
|
||||||
"unit_of_measurement": "°C",
|
"unit_of_measurement": "°C",
|
||||||
"payload_available": "online",
|
"payload_available": "online",
|
||||||
@@ -116,11 +120,11 @@ cpu_temp_entity = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gpu_temp_entity = {
|
gpu_temp_entity = {
|
||||||
"name": "gpu_temp_aorus",
|
"name": f"gpu_temp_{hostname}",
|
||||||
"type": "sensor",
|
"type": "sensor",
|
||||||
"unique_id": "gpu_temp_aorus_18:c0:4d:b5:65:74",
|
"unique_id": f"gpu_temp_{hostname}_18:c0:4d:b5:65:74",
|
||||||
"state_topic": "pilot/aorus/gpu_temp",
|
"state_topic": f"pilot/{hostname}/gpu_temp",
|
||||||
"availability_topic": "pilot/aorus/gpu_temp/available",
|
"availability_topic": f"pilot/{hostname}/gpu_temp/available",
|
||||||
"device_class": "temperature",
|
"device_class": "temperature",
|
||||||
"unit_of_measurement": "°C",
|
"unit_of_measurement": "°C",
|
||||||
"payload_available": "online",
|
"payload_available": "online",
|
||||||
@@ -130,11 +134,11 @@ gpu_temp_entity = {
|
|||||||
}
|
}
|
||||||
# Définition de l'entité memory_used
|
# Définition de l'entité memory_used
|
||||||
memory_used_entity = {
|
memory_used_entity = {
|
||||||
"name": "memory_used_aorus",
|
"name": f"memory_used_{hostname}",
|
||||||
"type": "sensor",
|
"type": "sensor",
|
||||||
"unique_id": "memory_used_aorus_18:c0:4d:b5:65:74",
|
"unique_id": f"memory_used_{hostname}_18:c0:4d:b5:65:74",
|
||||||
"state_topic": "pilot/aorus/memory_used",
|
"state_topic": f"pilot/{hostname}/memory_used",
|
||||||
"availability_topic": "pilot/aorus/memory_used/available",
|
"availability_topic": f"pilot/{hostname}/memory_used/available",
|
||||||
# "device_class": "memory",
|
# "device_class": "memory",
|
||||||
"unit_of_measurement": "MB",
|
"unit_of_measurement": "MB",
|
||||||
"payload_available": "online",
|
"payload_available": "online",
|
||||||
@@ -144,11 +148,11 @@ memory_used_entity = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gpu_memory_usage_entity = {
|
gpu_memory_usage_entity = {
|
||||||
"name": "gpu_memory_usage",
|
"name": f"gpu_memory_usage_{hostname}",
|
||||||
"type": "sensor",
|
"type": "sensor",
|
||||||
"unique_id": "gpu_memory_used_aorus_18:c0:4d:b5:65:74",
|
"unique_id": f"gpu_memory_used_{hostname}_18:c0:4d:b5:65:74",
|
||||||
"state_topic": "pilot/aorus/gpu_memory_usage/state",
|
"state_topic": f"pilot/{hostname}/gpu_memory_usage/state",
|
||||||
"availability_topic": "pilot/aorus/gpu_memory_usage/available",
|
"availability_topic": f"pilot/{hostname}/gpu_memory_usage/available",
|
||||||
"unit_of_measurement": "MB",
|
"unit_of_measurement": "MB",
|
||||||
# "device_class": "memory",
|
# "device_class": "memory",
|
||||||
"unit_of_measurement": "MB",
|
"unit_of_measurement": "MB",
|
||||||
@@ -159,11 +163,11 @@ gpu_memory_usage_entity = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
cpu_usage_entity = {
|
cpu_usage_entity = {
|
||||||
"name": "cpu_usage_aorus",
|
"name": f"cpu_usage_{hostname}",
|
||||||
"type": "sensor",
|
"type": "sensor",
|
||||||
"unique_id": "cpu_usage_aorus_18:c0:4d:b5:65:74",
|
"unique_id": f"cpu_usage_{hostname}_18:c0:4d:b5:65:74",
|
||||||
"state_topic": "pilot/aorus/cpu_usage",
|
"state_topic": f"pilot/{hostname}/cpu_usage",
|
||||||
"availability_topic": "pilot/aorus/cpu_usage/available",
|
"availability_topic": f"pilot/{hostname}/cpu_usage/available",
|
||||||
"unit_of_measurement": "%",
|
"unit_of_measurement": "%",
|
||||||
"payload_available": "online",
|
"payload_available": "online",
|
||||||
"payload_not_available": "offline",
|
"payload_not_available": "offline",
|
||||||
@@ -206,7 +210,7 @@ def publish_discovery_messages(client):
|
|||||||
)
|
)
|
||||||
# Publication de la configuration du capteur gpu_memory_usage
|
# Publication de la configuration du capteur gpu_memory_usage
|
||||||
client.publish(
|
client.publish(
|
||||||
f"homeassistant/sensor/{gpu_memory_usage_entity['name']}/config",
|
f"{discovery_prefix}/sensor/{gpu_memory_usage_entity['name']}/config",
|
||||||
json.dumps(gpu_memory_usage_entity),
|
json.dumps(gpu_memory_usage_entity),
|
||||||
retain=True,
|
retain=True,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ Type=simple
|
|||||||
User=gilles
|
User=gilles
|
||||||
# Délai avant de démarrer le service
|
# Délai avant de démarrer le service
|
||||||
ExecStartPre=/bin/sleep 60
|
ExecStartPre=/bin/sleep 60
|
||||||
ExecStart=/home/gilles/aorus/monenv/bin/python3 /home/gilles/aorus/main.py
|
ExecStart=/home/gilles/pilot/monenv/bin/python3 /home/gilles/pilot/main.py
|
||||||
Restart=on-failure
|
Restart=on-failure
|
||||||
RestartSec=30
|
RestartSec=30
|
||||||
ExecStopPost=/home/gilles/aorus/monenv/bin/python3 /home/gilles/aorus/mqtt_unvai.py
|
ExecStopPost=/home/gilles/pilot/monenv/bin/python3 /home/gilles/pilot/mqtt_unvai.py
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=multi-user.target
|
WantedBy=multi-user.target
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
import socket
|
||||||
|
|
||||||
|
hostname = socket.gethostname()
|
||||||
|
|
||||||
# Paramètres MQTT
|
# Paramètres MQTT
|
||||||
mqtt_broker_ip_address = "10.0.0.3"
|
mqtt_broker_ip_address = "10.0.0.3"
|
||||||
@@ -8,34 +11,34 @@ mqtt_password = ""
|
|||||||
|
|
||||||
# Configuration des entités
|
# Configuration des entités
|
||||||
shutdown_entity = {
|
shutdown_entity = {
|
||||||
"availability_topic": "pilot/aorus/shutdown/available",
|
"availability_topic": f"pilot/{hostname}/shutdown/available",
|
||||||
"payload_not_available": "offline",
|
"payload_not_available": "offline",
|
||||||
}
|
}
|
||||||
|
|
||||||
# battery_entity = {
|
# battery_entity = {
|
||||||
# "availability_topic": "pilot/aorus/battery/available",
|
# "availability_topic": f"pilot/{hostname}/battery/available",
|
||||||
# "payload_not_available": "offline",
|
# "payload_not_available": "offline",
|
||||||
# }
|
# }
|
||||||
cpu_temperature_entity = {
|
cpu_temperature_entity = {
|
||||||
"availability_topic": "pilot/aorus/cpu_temp/available",
|
"availability_topic": f"pilot/{hostname}/cpu_temp/available",
|
||||||
"payload_not_available": "offline",
|
"payload_not_available": "offline",
|
||||||
}
|
}
|
||||||
|
|
||||||
gpu_temperature_entity = {
|
gpu_temperature_entity = {
|
||||||
"availability_topic": "pilot/aorus/gpu_temp/available",
|
"availability_topic": f"pilot/{hostname}/gpu_temp/available",
|
||||||
"payload_not_available": "offline",
|
"payload_not_available": "offline",
|
||||||
}
|
}
|
||||||
gpu_memory_entity = {
|
gpu_memory_entity = {
|
||||||
"availability_topic": "pilot/aorus/gpu_memory_usage/available",
|
"availability_topic": f"pilot/{hostname}/gpu_memory_usage/available",
|
||||||
"payload_not_available": "offline",
|
"payload_not_available": "offline",
|
||||||
}
|
}
|
||||||
|
|
||||||
memory_usage_entity = {
|
memory_usage_entity = {
|
||||||
"availability_topic": "pilot/aorus/memory_used/available",
|
"availability_topic": f"pilot/{hostname}/memory_used/available",
|
||||||
"payload_not_available": "offline",
|
"payload_not_available": "offline",
|
||||||
}
|
}
|
||||||
cpu_usage_entity = {
|
cpu_usage_entity = {
|
||||||
"availability_topic": "pilot/aorus/cpu_usage/available",
|
"availability_topic": f"pilot/{hostname}/cpu_usage/available",
|
||||||
"payload_not_available": "offline",
|
"payload_not_available": "offline",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user