Could we swing something together do you think? Maybe even make it somewhat modular so that others could create their own python backends to read in the values you need to place in the component UI?
Ya, I’ll take a look tomorrow, like I said I’m so python guru. If you run that script you linked now what does the output look like? If it’s something usable all you’ll need to do is get it into a JSON format then let HA consume it.
Due to my inability to figure out how to upgrade Python 2 inside the Docker version of Home Assistant (I believe I was hitting an outdated OpenSSL issue), I ended up revising the script to use Mechanical Soup, since Mechanize is no longer being actively developed.
Looks like your revisions are along the same path as I took when I noticed Cox changed their code and the position of the needed script element moved.
I also decided to output the JSON to a file, so the sensors don’t need to trigger recursive connections to Cox’s site. Here’s my revision, in case anyone finds it useful.
@ntalekt I’m still struggling to get my IoT VLAN to send requests to Cox out the Cox WAN, rather than the VPN WAN, because Cox seems to refuse authentication requests from my VPN connection… so I haven’t implemented the HA side yet, but I was intending to use HA’s RESTful Sensor. I think you could output the JSON file into HA’s www folder, so HA would serve up the file itself.
I had the same problem before changing my network architecture and where I terminate my VPNs. In the past I had a DD-WRT router and was able to to setup policy based routing via IPTABLES. That why I could choose which devices traversed the VPN vs. the normal WAN.
Automation calls a shell_command for the python script
Python script scrapes the utilization data and writes the JSON to a file
File Sensors read the JSON file and make the data available in HA
A few things learned:
The File Sensors choked on the JSON output when “indent=4” was in the json.dump parameters. So I removed that.
My Cox billing cycle restarts today. Got to see that edge case difference in their data. Added conditionals to the File Sensors to cover that.
The File Sensor component doesn’t have icon_template functionality. I may abstract the File Sensor data into Template Sensors so that I can have conditional icon display for when my data is running low.
Thanks for this, nice way to track ISP utilization – I forked this for my ISP, CableONE as well on GitHub – I might recommend putting some of your values in sensor.template as I did – maybe a little easier to read and one less json read and parse, but probably fine either way.
Updated my configuration to call the update script from cron and that worked for a while but now it’s failing with:
Traceback (most recent call last):
File "/cox/cox-data-usage.py", line 42, in <module>
login_form.input({'username': cox_user, 'password': cox_pass})
File "/srv/cox/lib/python3.6/site-packages/mechanicalsoup/form.py", line 59, in set_input
i = self.form.find("input", {"name": name})
AttributeError: 'NoneType' object has no attribute 'find'
I imagine this is a missing module on the container that this is running on but I can’t find the local change that introduced this failure. Any ideas?
Wow i wish i searched here before i made my own sensor.
No big deal, but i plan on building out out a little more to include all the info that this example contains.
If others want to use my sensor feel free to.
I would like to display this in a graph. Maybe in Grafana so I can quickly see how we are doing. So a line with average usage and then where we are in relation to that, so we can quickly see if we are pushing it. Any suggestions?