magico13 / PyEmVue

Python Library for the Emporia Vue Energy Monitor
MIT License
185 stars 36 forks source link

auth.py unsupported operand type(s) for |: 'type' and 'NoneType' #64

Closed dalklein closed 4 months ago

dalklein commented 4 months ago

Thanks for this great tool! I'm one of those marginal programming ability folks that you're really helping out. I wouldn't be able to do any of this crazy stuff without all the great projects that people so gracefully share with the world.

I use it to control the EVSE from node-red, generally like the emporia excess solar feature, but that doesn't work very well due to how my solar & battery system is set up. The RPi has more info about what's going on from several arrays and other opportunity loads or switching some arrays completely to grid export, etc. I had it working a while back, then gave up on it when a beta firmware update to the EVSE for testing a feature seemed to break it, then the RPi ate the SD card and got a new SSD. Finally now I'm setting up pyemvue control of the EVSE again.

Ran it from a laptop and it worked right away, as before. in a venv, pip install pyemvue, and pip install pycognito==2021.3.1 when that other error showed up. The previous .py scripts to control the EVSE work fine.

Same install method on the raspberry pi, but, it comes up with this error right at initializing pyemvue: (user & pwd hardcoded in test_login.py in this example, that's why no prompt for it)

(.venv) pi@SMA-RPI:~/PyEmVue $ python3 test_login.py
Traceback (most recent call last):
  File "/home/pi/PyEmVue/test_login.py", line 1, in <module>
    from pyemvue.pyemvue import PyEmVue
  File "/home/pi/PyEmVue/.venv/lib/python3.9/site-packages/pyemvue/__init__.py", line 2, in <module>
    from pyemvue.pyemvue import PyEmVue
  File "/home/pi/PyEmVue/.venv/lib/python3.9/site-packages/pyemvue/pyemvue.py", line 8, in <module>
    from pyemvue.auth import Auth, SimulatedAuth
  File "/home/pi/PyEmVue/.venv/lib/python3.9/site-packages/pyemvue/auth.py", line 101, in <module>
    class SimulatedAuth(Auth):
  File "/home/pi/PyEmVue/.venv/lib/python3.9/site-packages/pyemvue/auth.py", line 102, in SimulatedAuth
    def __init__(self, host: str, username: str | None = None, password: str | None = None):
TypeError: unsupported operand type(s) for |: 'type' and 'NoneType'

editing the auth.py file, this change made it work. it connects to the cloud either with keys.json, or with username and password hardcoded or from input. (So I'm all set, just in case this might help anyone else)

class SimulatedAuth(Auth):
#    def __init__(self, host: str, username: str | None = None, password: str | None = None): 
    def __init__(self, host: str, username: str, password: str):
        self.host = host

here are the versions of everything on the RPi (left) and the laptop (right). The main difference is RPi python 3.9.2 vs. 3.11 ? Curious that cryptography version is higher on RPi with python3.9 ? urllib3 is different. pip & setuptools probably just go with the different python versions. Screenshot from 2024-02-23 22-02-49

Thanks again!

dalklein commented 4 months ago

for entertainment, the red line in the top plot is the EVSE commanded charge current amps, from the RPi Node-RED controls. (not getting through to the EVSE in this case, but tomorrow, it will! And the utility is getting none of my electricity during off-peak for no-good outflow compensation, it's going in the car baby!) Screenshot from 2024-02-23 22-24-43

magico13 commented 4 months ago

You don't need the SimulatedAuth class at all unless you want to point at the simulator I was working on building, so if it's only complaining about that when used then you should be able to avoid making changes. If it's upset just because it exists in that file then your workaround is fine. I suspect it's only an issue because that form of marking something optional is newer than what's supported in Python 3.9, I can probably swap to Optional[str] for it to work with older Python versions.

dalklein commented 4 months ago

Thanks for explaining.