The heart of the script is making GET requests with authentication. Is there a better way to do this?
jsonTokenResponse = requests.get('https://data.tankutility.com/api/getToken', auth=HTTPBasicAuth(TankUser, TankPassword)).json()
jsonDeviceResponse = requests.get('https://data.tankutility.com/api/devices?token=' + jsonTokenResponse["token"]).json()
# below is querying the first tank in the account - adjust the [0] if you want other tanks
jsonTankDataResponse = requests.get('https://data.tankutility.com/api/devices/' + jsonDeviceResponse["devices"][0] + '?token=' + jsonTokenResponse["token"]).json()
GET requests can be done through HTTP Actions. Work is progressing on a 2.x HTTP binding which will (hopefully) incorporate these Actions. There are also several Java libraries. If these are better is subjective… but they work. If you are having difficulty installing requests, you may find these easier to get going.
The Actions are okay, but the authentication is a bit painful with them as you have to do it yourself with encoding it into a header string (unless I missed something). I was looking forward to easily porting Python scripts as it makes this much easier. I’ve already got the script running externally as a Python script, so I can take some time to debug this.
If you have requests installed, you just need to make them accessible in the classpath (python.path) to be able to use it within Jython. Based on your post, you were installing to
/etc/openhab2/automation/lib/python
Is it in this directory? If not, you can add your Python2 module directory to the python.path or create a symlink to it.
That appears to almost work for me. The automation/lib/python directory does get populated with a requests directory that has the files in it. And the “import requests” line works. Progress.
But the next line fails:
from requests.auth import HTTPBasicAuth
Error during evaluation of script ‘file:/etc/openhab2/automation/jsr223/python/personal/tankUtil.py’: ImportError: No module named auth in at line number 8
There is an auth.py and auth.pyc in the requests directory. Any idea why that can’t be found?
thanks
EDIT: happened to reboot, and now it appeared to load. Not sure why that is needed, but it looks better. Let me test this a bit further. Thanks
EDIT #2: onward to the next problem. This does indeed load requests, but then it breaks exactly as seen here. That’s the new challenge. I was hoping that Jython would drop in for Python, but I guess not.
That works - thank you. I originally didn’t read your post carefully enough, but it does indeed install. But I hit this problem now as seen in an another thread, so I’ll try to follow that. I don’t think it is related to importing - that’s fine with your pip install line.
I had the same problem. The problem is not that I cannot extend the system path or configure my PYTHONPATH in /etc/default/openhab. The problem is that the openhab user needs to access it and this is not the user I work with on my operating system. I am using Linux in a virtual machine. Maybe this even works with openhabian, I can’t guarantee that.
You can install the package as follows, for example: