-
Notifications
You must be signed in to change notification settings - Fork 0
/
abbusage_ha.sh
82 lines (69 loc) · 3.22 KB
/
abbusage_ha.sh
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
#!/bin/bash
cd "$(dirname "$0")"
# Checking credentials have been entered
if [[ -e "abbcreds.json" ]]
then
abbcreds=$(cat abbcreds.json)
else
echo "EXITING. *****Credentials not found.***** Please run abbcreds.sh script to set up user details"
exit
fi
# Aussie Broadband Details
usageid=$(echo "$abbcreds" | jq -r '.usageid')
# Check Cookie has more than half life till expiry
epoch_expire=$(grep 'myaussie_cookie' abbcookie.txt)
epoch_expire=$(echo $epoch_expire | cut -d' ' -f5 -)
todaydatetime=$(date +%s)
abbtoken=$(cat abbtoken.json)
refreshTokenExpires=$(echo "$abbtoken" | jq '.expiresIn')
refreshTokenExpires=$(($refreshTokenExpires / 2))
daysleftcookie=$(($epoch_expire - $todaydatetime - $refreshTokenExpires))
if [[ $daysleftcookie < 0 ]]
then
refreshToken=$(echo "$abbtoken" | jq -r '.refreshToken')
curl -c abbcookie.txt -b abbcookie.txt -d "refreshToken=$refreshToken" -X PUT --url 'https://myaussie-auth.aussiebroadband.com.au/login' > abbtoken.json
fi
# Home Assistant Config
server=$(echo "$abbcreds" | jq -r '.server')
token=$(echo "$abbcreds" | jq -r '.token')
entitypicture=$(echo "$abbcreds" | jq -r '.entitypicture')
# Retrieving ABB Usage Data
cookie=abbcookie.txt
abbusagestring=$(curl -b $cookie --url "https://myaussie-api.aussiebroadband.com.au/broadband/$usageid/usage")
# Get Variables from String
usedMb=$(echo "$abbusagestring" | jq '.usedMb')
downloadedMb=$(echo "$abbusagestring" | jq '.downloadedMb')
uploadedMb=$(echo "$abbusagestring" | jq '.uploadedMb')
remainingMb=$(echo "$abbusagestring" | jq '.remainingMb')
daysTotal=$(echo "$abbusagestring" | jq '.daysTotal')
daysRemaining=$(echo "$abbusagestring" | jq '.daysRemaining')
lastUpdated=$(echo "$abbusagestring" | jq -r '.lastUpdated')
# Calculate Data Limit
if [ "$remainingMb" == "null" ]
then
allowanceMb=100000000
else
allowanceMb=$(echo "$(($usedMb + $remainingMb))")
fi
# Calculate Dates
todaydatetime=$(date -Is)
lastUpdatedISO=$(echo "$todaydatetime" |sed "s/${todaydatetime:11:8}/${lastUpdated:11:8}/g")
lastUpdated=$(date -d "$lastUpdated" +%s)
daysRemainingEpochs=$(($daysRemaining*86400))
nextRollover=$(($lastUpdated + $daysRemainingEpochs))
nextRollover=$(date -d @"$nextRollover" -Is)
nextRollover=$(echo "$nextRollover" |sed "s/${nextRollover:11:8}/00:00:00/g")
lastUpdated=$lastUpdatedISO
nextRollover=$(echo "$nextRollover" | sed "s/.\{2\}$/:&/")
nextRollover=$(echo "$nextRollover" | sed "s/::/:/g")
lastUpdated=$(echo "$lastUpdated" | sed "s/.\{2\}$/:&/")
lastUpdated=$(echo "$lastUpdated" | sed "s/::/:/g")
# Build daysUsed from daysTotal and daysRemaining
daysUsed=$(echo "$(($daysTotal - $daysRemaining))")
# Build JSON output
abbusagestring='{"state":"","attributes":{"usage":"","usedMb":'"$usedMb"',"downloadedMb":'"$downloadedMb"',"uploadedMb":'"$uploadedMb"',"remainingMb":'"$remainingMb"',"daysTotal":'"$daysTotal"',"daysRemaining":'"$daysRemaining"',"lastUpdated":"'"$lastUpdated"'","nextRollover":"'"$nextRollover"'","daysUsed":'"$daysUsed"',"allowanceMb":'"$allowanceMb"',"friendly_name":"ABB Usage","icon":"mdi:blank","entity_picture":"'"$entitypicture"'"}}'
# Publish to HA with token
curl -X POST -H "Authorization: Bearer $token" \
-H "Content-Type: application/json" \
-d "$abbusagestring" \
"$server"/api/states/sensor.abb_usage