indykoning / PyPi_GrowattServer

MIT License
70 stars 32 forks source link

Add custom user-agent headers #37

Closed muppet3000 closed 1 year ago

muppet3000 commented 1 year ago

Fix for https://github.com/indykoning/PyPi_GrowattServer/issues/36

NOTE - The 'User-Agent' header can actually be set to anything, including an empty string. However, I wanted to make it look as close as possible to an actual Android device so it's not easy to filter out on the growatt server side i.e. reducing the likelihood of getting blocked again in the future.

I'm happy to take an alternative solution, but this is the best that I could come up with.

Once merged & released this will also need to be updated on the HA integration to resolve this problem: https://github.com/home-assistant/core/issues/80950

indykoning commented 1 year ago

I agree, we want the requests to match closely to prevent any problems from happening. So this looks good to me 🚀 I'm guessing we can't pass the headers to the session constructor right? I'll try and get a release sorted this evening 👍

muppet3000 commented 1 year ago

Hero!

You can't pass it straight to the session constructor from what I can tell, even their documentation says to do it this way: https://requests.readthedocs.io/en/latest/user/advanced/ Looking at the source code: https://requests.readthedocs.io/en/latest/_modules/requests/sessions/#Session there is a function call to default_headers which wouldn't allow us to override it, so this is the correct option IMO.

It would be awesome if you could turn around a release this evening with just this fix in it because then I can submit the PR for HA straight afterwards and everything will be in a good place again.

Thanks for the quick response.