indykoning / PyPi_GrowattServer

MIT License
70 stars 32 forks source link

Issue with api #49

Closed lcsztl closed 1 year ago

lcsztl commented 1 year ago

It was working good but I start to receive this message instead

Traceback (most recent call last): File "C:\Users\lucas\PycharmProjects\teste\main.py", line 13, in login_response = api.login(gw_username, gw_user_pass) File "C:\Users\lucas\PycharmProjects\teste\venv\lib\site-packages\growattServer__init.py", line 132, in login data = json.loads(response.content.decode('utf-8'))['back'] File "C:\Users\lucas\AppData\Local\Programs\Python\Python310\lib\json\init__.py", line 346, in loads return _default_decoder.decode(s) File "C:\Users\lucas\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\lucas\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Sjord commented 1 year ago

See also Dont work from now? · Issue #48 · indykoning/PyPi_GrowattServer

mvf-ss commented 1 year ago

It might be worth failing gracefully - with an error message including the response - when the login fails. (I'm not a python programmer and it's just taken me a couple of hours of stepping through a .py file with pdb to find out why the code is dying with a JSON decode error. Takes me back to debugging in the '70s on a teletype terminal.) I guess I could/should have come here first. Maybe an error message could even include the suggestion to visit this site if the response is 403.

indykoning commented 1 year ago

This issue seems to be caused by Growatt returning an unexpected http code and this package not handling that correctly. It may have to do with the user agent being needed to be randomised https://github.com/indykoning/PyPi_GrowattServer#initialisation

I do not believe this package should silently handle these errors since that can cause unexpected behavior in applications implementing this package, however the thrown exception can be clearer and must be easier to be properly caught and debugged. This package will throw proper http exceptions in case the response code is not what is expected from https://github.com/indykoning/PyPi_GrowattServer/releases/tag/1.3.1 on 🙂

as an example

requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://server-api.growatt.com/newTwoLoginAPI.do
typxxi commented 1 year ago

UPDATE: I had been writing down all the steps I did and where I was struggling , how I came to this site and on the way also seemed to find a solution which other newbies coming from windows / a bit of raspberry python scripting + homeassistant work might help those too. So this is a trial and error story hopefully to a success.

I am total new to the whole idea, had played a bit with python before under raspi so can code a bit, use micropython for some Pimoroni BADGER PICO adventures, but here I am stuck richt now and got your error message mentioned in the last line.

But here is my starting point and steps done.

1

I am Running Windows 10 and I have installed Python 3.10 incl. PIP modules following some advice found here in south africa https://powerforum.co.za/topic/11621-raw-access-to-growatt-inverter-data/?do=findComment&comment=121451, where a lot had been described to have a good starting point.

2

Python 3.10 installation seemed to work so far and I went on: opened powershell administrator and used there the recommanded install procedure for growattServer with
pip install growattServer

Looked like that had worked fine too cause since then i get a "requirement already satisfied" message if I had tried the install command again with: pip install growattServer

3

I tried a python script called Hello world from the command line and had success there with a "Hello world" output. I typed into the powershell command line 'pyhton scripname.py' in this case 'Hello_World.py' which then worked fine with a 'Hello World' reply just to make sure that the basics are working cause I had never used pyhton on wiindows before, only thonny and micropython on a connected pico which is a total differen beast.

4

Then I downloaded your zip file from here and unpacked into a PyPi_GrowattServer folder

5

I went to the powershell (administrator) again and use 'cd' to get into the 'examples' directory. There I had edited first the simple.py by replacing with the real Growatt "username" used in the android app and "password", so replace everything else.

That looked great so far - but did not pay of cause did not work. I got this error message:

  File "C:\Users\typ_xxi\Desktop\- PICO W & BADGER -\PyPi_GrowattServer\examples\simple.py", line 4, in <module>
    login_response = api.login("joan", "joanjett")
  File "C:\Users\typ_xxi\AppData\Local\Programs\Python\Python310\lib\site-packages\growattServer\__init__.py", line 131, in login
    response = self.session.post(self.get_url('newTwoLoginAPI.do'), data={
  File "C:\Users\typ_xxi\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 635, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
  File "C:\Users\typ_xxi\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\typ_xxi\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\sessions.py", line 708, in send
    r = dispatch_hook("response", hooks, r, **kwargs)
  File "C:\Users\typ_xxi\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\hooks.py", line 30, in dispatch_hook
    _hook_data = hook(hook_data, **kwargs)
  File "C:\Users\typ_xxi\AppData\Local\Programs\Python\Python310\lib\site-packages\growattServer\__init__.py", line 41, in <lambda>
    'response': lambda response, *args, **kwargs: response.raise_for_status()
  File "C:\Users\typ_xxi\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://server-api.growatt.com/newTwoLoginAPI.do
api = growattServer.GrowattApi()

6

From there I started a search for the final 403 error message and found your last message and line. I read a bit and then added the true paramteter here: api = growattServer.GrowattApi(true)

No progress at all, at least for me.

And here I am stucked and can not get any further with my limited skills.

7

even though I started to read again and followed sjords advice and link where muppet3000 hinted to your directory with the user agent options script where I continued with the 3rd options cause I had not succeeded with the first and second one (true).

And here I used this 3rd option successfully at least for now I guess

import growattServer

api = growattServer.GrowattApi(False, "MIC5555")
login_response = api.login(""joan", "joanjett")
print("Override default User-Agent")
print("User-Agent: %s\nLogged in User id: %s" % (api.agent_identifier, login_response['userId']))
print("") 

finally got a nice reply which also might help others with limited skills and experience to get it solved.

Override default User-Agent User-Agent: MIC5555 Logged in User id: 987654321

And from there the story might continue.

Final question: might this work on a raspberry pico W too ? I have purchased a BADGER 2040 W from pimoroni which offers a great way to get an e - ink display running from battery and update every hour or so. Would love to use it there with some basic data displayed cause the badger comes with 5 buttons as navigation opportunity to show weater forecast, sun outlook, solar production of today and some other pages on a 2,9 inch e-ink screen like the hourly power rates we get each noon for the next day to use the different price from the exchange to charge the bbattery from the grid during winter time to use that for the heatpumps over the day.

thanks.