-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added X-Finity sensors
- Loading branch information
Showing
8 changed files
with
185 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"used": "5", | ||
"total": "10", | ||
"unit": "Gb" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#!/usr/bin/python3 | ||
from __future__ import print_function | ||
|
||
import json | ||
import logging | ||
import os | ||
import re | ||
import requests | ||
import time | ||
|
||
#username = os.environ['COMCAST_USERNAME'] | ||
#password = os.environ['COMCAST_PASSWORD'] | ||
username = 'turbosteff' | ||
password = 'kcplus01' | ||
json_file = "/json_files/comcast_usage.json" | ||
#json_file = "/Users/artdavis/.homeassistant/json_files/comcast_usage.json" | ||
|
||
logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.DEBUG) | ||
logging.getLogger('requests').setLevel(logging.ERROR) | ||
|
||
session = requests.Session() | ||
|
||
logger.debug("Finding req_id for login...") | ||
res = session.get('https://customer.xfinity.com/oauth/force_connect/?continue=%23%2Fdevices') | ||
#res = session.get('https://login.comcast.net/login?r=comcast.net&s=oauth&continue=https%3A%2F%2Flogin.comcast.net%2Foauth%2Fauthorize%3Fclient_id%3Dmy-account-web%26redirect_uri%3Dhttps%253A%252F%252Fcustomer.xfinity.com%252Foauth%252Fcallback%26response_type%3Dcode%26state%3D%2523%252Fdevices%26response%3D1&client_id=my-account-web') | ||
assert res.status_code == 200 | ||
m = re.search(r'<input type="hidden" name="reqId" value="(.*?)">', res.text) | ||
req_id = m.group(1) | ||
logger.debug("Found req_id = %r", req_id) | ||
|
||
data = { | ||
'user': username, | ||
'passwd': password, | ||
'reqId': req_id, | ||
'deviceAuthn': 'false', | ||
's': 'oauth', | ||
'forceAuthn': '1', | ||
'r': 'comcast.net', | ||
'ipAddrAuthn': 'false', | ||
'continue': 'https://oauth.xfinity.com/oauth/authorize?client_id=my-account-web&prompt=login&redirect_uri=https%3A%2F%2Fcustomer.xfinity.com%2Foauth%2Fcallback&response_type=code&state=%23%2Fdevices&response=1', | ||
'passive': 'false', | ||
'client_id': 'my-account-web', | ||
'lang': 'en', | ||
} | ||
|
||
logger.debug("Posting to login...") | ||
res = session.post('https://login.xfinity.com/login', data=data) | ||
assert res.status_code == 200 | ||
|
||
logger.debug("Fetching internet usage AJAX...") | ||
res = session.get('https://customer.xfinity.com/apis/services/internet/usage') | ||
#logger.debug("Resp: %r", res.text) | ||
assert res.status_code == 200 | ||
|
||
js = json.loads(res.text) | ||
|
||
out = { | ||
'raw': js, | ||
'used': js['usageMonths'][-1]['homeUsage'], | ||
'total': js['usageMonths'][-1]['allowableUsage'], | ||
'unit': js['usageMonths'][-1]['unitOfMeasure'], | ||
} | ||
print(json.dumps(out)) | ||
|
||
# Print JSON to file | ||
with open(json_file, 'w+') as outfile: | ||
json.dump(out, outfile, sort_keys=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
######################### | ||
# Command Line # | ||
######################### | ||
# | ||
#- platform: command_line | ||
# command: date +"%d" | ||
# name: Day Of Current Month | ||
# scan_interval: 3600 | ||
# | ||
#- platform: command_line | ||
# command: cal $(date +"%m %Y") | awk 'NF {DAYS = $NF}; END {print DAYS}' | ||
# name: Days In Current Month | ||
# scan_interval: 3600 | ||
|
||
- platform: file | ||
name: Comcast Utilization | ||
file_path: /Users/artdavis/.homeassistant/json_files/comcast_usage.json | ||
# /Users/artdavis/.homeassistant/json_files/comcast_usage.json | ||
# file_path: '/.homeassistant/json_files/comcast_usage.json' | ||
# file_path: /home/homeassistant/.homeassistant/comcast/comcast_usage.json | ||
value_template: > | ||
{% if value_json is defined %} | ||
{% if value_json.used | int == 0 and value_json.total | int == 0 %} | ||
stats unavailable | ||
{% else %} | ||
{{ value_json.used | int }} / {{ value_json.total | int }} GB ({{ ((value_json.used / value_json.total)*100) | round(1) }}%) | ||
{% endif %} | ||
{% else %} | ||
undefined | ||
{% endif %} | ||
- platform: file | ||
name: Comcast Avg GB Current | ||
file_path: /Users/artdavis/.homeassistant/json_files/comcast_usage.json | ||
value_template: > | ||
{% if value_json is defined %} | ||
{% if value_json.used | int == 0 and value_json.total | int == 0 %} | ||
stats unavailable | ||
{% else %} | ||
{{ ((value_json.used | int) / (states.sensor.dayofthemonth.state | int)) | round(1) }} GB per day | ||
{% endif %} | ||
{% endif %} | ||
- platform: file | ||
name: Comcast Avg GB Left | ||
file_path: /Users/artdavis/.homeassistant/json_files/comcast_usage.json | ||
value_template: > | ||
{% if value_json is defined %} | ||
{% if value_json.used | int == 0 and value_json.total | int == 0 %} | ||
stats unavailable | ||
{% else %} | ||
{{ (((value_json.total | int) - (value_json.used | int)) / ((states.sensor.sensor.days_in_current_month.state | int ) - (states.sensor.sensor.dayofthemonth.state | int))) | round(1) }} GB per day | ||
{% endif %} | ||
{% else %} | ||
undefined | ||
{% endif %} |