Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asyncify the API #55

Merged
merged 11 commits into from
Jan 10, 2024
Merged

Asyncify the API #55

merged 11 commits into from
Jan 10, 2024

Conversation

bubonicbob
Copy link
Contributor

Change the API to use asyncio to be compatible with Home Assistant. This was discussed here.

@bubonicbob
Copy link
Contributor Author

Please be kind, this is my first github contribution! :) Let me know how I can make this change better.

I don't have a powerwall myself so I haven't run the integration tests. Is there anybody who would be willing to help me out and try it?

…y, I shouldn't have removed the logic to automatically create an http client session and the close method. Adds this functionality back.
@ejbatts
Copy link

ejbatts commented Jan 3, 2024

Please be kind, this is my first github contribution! :) Let me know how I can make this change better.

I don't have a powerwall myself so I haven't run the integration tests. Is there anybody who would be willing to help me out and try it?

I can offer to assist with testing, but have no idea how. If you explain any procedure I will try.

@bubonicbob
Copy link
Contributor Author

bubonicbob commented Jan 3, 2024

Please be kind, this is my first github contribution! :) Let me know how I can make this change better.
I don't have a powerwall myself so I haven't run the integration tests. Is there anybody who would be willing to help me out and try it?

I can offer to assist with testing, but have no idea how. If you explain any procedure I will try.

Thanks! I'm on a mac, and I would run:

git clone https://github.com/jrester/tesla_powerwall.git
cd tesla_powerwall
gh pr checkout 55
POWERWALL_IP=youraddress POWERWALL_PASSWORD=yourpass tox -e integration

If you're missing any commands like gh and tox, run:

brew install gh tox

@bubonicbob
Copy link
Contributor Author

Hey @dh99999 would you be able to help test out this pull request for me? Also could I get you to help me get refreshed fixture data. Seems like there has been some big changes in the data structure as compared to version 0.3.19... maybe this corresponds to updated firmware. I wonder why it isn't a breaking change since it deviates a lot from the fixture data in the powerwall HA integration.

@ejbatts
Copy link

ejbatts commented Jan 4, 2024


If you're missing any commands like gh and tox, run:

brew install gh tox

@ejbatts
Copy link

ejbatts commented Jan 4, 2024

I am on a PC, how do I install gh and tox? Brew is not recognised

@bubonicbob
Copy link
Contributor Author

I am on a PC, how do I install gh and tox? Brew is not recognised

You'll have to find a guide to get python and pip installed. Then install tox.

I've never tried getting gh for windows, but perhaps it is available here? Else it might be easier to get this pull request via the github desktop app.

@ejbatts
Copy link

ejbatts commented Jan 4, 2024

got to the last command, tox has installed correctly but cant be found???

admin@STUDY MINGW64 ~/tesla_powerwall (asyncify)
$ powerwall_ip=192.168.1.41 powerwall_password=xxxxxx tox -e integration
bash: tox: command not found

@bubonicbob
Copy link
Contributor Author

got to the last command, tox has installed correctly but cant be found???

admin@STUDY MINGW64 ~/tesla_powerwall (asyncify) $ powerwall_ip=192.168.1.41 powerwall_password=xxxxxx tox -e integration bash: tox: command not found

Hmm, you can try this command and maybe the first line shows you the path to the tox binary?

pip show -f tox

Copy link
Owner

@jrester jrester left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bubonicbob Thank you very much for your PR! I think it already looks quite good, just some minor points that I would like to address before merging it. Additionally, I will also do some testing of your implementation soon, to see if everything works as expected.

Nevertheless, I am quite unsure, about the use of aresponses for testing, as the project seems to be inactive for over two years now and aiohttp looks like it already has good support for testing. I would prefer the direct use of aiohttp, but I would see this more as a future improvement, than a requirement for this PR, as it might otherwise blow the scope. If you want to take a look, I would be happy to also merge this, otherwise I will take care of this in the future.

tesla_powerwall/powerwall.py Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
tests/unit/test_powerwall.py Outdated Show resolved Hide resolved
tests/unit/test_powerwall.py Outdated Show resolved Hide resolved
@ejbatts
Copy link

ejbatts commented Jan 4, 2024

Hmm, you can try this command and maybe the first line shows you the path to the tox binary?

pip show -f tox

Okay I can see the path but how do i incorporate that into the cmd

powerwall_ip=192.168.1.41 powerwall_password=xxxxxx tox -e integration

@quozl
Copy link

quozl commented Jan 4, 2024

Had a quick look, and saw nothing obviously wrong. Here's what I thought of;

  • if requests is no longer needed, remove from requirements?
  • will merging this break all code that uses this module? i.e. force asyncify of others.

