Skip to content

Commit

Permalink
Add sent bytes and fix server id
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSuicideParrot committed Apr 20, 2024
1 parent 29f579c commit 1b127ec
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions speedtest/rootfs/etc/services.d/speedtest/run
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@ function post_result() {
local serverid=${5}
local servername=${6}
local servercountry=${7}
local bytes=${8}


if "$sensor" == "ping"; then
local data="{\"state\":$state, \"attributes\":{\"state_class\":\"measurement\",\"unit_of_measurement\":\"$unitmeasure\",\"friendly_name\":\"$friendlyname\",\"icon\":\"mdi:speedometer\",\"server_name\":$servername,\"server_country\":$servercountry,\"server_id\":\"$serverid\"}}"
elif "$sensor" == "download"; then
local data="{\"state\":$state, \"attributes\":{\"state_class\":\"measurement\",\"unit_of_measurement\":\"$unitmeasure\",\"friendly_name\":\"$friendlyname\",\"icon\":\"mdi:speedometer\",\"server_name\":$servername,\"server_country\":$servercountry,\"server_id\":\"$serverid\",\"bytes_received\":$bytes}}"
else
local data="{\"state\":$state, \"attributes\":{\"state_class\":\"measurement\",\"unit_of_measurement\":\"$unitmeasure\",\"friendly_name\":\"$friendlyname\",\"icon\":\"mdi:speedometer\",\"server_name\":$servername,\"server_country\":$servercountry,\"server_id\":\"$serverid\",\"bytes_sent\":$bytes}}"
fi

local data="{\"state\":$state, \"attributes\":{\"state_class\":\"measurement\",\"unit_of_measurement\":\"$unitmeasure\",\"friendly_name\":\"$friendlyname\",\"icon\":\"mdi:speedometer\",\"server_name\":$servername,\"server_country\":$servercountry,\"server_id\":$serverid}}"

bashio::api.supervisor POST "/core/api/states/sensor.speedtest_$sensor" "$data"

bashio::log.info "$friendlyname updated"
Expand All @@ -47,7 +55,9 @@ fi
bashio::log.debug "Speedtest output: ${RESULT}"

export down_load_speed=$(printf %.2f "$(($(echo $RESULT | jq .download.bandwidth)*8/10000))e-2")
export down_bytes=$(echo $RESULT | jq .download.bytes)
export up_load_speed=$(printf %.2f "$(($(echo $RESULT | jq .upload.bandwidth)*8/10000))e-2")
export up_bytes=$(echo $RESULT | jq .upload.bytes)
export ping_latency=$(echo $RESULT | jq .ping.latency)
export used_server_id=$(echo $RESULT | jq .server.id)
export used_server_name=$(echo $RESULT | jq .server.location) # This is exact the same behaviour of the native HomeAssistant speedtest
Expand All @@ -62,6 +72,6 @@ bashio::log.info "Server id: $used_server_id"
bashio::log.info "Server country: $used_server_country"
bashio::log.info "--------- ---------"

post_result "download" "Mbit/s" "Speedtest Download" $down_load_speed $used_server_id $used_server_name $used_server_country
post_result "upload" "Mbit/s" "Speedtest Upload" $up_load_speed $used_server_id $used_server_name $used_server_country
post_result "download" "Mbit/s" "Speedtest Download" $down_load_speed $used_server_id $used_server_name $used_server_country $down_bytes
post_result "upload" "Mbit/s" "Speedtest Upload" $up_load_speed $used_server_id $used_server_name $used_server_country $up_bytes
post_result "ping" "ms" "Speedtest Ping" $ping_latency $used_server_id $used_server_name $used_server_country

0 comments on commit 1b127ec

Please sign in to comment.