Comcast/Xfinity Data Usage Sensor

After finding this Cox Data Usage Sensor, I wanted to see if anyone who has Comcast has implemented anything similar before I try to modify this script to pull in the Xfinity data from my account page.

I have not had time to make a proper sensor component for this, but I’ve setup the following:
image

Setup:

  • install python package xfinity-usage
    – requires chromium-chromedriver (apt install on Ubuntu)
  • create cronjob such as (Comcast updates around midnight):
    # Daily at 4am
    0 4 * * *  /home/homeassistant/.homeassistant/scripts/xfinity_usage.sh -c
    
  • bash relay/wrapper script (you’ll need to adjust any paths): /home/homeassistant/.homeassistant/scripts/xfinity_usage.sh
#!/bin/bash

HASS_PATH=/home/homeassistant/.homeassistant
XFINITY_USAGE=/srv/homeassistant/bin/xfinity-usage
DATA_FILE=$HASS_PATH/xfinity-usage.data


# set creds
export XFINITY_USER="your-comcast-email-address"
export XFINITY_PASSWORD="your-account-password"


capture_usage() {
  # get usage report
  usage_str=$($XFINITY_USAGE -b chrome-headless)
  used=$(echo $usage_str  | cut -d' ' -f2)
  total=$(echo $usage_str | cut -d' ' -f4)
  unit=$(echo $usage_str  | cut -d' ' -f5)

  # print formatted result
  echo "$used / $total" > $DATA_FILE
  echo "$(cat $DATA_FILE)"

  # clean up
  rm -f $PWD/*.png
}


read_usage() {
  if [ -e $DATA_FILE ]; then
    echo "$(cat $DATA_FILE)"
  else
    echo "Unknown"
  fi
}


print_help() {
  echo "Usage: $(basename $0) command"
  echo
  echo "  Option        Description"
  echo "   -c           Capture new usage data"
  echo "   -u           Read captured usage data"
  echo "   -h           Print this help"
  echo
}


# take action based on command
case "$1" in
  "-c"|"--capture")
    capture_usage
    ;;

  "-u"|"--usage")
    read_usage
    ;;

  "-h"|"--help")
    print_help
    ;;

  *)
    echo -e "ERR: Invalid command!\n"
    print_help
    exit 1
    ;;
esac

# cya!
exit 0
  • Home Assistant component:
    - platform: command_line
      name: 'Xfinity Usage'
      unit_of_measurement: GB
      scan_interval: 86400  # 24hr
      command: "bash /home/homeassistant/.homeassistant/scripts/xfinity_usage.sh -u"
    

How heavy is this to run? Seems that since it uses a chromium instance, I’d guess it’s fairly heavy on the pi.

It seems that this is the one to use now. It does not appear to use a chromium instance, and instead just connects HTTPS directly and pulls the data from the content.

Thanks for sharing, I’m switching over and if time permits I’d like to make a legit component out of that source.

I ended up modifying and running the script that @silvrr was using. It’s a python script that grabs the json data through the comcast API, so this is probably the most efficient want to run it. I run it once a day at 5AM and it works great. The only issue is that if you exceed the 1TB and choose to pay for unlimited data, it breaks the script so I’ve made some changes in order to make it work a bit better.

https://github.com/SilvrrGIT/HomeAssistant/issues/105

1 Like

@poldim Do you have a copy of your current script? What I’m seeing isn’t working right now.

I have made a component for retrieving Xfinity usage data. It’s functional, but I’m splitting a piece to submit to pypi before I move forward. However, you could place the component into custom_components and it’ll work fine.

See changes in branch feature/add-xfinity-usage:

3 Likes

Thanks for sharing Robert. Your component works perfectly

1 Like

Thank you! I hope to soon commit this into a custom component repo of its own…and after I publish the pypi package, also try to get this into Hass.

Also, I’ll be adding more information from the Jason returned to provide more Comcast usage info.

Hey @ralfaro,

Love the sensor and have been using it a ton lately. I’ll have to double check it but the other day I started getting the following error:

Failed to fetch data, status_code:401

Also, any update on getting this moved into a custom component or into HASS?

Hi @john17,

Thanks! It’s been working solid for me at 1hr interval. I did notice a couple of weeks ago a connection error (perhaps code 401) that lasted about a day. When I noticed this happening, I went over to xfinity.com to check internet usage and upon logging in a prompt appeared to confirm/accept a message (I cannot remember if it was privacy agreement or what). After clearing the prompt, the integration was healed. See if you have a similar experience.

Regarding making this more official, I’m not sure how official it can be – need to adhere by the guidelines. I was planning to get the python package uploaded but coincidentally that’s when I noticed the error above and did not proceed. I’ll try to get it soon. This will help streamline the use of the integration as a custom_component (even through HACS).

I’ll be uploading a PG&E (gas/electric company) integration that is very similar to this one soon too.

That’s exactly what it was! I think it was a screen confirming your contact information and maybe also asking if you want to enable 2fa.

I guess becoming a custom_component (through HACS) would be nice to be consistent with my other custom components. Unfortunately I don’t use PG&E, but if you wanted to do Eversource (North East of the US) I certainly wouldn’t complain!

I wish I had more data from the http response to see if I can detect prompts or whatever.

Anyways, nice to see it’s working!

Hello Robert, @ralfaro

Any chance of getting this into a HACS custom component? If not, I will just install manually.

I assume the config info needed is the earlier post from

[https://github.com/astromgren/Comcast_Data_Usage]

Thanks

Hey there! I actually (for real this time) plan on working on this (and other components) very soon. I’ve finally got a good amount of time off work starting this week. Stay tuned!

If you’re in SF, I made an SFPUC scraper too

Very similar to Comcast component, I’ve got one for PGE I use hourly.

@toofewacres I made a custom component out of it…hacs compatible

Thanks, I’ll give it a try to night when I get home.