Hyundai-Kia-Connect / kia_uvo

A Home Assistant HACS integration that supports Kia Connect(Uvo) and Hyundai Bluelink. The integration supports the EU, Canada and the USA.
MIT License
398 stars 84 forks source link

2024 Kia Sportage - Incorrect request payload format #775

Closed chasebolt closed 7 months ago

chasebolt commented 7 months ago

Region and Brand of car US 2024 Kia Sportage X-Pro Prestige

Version of the integration v2.16.3

Describe the bug Trying to start the car with heating: 4, it fails with the error Incorrect request payload format. Screenshot below shows I do have all of these features available in the app.

Debug logs if an error occurred

2023-12-17 21:19:11.227 DEBUG (SyncWorker_4) [hyundai_kia_connect_api.KiaUvoAPIUSA] hyundai_kia_connect_api - Planned start_climate payload: {'remoteClimate': {'airCtrl': True, 'airTemp': {'unit': 1, 'value': '80'}, 'defrost': True, 'heatingAccessory': {'rearWindow': 4, 'sideMirror': 4, 'steeringWheel': 4}, 'ignitionOnDuration': {'unit': 4, 'value': 10}}}
2023-12-17 21:19:11.228 DEBUG (SyncWorker_4) [hyundai_kia_connect_api.KiaUvoAPIUSA] hyundai_kia_connect_api - sending https://api.owners.kia.com/apigw/v1/rems/start request with {'remoteClimate': {'airCtrl': True, 'airTemp': {'unit': 1, 'value': '80'}, 'defrost': True, 'heatingAccessory': {'rearWindow': 4, 'sideMirror': 4, 'steeringWheel': 4}, 'ignitionOnDuration': {'unit': 4, 'value': 10}}}
2023-12-17 21:19:11.671 DEBUG (SyncWorker_4) [hyundai_kia_connect_api.KiaUvoAPIUSA] hyundai_kia_connect_api got response {"status":{"statusCode":1,"errorType":1,"errorCode":9001,"errorMessage":"Incorrect request payload format"}}
2023-12-17 21:19:11.671 ERROR (SyncWorker_4) [hyundai_kia_connect_api.KiaUvoAPIUSA] hyundai_kia_connect_api - Error: unknown error response {"status":{"statusCode":1,"errorType":1,"errorCode":9001,"errorMessage":"Incorrect request payload format"}}
2023-12-17 21:19:11.674 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 468, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
response_data = await self._async_run_long_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
return long_task.result()
^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2067, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2104, in _execute_service
return await target(service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/kia_uvo/services.py", line 70, in async_handle_start_climate
await coordinator.async_start_climate(vehicle_id, climate_request_options)
File "/config/custom_components/kia_uvo/coordinator.py", line 198, in async_start_climate
await self.hass.async_add_executor_job(
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 134, in start_climate
return self.api.start_climate(self.token, self.get_vehicle(vehicle_id), options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoAPIUSA.py", line 652, in start_climate
response = self.post_request_with_logging_and_active_session(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoAPIUSA.py", line 41, in request_with_active_session_wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoAPIUSA.py", line 86, in request_with_logging_wrapper
raise RequestException
requests.exceptions.RequestException
2023-12-17 21:19:11.705 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140429685065152] Error handling message: Unknown error (unknown_error) Chase Bolt from 192.168.1.1 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36)
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 782, in handle_execute_script
script_result = await script_obj.async_run(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1578, in async_run
return await asyncio.shield(run.async_run())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 420, in async_run
await self._async_step(log_exceptions=False)
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 470, in _async_step
self._handle_exception(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 493, in _handle_exception
raise exception
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 468, in _async_step
await getattr(self, handler)()
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 704, in _async_call_service_step
response_data = await self._async_run_long_action(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 666, in _async_run_long_action
return long_task.result()
^^^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2067, in async_call
response_data = await coro
^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/core.py", line 2104, in _execute_service
return await target(service_call)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/config/custom_components/kia_uvo/services.py", line 70, in async_handle_start_climate
await coordinator.async_start_climate(vehicle_id, climate_request_options)
File "/config/custom_components/kia_uvo/coordinator.py", line 198, in async_start_climate
await self.hass.async_add_executor_job(
File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 134, in start_climate
return self.api.start_climate(self.token, self.get_vehicle(vehicle_id), options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoAPIUSA.py", line 652, in start_climate
response = self.post_request_with_logging_and_active_session(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoAPIUSA.py", line 41, in request_with_active_session_wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoAPIUSA.py", line 86, in request_with_logging_wrapper
raise RequestException
requests.exceptions.RequestException

To Reproduce

service: kia_uvo.start_climate
data:
  duration: 10
  climate: true
  temperature: 80
  defrost: true
  heating: 4
  device_id: 5115d312c9cce9f562401c1bb71ffc85
  flseat: 8
  frseat: 8

Expected behavior Start the car with heated steering wheel, heated mirrors, and rear defrost.

Screenshots IMG_B65D260C000D-1

Additional Info I tested different values for heating and these are available to me: 0, 1

cdnninja commented 7 months ago

Does that option exist in the native app?

chasebolt commented 7 months ago

Does that option exist in the native app?

Attached to the original post is a screenshot of what I have available in the app. My interpretation from what I see in the app - yes. But they are individual toggles.

cdnninja commented 7 months ago

To confirm seats work fine? If you send a 1 does it turn the items on you want?

We just changed this per: https://github.com/Hyundai-Kia-Connect/kia_uvo/pull/747

Maybe Canada and USA are different which makes this a pain to map.

chasebolt commented 7 months ago

sending 1 turns on Steering Wheel, Side and Back Defroster.

frseat and flseat setting does not respond on my vehicle. i sent it values 0-12 and the ventilated/heated seats never activated.

might need a new property for region to know how to handle these mappings.

chasebolt commented 7 months ago

is there debug documentation i can follow to help provide more info?

cdnninja commented 7 months ago

Looks like USA doesn't support this in our code for seats. https://github.com/Hyundai-Kia-Connect/hyundai_kia_connect_api/blob/00724b5ab2c4acf315b7752fe18ba32ff06fc2f4/hyundai_kia_connect_api/KiaUvoAPIUSA.py#L633-L650

Someone needs to sniff the traffic and adjust that section to include the seat commands. Unfortunately no one in the USA maintains this code today.

chasebolt commented 7 months ago

I tried sniffing the iOS traffic for the Kia Access app, which is what I use, but unfortunately I believe they are using SSL pinning and I am unable to make it work.

I will bust out an android device and see if I can make that work.

EDIT: ended up using the kia connect website, it sends the same API payload but to a different endpoint.

cdnninja commented 7 months ago

Awesome thanks for giving it a go. That's exactly the work that's needed to improve the USA Kia integration.

chasebolt commented 7 months ago

saving here the api data for any future searches

{
  "action": "ACTION_EXEC_REMOTE_CLIMATE_ON",
  "remoteClimate": {
    "airTemp": {
      "value": "80",
      "unit": 1
    },
    "airCtrl": true,
    "defrost": true,
    "ventilationWarning": false,
    "ignitionOnDuration": {
      "value": 10,
      "unit": 4
    },
    "heatingAccessory": {
      "steeringWheel": 1,
      "steeringWheelStep": null,
      "sideMirror": 1,
      "rearWindow": 1
    },
    "heatVentSeat": {
      "driverSeat": {
        "heatVentType": 1,
        "heatVentLevel": 4,
        "heatVentStep": 1
      },
      "passengerSeat": {
        "heatVentType": 2,
        "heatVentLevel": 4,
        "heatVentStep": 1
      },
      "rearLeftSeat": {
        "heatVentType": 0,
        "heatVentLevel": 1,
        "heatVentStep": 0
      },
      "rearRightSeat": {
        "heatVentType": 0,
        "heatVentLevel": 1,
        "heatVentStep": 0
      }
    }
  }
}
cdnninja commented 7 months ago

Is this something you could play with further to understand how options map for the seats for heat vs cool and the levels?

Would be very open for you to drop a pull request for the above code link to get this working.

chasebolt commented 7 months ago

how do we get a new release with the recent merge in the API?

cdnninja commented 7 months ago

Title wasn't correct so ci didn't release. From my phone card to tell if CI is happy or not. I often just fudge something to force it. I'll do that now.

If interested the USA api could add a lot of data and services to catch up to what this integration is capable of.

cdnninja commented 7 months ago

Done. Release is out of API.

chasebolt commented 7 months ago

thanks. how should i be titling PRs to make sure they get released?

cdnninja commented 7 months ago

The CI follows semantic release titles. It is how it picks the release number and generates release notes. I'm not sure why that last one didn't.

If you google semantic release it shows the rules of it. I'm no pro with this. When by a computer I'll tinker to try make sure the checks tell us in advance before I merge it.

chasebolt commented 7 months ago

@cdnninja when you get a chance can you cut a new release of the API?

also, forgive me I don't develop in python, how do I manually pass options to start_climate()? trying to test locally as described in the readme.

cdnninja commented 7 months ago

I thought it released yesterday? If you look at the home page of the api repo it shows the current bug fix release number. Put that into manifest to test.

As for running this with python I'll get you example shortly when I'm at a computer.

cdnninja commented 7 months ago

Example code that I use to test. This is so I can reference a local copy of the code:

from pathlib import Path
import logging
import sys
import os

path_root = r"path to local github directory with edit"
sys.path.append(str(path_root))
from hyundai_kia_connect_api import *
from hyundai_kia_connect_api.exceptions import *

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)s:%(message)s')
logger = logging.getLogger(__name__)

vm = VehicleManager(region=1, brand=3, username="test@gmail.com", password="test", pin="", geocode_api_enable = True)

print(vm.check_and_refresh_token())

vm.update_all_vehicles_with_cached_state()
print(vm.vehicles)

Now in here where the print is called you could add other items to run: start climate would be something like. It would also need seats added if you like following the names here: https://github.com/Hyundai-Kia-Connect/hyundai_kia_connect_api/blob/b705f143aa87f82c8e45d383254636281ecb96f0/hyundai_kia_connect_api/ApiImpl.py#L19-L28

climateOptions = ClimateRequestOptions(set_temp = 70, duration = 10, defrost = True, climate = True, heating = 1)
vm.start_climate(vm.vehicles[0].id, climateOptions)
chasebolt commented 7 months ago

great, thanks for all the help! heated/ventilated seats are complete for USA now. going to close this out 😄

not sure if there are any other missing features, ill poke around but if you know of any feel free to let me know and ill see what I can do to add them!

cdnninja commented 7 months ago

Great thanks!! The two things that come to mind are:

  1. Trip data. Our API supports it for EU. I assume it exists for USA.
  2. A lap around the response of raw API data to see if our mapping could add other items. I would expect we don't map all the data we could.