@bubonicbob
Copy link
Contributor Author

Hmm, you can try this command and maybe the first line shows you the path to the tox binary?

pip show -f tox

Okay I can see the path but how do i incorporate that into the cmd

powerwall_ip=192.168.1.41 powerwall_password=xxxxxx tox -e integration

POWERWALL_IP and POWERWALL_PASSWORD are just two individual environment variables (btw I bet these are case-sensitive so use all caps). I gave you a fancy way that bash can supply the variables temporarily at the same time, but you can follow guides for bash, powershell, or command prompt to just set the variables by themselves so that they take effect when you run tox alone later in your shell.

It is a good habit to not set passwords into your environment so don't go out of your way to make these global settings, or if you really want to make sure, set your POWERWALL_PASSWORD to something bogus when you're done.

After setting those two variables for your specific shell, you can just run tox by itself:

tox -e integration

@bubonicbob
Copy link
Contributor Author

Had a quick look, and saw nothing obviously wrong. Here's what I thought of;

  • if requests is no longer needed, remove from requirements?
  • will merging this break all code that uses this module? i.e. force asyncify of others.

Thank you! Yes merging this will break anything that depends on this library. In my limited experience, it seems typical in python-land that libraries in this situation just hard fork into something with an 'a' in front of the original name and don't bother with maintaining a hybrid api. In the case of this specific library, there's support for taking the plunge and moving the API over.

@jrester since this is a major API change, should the version change to 0.5.0? Not sure the official way to do that.

@bubonicbob
Copy link
Contributor Author

bubonicbob commented Jan 4, 2024

@bubonicbob Thank you very much for your PR! I think it already looks quite good, just some minor points that I would like to address before merging it. Additionally, I will also do some testing of your implementation soon, to see if everything works as expected.

Nevertheless, I am quite unsure, about the use of aresponses for testing, as the project seems to be inactive for over two years now and aiohttp looks like it already has good support for testing. I would prefer the direct use of aiohttp, but I would see this more as a future improvement, than a requirement for this PR, as it might otherwise blow the scope. If you want to take a look, I would be happy to also merge this, otherwise I will take care of this in the future.

Much appreciated! I'm pretty new to python so I don't know what are the popular choices for testing. I picked aresponses since the tesla-wall-connector similar library used it. I was confused even more to learn that HA wants to stop using unittest.TestCase style tests. It is a jungle out there. 🤷

While you're testing, I'd really appreciate help updating the fixture data for the HA integration tests. I've made a draft pull request to use this library and the fixture data breaks tests over there. I saw these samples from a similar project but still the powerwall firmware is a few years old.

@bubonicbob bubonicbob requested a review from jrester January 4, 2024 18:13
@ejbatts
Copy link

ejbatts commented Jan 4, 2024

