jrester / tesla_powerwall

Python API for Tesla Powerwall
MIT License
73 stars 25 forks source link

Feature: Take powerwall on/off grid #42

Closed daniel-simpson closed 1 year ago

daniel-simpson commented 1 year ago

This PR adds a function to set the islanding mode (this is "Take off-grid" and its inverse in the app).

Note: Been a few years since I've written any python, so opening this up to some feedback nice and early. I'm expecting there might be some bits that need finessing, but hopefully this is seen as a worthwhile addition.

jrester commented 1 year ago

@daniel-simpson Thank you very much for your contribution, it is greatly appreciated :) I have only two remaining requests:

daniel-simpson commented 1 year ago

Hi!

I've updated the readme, and had a crack at running the integration and unit tests.

Unit tests worked perfectly, but there was an issue (a bug in my code) with the integration tests. Unfortunately after running the integration tests, my internal powerwall IP isn't responding to any requests, so further integration test runs are failing with a create connection error.

I'll keep debugging, but probably come back to this tomorrow.

For now it's close, but definitely not merge ready :)

daniel-simpson commented 1 year ago

Oh, and I'm running python and tox on windows, so have had to do a few workarounds to get it working, but I'll make sure not to commit these 😅

daniel-simpson commented 1 year ago

Not having much luck with tox (definitely my fault though!)

The errors I'm getting while running my integration test are as follows:

λ python -m tox -e integration
.pkg: _optional_hooks> python C:\Users\dan.simpson\AppData\Roaming\Python\Python310\site-packages\pyproject_api\_backend.py True setuptools.build_meta
.pkg: get_requires_for_build_sdist> python C:\Users\dan.simpson\AppData\Roaming\Python\Python310\site-packages\pyproject_api\_backend.py True setuptools.build_meta
.pkg: prepare_metadata_for_build_wheel> python C:\Users\dan.simpson\AppData\Roaming\Python\Python310\site-packages\pyproject_api\_backend.py True setuptools.build_meta
.pkg: build_sdist> python C:\Users\dan.simpson\AppData\Roaming\Python\Python310\site-packages\pyproject_api\_backend.py True setuptools.build_meta
integration: install_package> python -I -m pip install --force-reinstall --no-deps C:\Projects\tesla_powerwall\.tox\.tmp\package\22\tesla_powerwall-0.3.18.tar.gz
integration: commands[0]> python -m unittest discover tests/integration
E
======================================================================
ERROR: test_islanding (test_powerwall.TestPowerwall)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Projects\tesla_powerwall\.tox\integration\lib\site-packages\requests\models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Python310\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Python310\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\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)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Projects\tesla_powerwall\tesla_powerwall\api.py", line 91, in _process_response
    response_json = response.json()
  File "C:\Projects\tesla_powerwall\.tox\integration\lib\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Projects\tesla_powerwall\tests\integration\test_powerwall.py", line 106, in test_islanding
    self.powerwall.set_island_mode(IslandMode.OFFGRID)
  File "C:\Projects\tesla_powerwall\tesla_powerwall\powerwall.py", line 166, in set_island_mode
    return IslandMode(assert_attribute(self._api.post_islanding_mode({"island_mode": mode.value}), "island_mode"))
  File "C:\Projects\tesla_powerwall\tesla_powerwall\api.py", line 250, in post_islanding_mode
    return self.post("v2/islanding/mode", body)
  File "C:\Projects\tesla_powerwall\tesla_powerwall\api.py", line 144, in post
    return self._process_response(response)
  File "C:\Projects\tesla_powerwall\tesla_powerwall\api.py", line 93, in _process_response
    raise APIError(
tesla_powerwall.error.APIError: Powerwall api error: Error while decoding json of response:

----------------------------------------------------------------------
Ran 1 test in 0.929s

FAILED (errors=1)
integration: exit 1 (1.23 seconds) C:\Projects\tesla_powerwall> python -m unittest discover tests/integration pid=37588
.pkg: _exit> python C:\Users\dan.simpson\AppData\Roaming\Python\Python310\site-packages\pyproject_api\_backend.py True setuptools.build_meta
  integration: FAIL code 1 (9.16=setup[7.92]+cmd[1.23] seconds)
  evaluation failed :( (9.27 seconds)

From what I can tell, the endpoint is returning an empty response (not what I'm getting while using postman against my local env!) :\ I've tried adding extra headers but nothing I've done so far has changed those messages

Any help would be greatly appreciated, otherwise I'll have another crack at this tomorrow. Thanks!

daniel-simpson commented 1 year ago

Ok, managed to get the integration tests running and fixed up my code bugs (looks like the endpoint needed the payload serialised, and marked as application/json 😖

Anyway this is running now, and happy for any more feedback!

jrester commented 1 year ago

Perfect, then this can PR can be merged and I will release a new version of the lib. Thank you for your contribution :)

daniel-simpson commented 1 year ago

Quick question... I can see the github release for this here: https://pypi.org/project/tesla-powerwall/

But that version (0.3.19) isn't available via pip yet? Looks like the latest is still 0.3.18: https://pypi.org/project/tesla-powerwall/

Anything I can do to get it released? :) (working on a Home Assistant PR)

jrester commented 1 year ago

@daniel-simpson ah sorry, forgot to push it. 0.3.19 should now be available :)