-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeedtest_to_mqtt_ha.py
More file actions
269 lines (233 loc) Β· 10.4 KB
/
speedtest_to_mqtt_ha.py
File metadata and controls
269 lines (233 loc) Β· 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python3
import subprocess
import json
import paho.mqtt.client as mqtt
import os
import time
import requests
import threading
from paho.mqtt.client import CallbackAPIVersion
from datetime import datetime
def get_version():
"""Get version from VERSION file created during Docker build"""
try:
with open('/app/VERSION', 'r') as f:
return f.read().strip()
except:
return 'unknown'
# Config from environment (with defaults)
MQTT_BROKER_HOST = os.getenv("MQTT_BROKER_HOST", "localhost")
MQTT_BROKER_PORT = int(os.getenv("MQTT_BROKER_PORT", "1883"))
MQTT_USERNAME = os.getenv("MQTT_USERNAME")
MQTT_PASSWORD = os.getenv("MQTT_PASSWORD")
DISCOVERY_PREFIX = os.getenv("DISCOVERY_PREFIX", "homeassistant")
SENSOR_PREFIX = os.getenv("SENSOR_PREFIX", "home/internet/speedtest")
COMMAND_TOPIC = os.getenv("COMMAND_TOPIC", "home/internet/speedtest/run")
DEVICE_MANUFACTURER = os.getenv("DEVICE_MANUFACTURER", "Ookla + MQTT")
DEVICE_MODEL = os.getenv("DEVICE_MODEL", "Speedtest CLI")
DEVICE_NAME = os.getenv("DEVICE_NAME", "speedtest_sensor")
# Global variables for threading and MQTT
speedtest_lock = threading.Lock()
last_speedtest_time = 0
mqtt_client = None
def bytes_to_mbps(bytes_per_sec):
return round((bytes_per_sec * 8) / 1_000_000, 2)
def run_speedtest():
global last_speedtest_time
current_time = time.time()
# Prevent running speedtests too frequently (minimum 30 seconds between runs)
if current_time - last_speedtest_time < 30:
print(f"β³ Speedtest skipped - too soon (minimum 30 seconds between runs)")
return None
with speedtest_lock:
print(f"Running speedtest...")
last_speedtest_time = current_time
try:
result = subprocess.run(["speedtest", "--format=json"], capture_output=True, text=True, check=True)
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
print("β Speedtest failed!")
print(f"Exit code: {e.returncode}")
print("STDOUT:")
print(e.stdout.strip())
print("STDERR:")
print(e.stderr.strip())
return None
def extract_summary(data):
# Parse the UTC ISO timestamp from Speedtest
utc_time = datetime.fromisoformat(data["timestamp"].replace("Z", "+00:00"))
# Convert to local system time (respects TZ env variable)
local_time = utc_time.astimezone() # β no arguments = use system local time
local_time_str = local_time.strftime("%d/%m/%Y %H:%M:%S %Z")
return {
"ping_ms": round(data["ping"]["latency"], 2),
"jitter_ms": round(data["ping"]["jitter"], 2),
"download_mbps": bytes_to_mbps(data["download"]["bandwidth"]),
"upload_mbps": bytes_to_mbps(data["upload"]["bandwidth"]),
"packet_loss": data.get("packetLoss", 0),
"external_ip": data["interface"]["externalIp"],
"isp": data["isp"],
"server": data["server"]["name"],
"result_url": data["result"]["url"],
"image_url": data["result"]["url"] + ".png",
"timestamp": local_time_str # ISO string in system timezone
}
def publish_discovery(client, sensor_id, name, unit, icon, value_template):
object_id = f"{DEVICE_NAME}_{sensor_id}"
unique_id = f"{DEVICE_NAME}_{sensor_id}"
topic = f"{DISCOVERY_PREFIX}/sensor/{DEVICE_NAME}/{sensor_id}/config"
payload = {
"name": f"Speedtest {name}",
"object_id": object_id,
"state_topic": f"{SENSOR_PREFIX}/{sensor_id}",
"unit_of_measurement": unit,
"value_template": value_template,
"unique_id": unique_id,
"device": {
"identifiers": [DEVICE_NAME],
"name": DEVICE_NAME.replace("_", " ").title(),
"manufacturer": DEVICE_MANUFACTURER,
"model": DEVICE_MODEL
},
"icon": icon
}
client.publish(topic, json.dumps(payload), retain=True)
def publish_camera_discovery(client):
topic = f"{DISCOVERY_PREFIX}/camera/{DEVICE_NAME}/result/config"
payload = {
"name": "Speedtest Result Image",
"unique_id": f"{DEVICE_NAME}_camera",
"topic": f"{SENSOR_PREFIX}/image", # not used here, but needed
"device": {
"identifiers": [DEVICE_NAME],
"name": DEVICE_NAME.replace("_", " ").title(),
"manufacturer": DEVICE_MANUFACTURER,
"model": DEVICE_MODEL
}
}
client.publish(topic, json.dumps(payload), retain=True)
def publish_camera_image(client, image_url):
try:
response = requests.get(image_url)
response.raise_for_status()
image_data = response.content # binary data
client.publish(f"{SENSOR_PREFIX}/image", image_data, retain=False)
except Exception as e:
print(f"Failed to download or publish image: {e}")
def publish_values(client, summary):
for key, value in summary.items():
if isinstance(value, (int, float, str)):
client.publish(f"{SENSOR_PREFIX}/{key}", value, retain=True)
def on_connect(client, userdata, flags, reason_code, properties=None):
print(f"Connected to MQTT broker with result code {reason_code}")
# Subscribe to the command topic
client.subscribe(COMMAND_TOPIC)
print(f"π‘ Subscribed to command topic: {COMMAND_TOPIC}")
def on_message(client, userdata, msg):
try:
topic = msg.topic
payload = msg.payload.decode()
print(f"π¨ Received message on {topic}: {payload}")
if topic == COMMAND_TOPIC and payload.lower() in ["run", "start", "execute", "go"]:
print("π Manual speedtest triggered via MQTT command")
# Run speedtest in a separate thread to avoid blocking
thread = threading.Thread(target=run_speedtest_and_publish, daemon=True)
thread.start()
else:
print(f"Unknown command or topic: {topic} -> {payload}")
except Exception as e:
print(f"Error processing MQTT message: {e}")
def connect_mqtt():
client = mqtt.Client(protocol=mqtt.MQTTv311, callback_api_version=CallbackAPIVersion.VERSION2)
if MQTT_USERNAME:
client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
# Set callbacks
client.on_connect = on_connect
client.on_message = on_message
client.connect(MQTT_BROKER_HOST, MQTT_BROKER_PORT, 60)
return client
def cleanup_mqtt():
"""Clean up MQTT connection and stop loops"""
global mqtt_client
if mqtt_client:
print("π Disconnecting from MQTT broker...")
mqtt_client.loop_stop()
mqtt_client.disconnect()
mqtt_client = None
def printEnvVars():
print(f"MQTT_BROKER_HOST: {MQTT_BROKER_HOST}")
print(f"MQTT_BROKER_PORT: {MQTT_BROKER_PORT}")
print(f"MQTT_USERNAME: {MQTT_USERNAME}")
print(f"MQTT_PASSWORD: {MQTT_PASSWORD}")
print(f"DISCOVERY_PREFIX: {DISCOVERY_PREFIX}")
print(f"SENSOR_PREFIX: {SENSOR_PREFIX}")
print(f"COMMAND_TOPIC: {COMMAND_TOPIC}")
print(f"DEVICE_MANUFACTURER: {DEVICE_MANUFACTURER}")
print(f"DEVICE_MODEL: {DEVICE_MODEL}")
print(f"DEVICE_NAME: {DEVICE_NAME}")
print(f"TZ: {os.getenv('TZ')}")
def run_speedtest_and_publish():
"""Run speedtest and publish results - used by both timer and MQTT commands"""
global mqtt_client
result = run_speedtest()
if result:
summary = extract_summary(result)
print(summary)
# Use the global MQTT client, create if needed (lazy initialization)
if not mqtt_client or not mqtt_client.is_connected():
print("π Connecting to MQTT broker...")
mqtt_client = connect_mqtt()
mqtt_client.loop_start()
publish_discovery(mqtt_client, "ping_ms", "Ping", "ms", "mdi:speedometer", "{{ value }}")
publish_discovery(mqtt_client, "jitter_ms", "Jitter", "ms", "mdi:chart-bell-curve", "{{ value }}")
publish_discovery(mqtt_client, "download_mbps", "Download", "Mbps", "mdi:download-network", "{{ value }}")
publish_discovery(mqtt_client, "upload_mbps", "Upload", "Mbps", "mdi:upload-network", "{{ value }}")
publish_discovery(mqtt_client, "packet_loss", "Packet Loss", "%", "mdi:percent", "{{ value }}")
publish_discovery(mqtt_client, "external_ip", "External IP", None, "mdi:ip-network", "{{ value }}")
publish_discovery(mqtt_client, "isp", "ISP", None, "mdi:access-point-network", "{{ value }}")
publish_discovery(mqtt_client, "server", "Server", None, "mdi:server", "{{ value }}")
publish_discovery(mqtt_client, "timestamp", "Timestamp", None, "mdi:clock", "{{ value }}")
publish_camera_discovery(mqtt_client)
publish_camera_image(mqtt_client, summary["image_url"])
publish_values(mqtt_client, summary)
def run_once():
"""Legacy function for backwards compatibility"""
run_speedtest_and_publish()
def run_interval_timer(interval):
"""Run speedtest at regular intervals in a separate thread"""
while True:
time.sleep(interval)
print(f"β° Scheduled speedtest triggered (interval: {interval}s)")
run_speedtest_and_publish()
if __name__ == "__main__":
# Print version on startup
version = get_version()
print(f"π Speedtest MQTT HA - Version: {version}")
test_mode = os.getenv("TEST_MODE", "0") == "1"
interval = int(os.getenv("SAMPLE_INTERVAL_SECONDS", "10800"))
printEnvVars()
if test_mode:
print("π§ͺ Running in test mode (once)...")
run_once()
# Clean up the connection after test mode
cleanup_mqtt()
else:
print(f"π Starting service with {interval} second intervals...")
print(f"π‘ MQTT command topic: {COMMAND_TOPIC}")
print("Send 'run', 'start', 'execute', or 'go' to trigger manual speedtest")
# Connect to MQTT and start listening for commands
mqtt_client = connect_mqtt()
mqtt_client.loop_start()
# Start the interval timer in a separate thread
timer_thread = threading.Thread(target=run_interval_timer, args=(interval,), daemon=True)
timer_thread.start()
# Run initial speedtest
print("π Running initial speedtest...")
run_speedtest_and_publish()
try:
# Keep the main thread alive
while True:
time.sleep(1)
except KeyboardInterrupt:
print("\nπ Shutting down...")
cleanup_mqtt()