alandtse / tesla

Tesla custom integration for Home Assistant. This requires a refresh token be generated by third-party apps to login.
Apache License 2.0
597 stars 99 forks source link

tesla_custom.api throws an UnknownError #72

Open jherby2k opened 3 years ago

jherby2k commented 3 years ago

Version of the custom_component

1.2.0

Configuration

service: tesla_custom.api
data:
  command: CHANGE_CLIMATE_TEMPERATURE_SETTING
  parameters:
    path_vars:
      vehicle_id: '786889216'
    driver_temp: 23.0
    passenger_temp: 23.0
    wake_if_asleep: true

or

service: tesla_custom.api
data:
  command: REMOTE_SEAT_HEATER_REQUEST
  parameters:
    path_vars:
      vehicle_id: '786889216'
    heater: 1
    level: 1
    wake_if_asleep: true

Describe the bug

Can't seem to get any tesla_custom.api service calls to work.

Both the above examples return an UnknownError immediately. I imagine i'm doing something wrong, but not sure what. I grabbed the vehicle_id from teslafi - it should always be the same, right?

Logger: homeassistant.components.websocket_api.http.connection Source: custom_components/tesla_custom/services.py:89 Integration: Home Assistant WebSocket API (documentation, issues) First occurred: 4:13:54 PM (3 occurrences) Last logged: 4:24:49 PM