I got tox to run by exporting the PATH using:
export PATH=$PATH:/c/users/admin/AppData/Roaming/Python/Python312/scripts I note that tox.exe was located in 'scripts' and not 'site-packages' as suggested by previous analysis.
Having now successfully run the evaluation I have received the following summary:
integration: FAIL code 1 (11.91 seconds)
evaluation failed :( (12.05 seconds)

It appears that the error lies here:
integration: install_deps> python -I -m pip install aresponses
error: subprocess-exited-with-error

And here:
_Python recognizes 'multidict.multilib' as an importable package[^1],
but it is absent from setuptools' packages configuration.

_This leads to an ambiguous overall configuration. If you want to distribute this
package, please make sure that 'multidict.multilib' is explicitly added
to the packages configuration field.

....so I am looking for more help please.

@bubonicbob
Copy link
Contributor Author

multidict

Weird. The multidict package should have been installed automatically because there is a reference to aiohttp as a dependency. I wonder if pip is having some issue fetching the dependencies? Try running (from the root tesla_powerwall directory):

tox exec -- pip show aresponses

You can manually run the pip install for it like this:

tox exec -- pip install aresponses
tox exec -- pip install multidict

@ejbatts
Copy link

ejbatts commented Jan 4, 2024

Tried both of the above but failed. Verbose output:
error: subprocess-exited-with-error

[removed verbose output SEE BELOW]

@ejbatts
Copy link

ejbatts commented Jan 4, 2024

I am installing Microsoft Visual C++ 14.3 now to see if that is the culprit.

EDIT: now the test runs okay but failed due to login error, need to resolve that now.
EDIT: Login error resolved

@ejbatts
Copy link

ejbatts commented Jan 4, 2024

Test still fails :(

@bubonicbob bubonicbob closed this Jan 4, 2024
@bubonicbob bubonicbob reopened this Jan 4, 2024
@ejbatts
Copy link

ejbatts commented Jan 4, 2024

Attached output from last test:

Verbose Output.txt

@bubonicbob
Copy link
Contributor Author

Just ran gh pr checkout 55 and the individual test...same result...

File "C:\Users\admin\tesla_powerwall\tests\integration\test_powerwall.py", line 20, in asyncSetUp
assert self.powerwall.is_authenticated()
AssertionError

My fault, please try one more time.

@ejbatts
Copy link

ejbatts commented Jan 6, 2024

My fault, please try one more time.

SUCCESS!

Ran 10 tests in 27.502s

OK
.pkg: _exit> python C:\Users\admin\AppData\Roaming\Python\Python312\site-packages\pyproject_api_backend.py True setuptools.build_meta
integration: OK (36.28=setup[8.34]+cmd[27.94] seconds)
congratulations :) (36.45 seconds)

@bubonicbob
Copy link
Contributor Author

@ejbatts

@ejbatts Sweet. I owe you a beer next time you're in Seattle. 🍻

@bubonicbob
Copy link
Contributor Author

@jrester Sorry for the spammy comments on this. It is ready to land: both integration and unit tests pass. Don't think I have permissions to actually merge it to master.

@ejbatts
Copy link

ejbatts commented Jan 6, 2024

Will the changed integration allow me to access individual battery data? The essential thing I want is BATTERY POWER (charge/discharge rate) as individual entities and not the combined value that Tesla provides as default.

@bubonicbob
Copy link
Contributor Author

Will the changed integration allow me to access individual battery data? The essential thing I want is BATTERY POWER (charge/discharge rate) as individual entities and not the combined value that Tesla provides as default.

Create an issue on this repo for that and we can all discuss there. Instructions.

@ejbatts
Copy link

ejbatts commented Jan 6, 2024

Oh okay. I actually thought this testing was heading in that direction after I posted this on https://github.com/home-assistant/core/issues/100065#top thread.

I would like a similar feature but the opposite way round. Is it possible to extract data from separate powerwalls attached to a single gateway? I have 2 powerwalls connected to separate phases of a 3 phase installation. Specifically, I would like to know battery load (charge/discharge rate) of each battery so I can monitor house loads on each phase.
This separate data is available on //[gateway ip address]/system page, as well as voltage and state of charge, but I don't know how to extract it in HA.

I'll raise a blank issue.

EDIT: DONE!

tesla_powerwall/api.py Outdated Show resolved Hide resolved
tesla_powerwall/powerwall.py Outdated Show resolved Hide resolved
tesla_powerwall/powerwall.py Outdated Show resolved Hide resolved
tesla_powerwall/api.py Outdated Show resolved Hide resolved
@bdraco
Copy link
Contributor

bdraco commented Jan 7, 2024

Everything seems to be working great after I small fixes I added to home-assistant/core#107164

@bdraco
Copy link
Contributor

bdraco commented Jan 7, 2024

Pretty significant drop in cpu usage for the HA integration observed as well

@bdraco
Copy link
Contributor

bdraco commented Jan 7, 2024

There's still a lot of JSON being passed around, which is what's mostly left of the CPU runtime, using orjson to parse would probably solve that, but that's for another PR.

@bdraco
Copy link
Contributor

bdraco commented Jan 7, 2024

@bubonicbob
Copy link
Contributor Author

There's still a lot of JSON being passed around, which is what's mostly left of the CPU runtime, using orjson to parse would probably solve that, but that's for another PR.

Added an issue for it.

@bdraco
Copy link
Contributor

bdraco commented Jan 8, 2024

I think this looks ready for a release once the typing comments and the aiohttp session comments are sorted

The HA PR looks like its good to go after this is released and the tests are updated

@bubonicbob
Copy link
Contributor Author

I think this looks ready for a release once the typing comments and the aiohttp session comments are sorted

The HA PR looks like its good to go after this is released and the tests are updated

I'm ready to get this released. I don't think I'm able to do this myself.

tox.ini Outdated Show resolved Hide resolved
@bubonicbob bubonicbob requested a review from jrester January 8, 2024 18:23
@bdraco
Copy link
Contributor

bdraco commented Jan 10, 2024

Retested with latest code. Everything seems to be working as expected

@jrester jrester merged commit aa75e6f into jrester:master Jan 10, 2024
@jrester
Copy link
Owner

jrester commented Jan 10, 2024

Thank you very much @bubonicbob for implementing this. Also thank you very much @bdraco, @ejbatts and @quozl for testing and reviewing!

@jrester
Copy link
Owner

jrester commented Jan 10, 2024

Version 0.5.0 is now live on PyPi

@quozl
Copy link

quozl commented Jan 10, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants