Skip to content

Commit

Permalink
9-13-19 Update
Browse files Browse the repository at this point in the history
Added X-Finity sensors
  • Loading branch information
kartcon committed Sep 14, 2019
1 parent 78ca24a commit a1713c5
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 28 deletions.
18 changes: 18 additions & 0 deletions automations/scheduled_events.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,22 @@
- service: notify.ios_group_art_steff
data:
message: "Reminder: Change the furnace filter."
##
#############################################################
# Query Comcast Data Usage
#############################################################
- id: query_comcast_data_usage
alias: 'Query Comcast Data Usage'
initial_state: true
trigger:
platform: time_pattern
hours: '/6'
minutes: 0
seconds: 0
action:
service: shell_command.query_comcast_data_usage
#
# "used": "5",
# "total": "10",
# "unit": "Gb"
#
22 changes: 12 additions & 10 deletions configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ homeassistant:
# http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
customize: !include customize.yaml
time_zone: !secret timezone
whitelist_external_dirs:
- /Users/artdavis/.homeassistant/json_files
#
# ---------------
# 0.90 setup see:
# https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/config/configuration.yaml
Expand Down Expand Up @@ -158,7 +161,7 @@ hacs:
# ---------------
# Own Tracks
owntracks:
max_gps_accuracy: 65
max_gps_accuracy: 50
events_only: false
# waypoints: true
#
Expand All @@ -181,6 +184,13 @@ rachio:
api_key: !secret rachio_api_key
#
# ---------------
# Shell Commands
shell_command:
query_comcast_data_usage: 'python /python_scripts/comcast.py'
# image_classify: bash /home/homeassistant/.homeassistant/shell_scripts/image_classification.sh "{{camera}}"
# facebox: bash /home/homeassistant/.homeassistant/shell_scripts/facebox.sh "{{camera}}"
#
# ---------------
# Sleep IQ Bed
sleepiq:
username: !secret sleepiq_un
Expand All @@ -189,15 +199,7 @@ sleepiq:
# ---------------
# Speed Test
# https://www.home-assistant.io/components/speedtestdotnet/
speedtestdotnet:
monitored_conditions:
- ping
- download
- upload
#- speedtestdotnet:
# scan_interval:
# hours: 6
# minutes: 0
#speedtestdotnet:
# monitored_conditions:
# - ping
# - download
Expand Down
4 changes: 2 additions & 2 deletions includes/binary_sensors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
#
- platform: ping
name: Ping Sunroom Sonos
host: !secret sonos_sunroom_url
host: !secret sonos_black_url
scan_interval: 300
#
- platform: ping
name: Ping Bedroom Sonos
host: !secret sonos_bedroom_url
host: !secret sonos_white_url
scan_interval: 300
#
- platform: ping
Expand Down
5 changes: 5 additions & 0 deletions json_files/comcast_usage.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"used": "5",
"total": "10",
"unit": "Gb"
}
36 changes: 20 additions & 16 deletions lovelace/media_players.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,48 +115,52 @@ cards:
- type: custom:mini-media-player
entity: media_player.kitchen_echo
artwork: none
tts:
platform: alexa
entity_id: media_player.kitchen_echo
toggle_power: false
hide:
controls: false
volume: false
source: true
power: true
toggle_power: false
tts:
platform: alexa
entity_id: media_player.kitchen_echo
#
- type: custom:mini-media-player
entity: media_player.office_echo
artwork: none
tts:
platform: alexa
entity_id: media_player.office_echo
toggle_power: false
hide:
controls: false
volume: false
source: true
power: true
toggle_power: false
tts:
platform: alexa
entity_id: media_player.office_echo
#
- type: custom:mini-media-player
entity: media_player.sunroom_echo_white
artwork: none
tts:
platform: alexa
entity_id: media_player.sunroom_echo_white
toggle_power: false
hide:
controls: false
volume: false
source: true
power: true
toggle_power: false
tts:
platform: alexa
entity_id: media_player.sunroom_echo_white
#
- type: custom:mini-media-player
entity: media_player.sunroom_echo_black
artwork: none
tts:
platform: alexa
entity_id: media_player.sunroom_echo_black
toggle_power: false
hide:
controls: false
volume: false
source: true
power: true
toggle_power: false
tts:
platform: alexa
entity_id: media_player.sunroom_echo_black
#
68 changes: 68 additions & 0 deletions python_scripts/comcast.py
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)
4 changes: 4 additions & 0 deletions scripts/user_briefings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ steff_briefing_one:
test_briefing_one:
alias: Test Briefing One
sequence:
- service: media_player.volume_set
data:
entity_id: media_player.office_echo
volume_level: '0.3'
- service: notify.alexa_media
data_template:
target:
Expand Down
56 changes: 56 additions & 0 deletions sensors/comcast.yaml
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 %}

0 comments on commit a1713c5

Please sign in to comment.