zabuldon / teslajsonpy

Apache License 2.0
57 stars 62 forks source link

OPERATION no longer listed under BATTERY_DATA #410

Closed jaymunro closed 1 year ago

jaymunro commented 1 year ago

In HA the operation mode is no longer populated on startup and is left empty. See comment in #618 of https://github.com/alandtse/tesla/issues/618#issuecomment-1590420509

It seems this can be resolved by retrieving the data from the SITE_CONFIG endpoint under the default_real_mode property.

{
    "response":{
        "id":"<redacted>",
        "site_name":"My Home",
        "backup_reserve_percent":15,
        "default_real_mode":"autonomous",
        "installation_date":"2022-07-01T15:15:19+12:00",
        "user_settings":{
            "storm_mode_enabled":true,
            "powerwall_onboarding_settings_set":true,
            "powerwall_tesla_electric_interested_in":false,
            "sync_grid_alert_enabled":true,
            "breaker_alert_enabled":false},
        "components":{
            "solar":true,
            "solar_type":"pv_panel",
...       
        }}}

Details of my API responses are at https://github.com/zabuldon/teslajsonpy/issues/342#issuecomment-1590487294

energy.py, line 175-178:

    @property
    def operation_mode(self) -> str:
        """Return operation mode."""
        return self._battery_data.get("operation")

Change to:

    @property
    def operation_mode(self) -> str:
        """Return operation mode."""
        return self._site_config.get("default_real_mode")
alandtse commented 1 year ago

PRs are welcome.

jaymunro commented 1 year ago

Thanks Alan. I’m new to this and I’d often wondered how to create a PR.

I’ve forked and edited, but have no idea how to test this with tesla_custom using my new fork or by just manually editing the code in HA (teslajsonpy code does not seem to exist in the custom_components folder). Any pointers would be most welcome.

alandtse commented 1 year ago

If you install teslajsonpy locally into custom_components it will take precedence over the system copy. You can also modify it in the system location if you can figure out where your system is hosting the library.

jaymunro commented 1 year ago

If you install teslajsonpy locally into custom_components it will take precedence over the system copy. You can also modify it in the system location if you can figure out where your system is hosting the library.

Thanks for the pointer. I've downloaded the teslajsonpy source into the custom_components directory and updated code there, but it does not seem to be using the code at that location. I'm probably missing something basic.

This is the location I added the source code for confirmation:

Screenshot 2023-06-15 at 11 58 20 PM
purcell-lab commented 1 year ago

This PR has become more urgent as the BATTERY endpoints have stopped functioning but the SITE_CONFIG endpoint is still OK.

$ python3 ./cli.py -e mark@purcell.id.au -z BATTERY_DATA
Traceback (most recent call last):
  File "/home/mark/./cli.py", line 221, in <module>
    main()
  File "/home/mark/./cli.py", line 122, in main
    print(product.api(command, **command_data))
  File "/usr/local/lib/python3.9/dist-packages/teslapy/__init__.py", line 726, in api
    return self.tesla.api(name, pathvars, **kwargs)
  File "/usr/local/lib/python3.9/dist-packages/teslapy/__init__.py", line 363, in api
    return self.request(endpoint['TYPE'], uri, serialize,
  File "/usr/local/lib/python3.9/dist-packages/teslapy/__init__.py", line 155, in request
    response.raise_for_status()  # Raise HTTPError, if one occurred
  File "/usr/lib/python3/dist-packages/requests/models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 410 Client Error: https://powergate.prd.sn.tesla.services:443/api/powerwalls/1152100-14-J--CN321293G4J50D/fullstatus => Gone for url: https://owner-api.teslamotors.com/api/1/powerwalls/STE20220321-00093
jaymunro commented 1 year ago

If someone can help me get up to speed with getting tesla_custom to use my local version of teslajsonpy then I may be able to test and advance this PR.

gkwok1 commented 1 year ago

If someone can help me get up to speed with getting tesla_custom to use my local version of teslajsonpy then I may be able to test and advance this PR.

You can use the local version by putting teslajsonpy folder under /config instead of /config/custom_components.

Jonesnetwork commented 1 year ago

Any chance you could share the Teslajson file? Happy to have a play to try and get it working?

jaymunro commented 1 year ago

If someone can help me get up to speed with getting tesla_custom to use my local version of teslajsonpy then I may be able to test and advance this PR.

You can use the local version by putting teslajsonpy folder under /config instead of /config/custom_components.

Will try this. Thanks.

jaymunro commented 1 year ago

My branch is here for those that would like to help https://github.com/jaymunro/teslajsonpy

gkwok1 commented 1 year ago

My branch is here for those that would like to help https://github.com/jaymunro/teslajsonpy

The latest is not working for me but I found this which has the latest API and response. It's a little too much for me to handle.

https://tesla-api.timdorr.com/energy-products/energy

jaymunro commented 1 year ago

My changes in that repo were back in June and in response to a different api change, much more minor just involving one number variable self._battery_data.get("operation") which was replaced by self._site_config.get("default_real_mode").

@gkwok1 if you give me access to your repo, I’ll add this change (and the associated logging code change as _battery_data log is no longer working).

JasonRS1969 commented 1 year ago

@gkwok1 appears to have fixed the issue.

https://github.com/alandtse/tesla/issues/724#issuecomment-1751808022

gkwok1 commented 1 year ago

My changes in that repo were back in June and in response to a different api change, much more minor just involving one number variable self._battery_data.get("operation") which was replaced by self._site_config.get("default_real_mode").

@gkwok1 if you give me access to your repo, I’ll add this change (and the associated logging code change as _battery_data log is no longer working).

I have already included your change in my PR.

https://github.com/zabuldon/teslajsonpy/pull/428

jaymunro commented 1 year ago

Yes, reviewing it now…

purcell-lab commented 1 year ago

My changes in that repo were back in June and in response to a different api change, much more minor just involving one number variable self._battery_data.get("operation") which was replaced by self._site_config.get("default_real_mode").

Thanks @jaymunro , this Issue and your PR were the early indicators that the upstream API was changing, unfortunately the implications were not fully understood and once the upstream API was finally changed we had this mad scramble for 24 hours to get back to a working condition. Thanks @gkwok1 for turning something around very quickly which is now working and can be merged back into the HA eco system.

The joy of working with an undocumented API :-)

jaymunro commented 1 year ago

Yes, a pity I didn’t have the knowledge to put the teslajsonpy repo in the right place for testing. Still, scrambles like this are good for keeping the community tight. 😜

jaymunro commented 1 year ago

Closing as resolved by https://github.com/alandtse/tesla/issues/724#