[281473288586144] Error handling message: Unknown error

Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 26, in _handle_async_response await func(hass, connection, msg) File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 525, in handle_execute_script await script_obj.async_run(msg.get("variables"), context=context) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1219, in async_run await asyncio.shield(run.async_run()) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 353, in async_run await self._async_step(log_exceptions=False) File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 371, in _async_step await getattr(self, handler)() File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 571, in _async_call_service_step await service_task File "/usr/src/homeassistant/homeassistant/core.py", line 1491, in async_call task.result() File "/usr/src/homeassistant/homeassistant/core.py", line 1526, in _execute_service await handler.job.target(service_call) File "/config/custom_components/tesla_custom/services.py", line 43, in async_call_tesla_service await api(service_call) File "/config/custom_components/tesla_custom/services.py", line 89, in api return await controller.api(name=command, path_vars=path_vars, *parameters) File "/usr/local/lib/python3.9/site-packages/teslajsonpy/controller.py", line 166, in wake_up result = await wrapped(args, **kwargs) File "/usr/local/lib/python3.9/site-packages/teslajsonpy/controller.py", line 1180, in api return await self.connection.post( File "/usr/local/lib/python3.9/site-packages/teslajsonpy/connection.py", line 167, in post return await self.open( File "/usr/local/lib/python3.9/site-packages/teslajsonpy/connection.py", line 218, in __open raise TeslaException(resp.status_code) teslajsonpy.exceptions.TeslaException

alandtse commented 3 years ago

You need to enable teslajsonpy and read those logs to see why it's failing. As this is access to the raw API interface, I won't be spending much time debugging issues since this is really developer level access. I have no idea if the vehicle_id is the same in teslafi. The teslajsonpy logs will show you the right value.

alandtse commented 3 years ago

Oh please make sure you understand vehicle_id vs id. https://tesla-api.timdorr.com/api-basics/vehicles#vehicle_id-vs-id

jherby2k commented 3 years ago

Thanks, that was definitely part of my problem.

it seems like i can get the API calls to work IF the car is already awake. If i send any commands to a sleeping car, they return the UnknownError immediately. And the car doesn't wake up. I tried sending a WAKE_UP specifically and it still doesn't wake. Just the error.

Happy to investigate this myself, but what do you mean by "enable teslajsonpy"? What do i need to do to enable this logging? Sorry, still new to HA development and python in general.

giachello commented 3 years ago

in the debug section of homeassistan's configuration.yaml add teslajsonpy: debug

jherby2k commented 3 years ago

Thanks. From the debug output it looks like the vehicle_id / id changed since last night.

I know this is an "unsupported feature" but i think to make it useful we need a "vehicle ID" sensor entity to plug into this service call. Otherwise it can't be used in automations / scripts.

Thanks for all your hard work by the way! This integration is a godsend.

alandtse commented 3 years ago

I'll look at exposing that somewhere, but it shouldn't change so once you find it, it should work indefinitely. This is meant as an advanced feature since HA doesn't allow any real feedback on it other than the debug logs.

jherby2k commented 3 years ago

mine definitely rotated since last night. I made some successful API calls just before midnight after you pointed out that vehicle_id vs id thing, and this morning it was failing because the number had changed.

InTheDaylight14 commented 3 years ago

Could the normal full response be made available as attributes of the location sensor? Or can that already be accessed elsewhere? That would make the vehicle_id available via attribute and we can use anything in the normal response cadence. Especially in templates.

As an example use case, I have a cover template setup for opening and closing the windows. I'm using the latitude and longitude attributes of the location tracker in my api call to close the windows.

cover:
  - platform: template
    covers:
      tesla_window:
        device_class: window
        friendly_name: "Tesla Window"
        # value_template: "{{ ? }}"
        open_cover:
          - service: tesla_custom.api
            data:
              command: WINDOW_CONTROL
              parameters:
                path_vars:
                  vehicle_id: 'my_id'
                command: vent
                lat: "{{state_attr('device_tracker.tesla_location_tracker','latitude') }}"
                long: "{{state_attr('device_tracker.tesla_location_tracker','longitude') }}"
                wake_if_asleep: true
        close_cover:
          - service: tesla_custom.api
            data:
              command: WINDOW_CONTROL
              parameters:
                path_vars:
                  vehicle_id: 'my_id'
                command: close
                lat: "{{state_attr('device_tracker.tesla_location_tracker','latitude') }}"
                long: "{{state_attr('device_tracker.tesla_location_tracker','longitude') }}"
                wake_if_asleep: true

If turned into an attribute, I think I could extract the current state of the windows from these in the normal response. Might be nice to have everything available. You never know what could be useful for edge cases.

"vehicle_state":{
...
         "fd_window":0,
         "fp_window":0,
alandtse commented 3 years ago

Could the normal full response be made available as attributes of the location sensor? Or can that already be accessed elsewhere?

I don't know what you're asking about with "normal full response". Service calls in HA cannot return data.

alandtse commented 3 years ago

Please try the dev branch. The online sensor has attributes for vin, id, and vehicle_id.

jherby2k commented 3 years ago

Awesome! Works great.

service: tesla_custom.api
data:
  command: XXXX
  parameters:
    path_vars:
      vehicle_id: "{{ state_attr('binary_sensor.<car_name>_online_sensor', 'id') }}"
  foo: bar

... is going to be the standard template for just about every call, right? Would be cool if you could just do something like this instead:

service: tesla_custom.api
data:
  command: XXXX
  car: <car_name>
  parameters:
    foo: bar

... but for what it is (a dev mode) this is wonderful. Thanks again!

InTheDaylight14 commented 3 years ago

Could the normal full response be made available as attributes of the location sensor? Or can that already be accessed elsewhere?

I don't know what you're asking about with "normal full response". Service calls in HA cannot return data.

Right, sorry. In the logs I see this:

2021-10-20 19:01:59 DEBUG (MainThread) [teslajsonpy.connection] 200: {"response":[{.........

It seems to happen at a pretty steady rate. The 200: response json goes on for a while and has a lot of values. Can all of these values be made available as attributes?

alandtse commented 3 years ago

... is going to be the standard template for just about every call, right? Would be cool if you could just do something like this instead:

This is intended to be direct access to the underlying api. Adding a translation layer defeats the point of direct access.

It seems to happen at a pretty steady rate. The 200: response json goes on for a while and has a lot of values. Can all of these values be made available as attributes?

Since every attribute get logged in HA at each update, this is not advisable. You're welcome to submit a PR for a sensor that does this if you want but it must be default off and I won't be maintaining it.

cbirkenbeul commented 2 years ago

Hi, i also get the same error. But can can see a very strange behavior. The Integration is installed fine in Home Assistant and I can control the car via the switches. I just cannot call the Tesla API via the service for example to change the amps. My ID in HASS ends with a 10. But if the integration makes a get request I get an id with 11 at the end. Rest is identical.

Therefore I results in an unknown error I guess:

2022-01-14 13:37:42 DEBUG (MainThread) [teslajsonpy.connection] post: https://owner-api.teslamotors.com/api/1/vehicles/554866356xxx42910/command/charge_start {}
2022-01-14 13:37:42 DEBUG (MainThread) [teslajsonpy.connection] 404: {"response":null,"error":"not_found","error_description":""}
2022-01-14 13:37:42 DEBUG (MainThread) [teslajsonpy.controller] Exception:
api(() {'name': 'START_CHARGE', 'path_vars': {'vehicle_id': '554866356xxx42910'}})
2022-01-14 13:37:42 ERROR (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Unexpected error for call_service at pos 1:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 381, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 584, in _async_call_service_step
await service_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1495, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1530, in _execute_service
await handler.job.target(service_call)
File "/config/custom_components/tesla_custom/services.py", line 43, in async_call_tesla_service
await api(service_call)
File "/config/custom_components/tesla_custom/services.py", line 89, in api
return await controller.api(name=command, path_vars=path_vars, **parameters)
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/controller.py", line 170, in wake_up
result = await wrapped(*args, **kwargs)
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/controller.py", line 1726, in api
return await self.__connection.post(
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/connection.py", line 168, in post
return await self.__open(url, method=method, headers=self.head, data=data)
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/connection.py", line 217, in __open
raise TeslaException(resp.status_code)
teslajsonpy.exceptions.TeslaException
2022-01-14 13:37:42 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [139980022390160] Error handling message: Unknown error
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/websocket_api/decorators.py", line 27, in _handle_async_response
await func(hass, connection, msg)
File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 527, in handle_execute_script
await script_obj.async_run(msg.get("variables"), context=context)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1260, in async_run
await asyncio.shield(run.async_run())
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 363, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 381, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 584, in _async_call_service_step
await service_task
File "/usr/src/homeassistant/homeassistant/core.py", line 1495, in async_call
task.result()
File "/usr/src/homeassistant/homeassistant/core.py", line 1530, in _execute_service
await handler.job.target(service_call)
File "/config/custom_components/tesla_custom/services.py", line 43, in async_call_tesla_service
await api(service_call)
File "/config/custom_components/tesla_custom/services.py", line 89, in api
return await controller.api(name=command, path_vars=path_vars, **parameters)
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/controller.py", line 170, in wake_up
result = await wrapped(*args, **kwargs)
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/controller.py", line 1726, in api
return await self.__connection.post(
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/connection.py", line 168, in post
return await self.__open(url, method=method, headers=self.head, data=data)
File "/config/deps/lib/python3.9/site-packages/teslajsonpy/connection.py", line 217, in __open
raise TeslaException(resp.status_code)
teslajsonpy.exceptions.TeslaException

Thats a get command in the logs (charger switch in HASS)

2022-01-14 13:48:51 DEBUG (MainThread) [teslajsonpy.connection] Token expiration in 44 days, 22:58:59
2022-01-14 13:48:51 DEBUG (MainThread) [teslajsonpy.connection] post: https://owner-api.teslamotors.com/api/1/vehicles/554866356xxx42911/command/charge_stop {}
2022-01-14 13:48:51 DEBUG (MainThread) [teslajsonpy.connection] 200: {"response":{"reason":"","result":true}}

Does his matter in which location the car is? I guess is should not important if its a US car or EU. Firmware is 2021.44.30.2