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
435 stars 87 forks source link

[Kia/Hyundai EU] Integration stops working after some time (invalid device id) #672

Closed bootc closed 11 months ago

bootc commented 1 year ago

Region and Brand of car

Europe (UK), Kia (e-Niro)

Version of the integration

2.10.4

Describe the bug I know there have been issues the last few days and a flurry up updates to fix it, but I'm running the latest release and still having issues.

Last night my charging automation didn't work; the start/stop charge service calls resulted in messages such as:

Server returned: 'Invalid request body - Invalid deviceId. '

This morning I enabled debug logging in the integration and restarted Home Assistant, which resulted in it taking a very long time but I'm not sure if that's expected. The startup log complains about:

"Force update failed, falling back to cached: Request timeout. - Request timeout."

However it does manage to list the vehicles and obtain the cached data OK.

Debug logs if an error occurred

When calling the stop_charge service:

2023-07-04 08:53:41.986 DEBUG (SyncWorker_22) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - Stop Charge Action Request {'action': 'stop', 'deviceId': '1591fb40-0738-4daf-8b68-b5a5bbbd9785'}
2023-07-04 08:53:41.986 DEBUG (SyncWorker_22) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - get_stamp 2023-07-04 05:02:13.150000+00:00 120000 85 998 85.74030704166667
2023-07-04 08:53:42.133 DEBUG (SyncWorker_22) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - Stop Charge Action Response: {'retCode': 'F', 'resCode': '4002', 'resMsg': 'Invalid request body - Invalid deviceId. ', 'msgId': '77ce6410-6146-4b27-8bbd-9d9dafbb9952'}
2023-07-04 08:53:42.134 ERROR (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Unexpected error for call_service at pos 1: Server returned: 'Invalid request body - Invalid deviceId. '
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 452, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 685, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1910, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1950, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/config/custom_components/kia_uvo/services.py", line 105, in async_handle_stop_charge
    await coordinator.async_stop_charge(vehicle_id)
  File "/config/custom_components/kia_uvo/coordinator.py", line 219, in async_stop_charge
    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 "/config/deps/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 151, in stop_charge
    return self.api.stop_charge(self.token, self.get_vehicle(vehicle_id))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/deps/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 854, in stop_charge
    _check_response_for_errors(response)
  File "/config/deps/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 111, in _check_response_for_errors
    raise APIError(f"Server returned: '{response['resMsg']}'")
hyundai_kia_connect_api.exceptions.APIError: Server returned: 'Invalid request body - Invalid deviceId. '
2023-07-04 08:53:42.139 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140567518132112] Error handling message: Unknown error (unknown_error) Chris Boot from 2001:67c:2e80:60:450d:474a:2a87:cf17 (Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.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 678, in handle_execute_script
    await script_obj.async_run(msg.get("variables"), context=context)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1533, in async_run
    await asyncio.shield(run.async_run())
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 410, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 454, in _async_step
    self._handle_exception(
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 477, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 452, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 685, in _async_call_service_step
    await service_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 1910, in async_call
    task.result()
  File "/usr/src/homeassistant/homeassistant/core.py", line 1950, in _execute_service
    await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)(
  File "/config/custom_components/kia_uvo/services.py", line 105, in async_handle_stop_charge
    await coordinator.async_stop_charge(vehicle_id)
  File "/config/custom_components/kia_uvo/coordinator.py", line 219, in async_stop_charge
    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 "/config/deps/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 151, in stop_charge
    return self.api.stop_charge(self.token, self.get_vehicle(vehicle_id))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/deps/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 854, in stop_charge
    _check_response_for_errors(response)
  File "/config/deps/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 111, in _check_response_for_errors
    raise APIError(f"Server returned: '{response['resMsg']}'")
hyundai_kia_connect_api.exceptions.APIError: Server returned: 'Invalid request body - Invalid deviceId. '

To Reproduce Call the kia_uvo.stop_charge (with a valid device_id from the web ui popup).

Expected behavior Car should stop charging etc...

Screenshots n/a

Additional context

DickiePhitt commented 1 year ago

I also still have non-working integration. Seems previous post has been closed prematurely.

fuatakgun commented 1 year ago

after fixing the integration, you device IDs might have changed, can you try to remove and add back the integration.

If the issue persists, please generate logs while restarting your home assistant so we can see if the device id is matching with vehicles response and your call.

fjavimer commented 1 year ago

It stopped working for me too, since I didn't know the reason I removed the application and tried to put it back on and it won't let me register, when I enter data I get the message "Unexpected error"

bootc commented 1 year ago

after fixing the integration, you device IDs might have changed, can you try to remove and add back the integration.

That seems to have fixed it for me. Interestingly the 32-character hex device_id string generated by the web UI when selecting my vehicle from the popup hasn't changed.

If the issue persists, please generate logs while restarting your home assistant so we can see if the device id is matching with vehicles response and your call.

I suspect the internal vehicleId field changed as you suggest. Perhaps it would be more useful to store the VIN as the permanent identifier and cache the vehicleId at login time?

fjavimer commented 1 year ago

A mi también me dejo de funcionar, ya que no sabia el motivo eliminé la aplicación e intenté volver a ponerla y no me deja registrarme, cuando ingreso datos me sale el mensaje "Error inesperado"

I answer myself: I updated to 2.10.4 and it works now: Hyundai Tucson PHEV, Spain

vahaldor commented 1 year ago

I updated to 2.10.4 on 3rd July evening and everything worked. I also had correct numbers in EV remaining distance and fuel remaining distance sensors, which I never had correct (KIA sorento phev). However, today on 4th I forced update, locked car remotely and a couple of minutes later I got the same error as before:

Logger: homeassistant.components.websocket_api.http.connection Source: custom_components/kia_uvo/coordinator.py:159 Integration: Home Assistant WebSocket API (documentation, issues) First occurred: 11:41:00 (3 occurrences) Last logged: 12:33:06

[140426700196496] Server returned: 'Invalid request body - Invalid deviceId. ' [140427499349968] Server returned: 'Invalid request body - Invalid deviceId. ' [140426702490192] Server returned: 'Invalid request body - Invalid deviceId. ' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 205, in handle_call_service await hass.services.async_call( File "/usr/src/homeassistant/homeassistant/core.py", line 1910, in async_call task.result() File "/usr/src/homeassistant/homeassistant/core.py", line 1950, in _execute_service await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)( File "/config/custom_components/kia_uvo/services.py", line 49, in async_handle_force_update await coordinator.async_force_update_all() File "/config/custom_components/kia_uvo/coordinator.py", line 159, in async_force_update_all 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 109, in force_refresh_all_vehicles_states self.force_refresh_vehicle_state(vehicle_id) File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 114, in force_refresh_vehicle_state self.api.force_refresh_vehicle_state(self.token, vehicle) File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 322, in force_refresh_vehicle_state state = self._get_forced_vehicle_state(token, vehicle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 739, in _get_forced_vehicle_state _check_response_for_errors(response) File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 111, in _check_response_for_errors raise APIError(f"Server returned: '{response['resMsg']}'") hyundai_kia_connect_api.exceptions.APIError: Server returned: 'Invalid request body - Invalid deviceId. '

RuneNyhuus commented 1 year ago

I have also updated to 2.10.4 and then Integration works for some hours, and then i need to Reload integration.. If i keep reloading my integration, it keeps working..

bootc commented 1 year ago

Indeed, same as @RuneNyhuus - this stops working after a while. Reloading the integration brings it back to life for a finite amount of time before it starts failing again with the "Invalid request body - Invalid deviceId." error.

fuatakgun commented 1 year ago

Thanks all, we were able to figure out the problem (not the solution) here.

While connecting to Hyundai/Kia EU servers, we have to supply a unique id to receive push notifications about commands sent to cars. Until last week, we were using a fixed id as we were not actually connecting with the push notifications servers, this week, we started to generate random numbers (following the expected pattern) but still not connected to respective servers.

Apparently, our registration mechanism with random id is invalidated after you receive your first push notification from your car. Next time, when integration tries to do an action, it generates an invalid device id error.

VictorTorrens commented 1 year ago

I got a "Cant call service kia_uvo/update. Server returned: 'Invalid request body - Invalid deviceId. '. As already mentioned, it works the first time but then the error pops up

acs-lux commented 1 year ago

For me it works until i ask the integration to do something. Then it does it but must be reloaded afterwards.

GarryG commented 1 year ago

With the latest update, integration in HA seems to be working fine again (including setting values, like the charging limit). Thanks! (Hyundai, Europe)

blair287 commented 1 year ago

Set this up for my mate calling a service like start climate works once then stops working with device id unknown.

Removing the integration adding again allows one more go then after first service stops working again

mthlvt commented 1 year ago

+1 sharing debug logs on v2.10.4

Unexpected error fetching kia_uvo data: Server returned: 'Invalid request body - Invalid parameter.'
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 283, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/kia_uvo/coordinator.py", line 107, in _async_update_data
    await self.async_check_and_refresh_token()
  File "/config/custom_components/kia_uvo/coordinator.py", line 166, in async_check_and_refresh_token
    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 120, in check_and_refresh_token
    self.initialize()
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 64, in initialize
    self.token: Token = self.api.login(self.username, self.password)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 198, in login
    device_id = self._get_device_id(stamp)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 1104, in _get_device_id
    _check_response_for_errors(response)
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 110, in _check_response_for_errors
    raise APIError(f"Server returned: '{response['resMsg']}'")
hyundai_kia_connect_api.exceptions.APIError: Server returned: 'Invalid request body - Invalid parameter.'
matthewcky2k commented 1 year ago

Same problem stops working after some time error log below.

`. This error originated from a custom integration.

Logger: custom_components.kia_uvo.coordinator Source: custom_components/kia_uvo/coordinator.py:142 Integration: Hyundai / Kia Connect (documentation, issues) First occurred: July 7, 2023 at 10:14:36 PM (2 occurrences) Last logged: July 7, 2023 at 10:44:36 PM

Unexpected error fetching kia_uvo data: Server returned: 'Invalid request body - Invalid deviceId. ' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 283, in _async_refresh self.data = await self._async_update_data() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/kia_uvo/coordinator.py", line 142, in _async_update_data 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 75, in update_all_vehicles_with_cached_state self.update_vehicle_with_cached_state(vehicle_id) File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 80, in update_vehicle_with_cached_state self.api.update_vehicle_with_cached_state(self.token, vehicle) File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 300, in update_vehicle_with_cached_state state = self._get_cached_vehicle_state(token, vehicle) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 714, in _get_cached_vehicle_state _check_response_for_errors(response) File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 111, in _check_response_for_errors raise APIError(f"Server returned: '{response['resMsg']}'") hyundai_kia_connect_api.exceptions.APIError: Server returned: 'Invalid request body - Invalid deviceId. ' `

DJBenson commented 1 year ago

I think I'm having this same issue. It seems that after some update from the service, the integration goes unavailable and needs reloading.

corvus2606 commented 1 year ago

if it helps, as a workaround, i've added an extra step to my automation. after calling the service for my kia, i wait 5 seconds and call:

service: homeassistant.reload_config_entry
data: {}
target:
  device_id: [device id for car]

seems to be keeping things working for now. but far from a solution

cdnninja commented 1 year ago

Try update your kia_uvo integration from "master" and please report back if improved. As a note the fix relogins when this error occurs consuming API calls. Not a perfect fix but helps for now.

DJBenson commented 1 year ago

I removed and re-added the integration (2.10.4) this afternoon and since then the integration has stayed up.

grahamrjuk commented 1 year ago

Try update your kia_uvo integration from "master" and please report back if improved. As a note the fix relogins when this error occurs consuming API calls. Not a perfect fix but helps for now.

Having updated from master a few days ago I eventually tried a force update. All values became unavailable and I will have to download again.

cdnninja commented 1 year ago

Try update your kia_uvo integration from "master" and please report back if improved. As a note the fix relogins when this error occurs consuming API calls. Not a perfect fix but helps for now.

Having updated from master a few days ago I eventually tried a force update. All values became unavailable and I will have to download again.

Could you provide logs? Also re-installing doesn't change anything.

grahamrjuk commented 1 year ago

Try update your kia_uvo integration from "master" and please report back if improved. As a note the fix relogins when this error occurs consuming API calls. Not a perfect fix but helps for now.

Having updated from master a few days ago I eventually tried a force update. All values became unavailable and I will have to download again.

Could you provide logs? Also re-installing doesn't change anything.

Interestingly I tried again (sorry, I don't have the log from the first time), and it has carried on working, with nothing in the log. Sorry for the false alarm - I will keep watching it and do better with the logs if there is another problem.

grahamrjuk commented 1 year ago

Try update your kia_uvo integration from "master" and please report back if improved. As a note the fix relogins when this error occurs consuming API calls. Not a perfect fix but helps for now.

Having updated from master a few days ago I eventually tried a force update. All values became unavailable and I will have to download again.

Could you provide logs? Also re-installing doesn't change anything.

Interestingly I tried again (sorry, I don't have the log from the first time), and it has carried on working, with nothing in the log. Sorry for the false alarm - I will keep watching it and do better with the logs if there is another problem.

I am on 2.10.5 and lost connection again. It hasn't recovered. Log is the same as usual:

This error originated from a custom integration.

Logger: custom_components.kia_uvo.coordinator
Source: custom_components/kia_uvo/coordinator.py:142
Integration: Kia Uvo / Hyundai Bluelink (documentation, issues)
First occurred: 12 July 2023 at 23:31:00 (13 occurrences)
Last logged: 05:31:01

Unexpected error fetching kia_uvo data: Invalid request body - Invalid deviceId.
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 283, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/kia_uvo/coordinator.py", line 142, in _async_update_data
    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 75, in update_all_vehicles_with_cached_state
    self.update_vehicle_with_cached_state(vehicle_id)
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 80, in update_vehicle_with_cached_state
    self.api.update_vehicle_with_cached_state(self.token, vehicle)
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 304, in update_vehicle_with_cached_state
    state = self._get_cached_vehicle_state(token, vehicle)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 718, in _get_cached_vehicle_state
    _check_response_for_errors(response)
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 111, in _check_response_for_errors
    raise error_code_mapping[response["resCode"]](response["resMsg"])
hyundai_kia_connect_api.exceptions.DeviceIDError: Invalid request body - Invalid deviceId. 
oliveropik commented 1 year ago

I have a same problem with 2.10.5 if i try to start or stop charging. After HA restart its works, but only ONE time. EU, Kia EV6.

Rothammel commented 1 year ago

Same here

robaw commented 1 year ago

Same issue here in EU on v2.10.5

RyanBlacklaws-Pi commented 1 year ago

if it helps, as a workaround, i've added an extra step to my automation. after calling the service for my kia, i wait 5 seconds and call:

service: homeassistant.reload_config_entry
data: {}
target:
  device_id: [device id for car]

seems to be keeping things working for now. but far from a solution

This workaround is still working for me.

robaw commented 1 year ago

if it helps, as a workaround, i've added an extra step to my automation. after calling the service for my kia, i wait 5 seconds and call:

service: homeassistant.reload_config_entry
data: {}
target:
  device_id: [device id for car]

seems to be keeping things working for now. but far from a solution

This workaround is still working for me.

Yup this works for me 👍

gergyverjux commented 1 year ago

How can this help for a dashboard button ? Can we add this reload action after a button action?

corvus2606 commented 1 year ago

How can this help for a dashboard button ? Can we add this reload action after a button action?

you could make a script that calls the service, then waits a few seconds, then calls the reload service. set the tap-action on your dashboard button to run the script instead of calling the service directly

gergyverjux commented 1 year ago

Will do that, thanks @corvus2606 !

cdnninja commented 1 year ago

For those with this issue are you a Hyundai or Kia owner?

fuatakgun commented 1 year ago

to add more, please get latest debug logs with error captured on it. we were expecting this to be fixed with latest changes, so we are not exactly sure how to fix it without you share your logs

JDkiwi commented 1 year ago

i'm having the same issue, except that i'm unable to get it working by reloading the integration.

i own a Hyundai Kona EV

here are the logs:

Unexpected error fetching kia_uvo data: 'redirectUrl'
Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 213, in login
    authorization_code = self._get_authorization_code_with_redirect_url(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 1154, in _get_authorization_code_with_redirect_url
    parsed_url = urlparse(response["redirectUrl"])
                          ~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'redirectUrl'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/update_coordinator.py", line 283, in _async_refresh
    self.data = await self._async_update_data()
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/kia_uvo/coordinator.py", line 107, in _async_update_data
    await self.async_check_and_refresh_token()
  File "/config/custom_components/kia_uvo/coordinator.py", line 166, in async_check_and_refresh_token
    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 120, in check_and_refresh_token
    self.initialize()
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 64, in initialize
    self.token: Token = self.api.login(self.username, self.password)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 218, in login
    authorization_code = self._get_authorization_code_with_form(
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 1252, in _get_authorization_code_with_form
    parsed_url = urlparse(response["redirectUrl"])
                          ~~~~~~~~^^^^^^^^^^^^^^^
KeyError: 'redirectUrl'

but i also noticed, that i'm now unable to log into the BlueLink App on my phone 🫣 do you think it has something to do with the integration?

a-schiffer commented 1 year ago

@JDkiwi: This is a different problem, not related to the "invalid device id" of this thread - so if you want this to be followed-up, please open a different thread. I also had the "redirectUrl" problem yesterday for several hours - now it works again; hopefully it was just a hick-up :) My BlueLink App worked fine all the time.

JDkiwi commented 1 year ago

thanks for the heads up. it also works now for me again, so i guess it was just temporary.

Sab44 commented 1 year ago

@fuatakgun @cdnninja Find attached a full log of the issue after fresh HA restart. Vehicle: Hyundai Kona Electric Region EU (Germany)

Initial state: Vehicle is plugged in, not charging.

12:56 - calling start_charge (successfully) 13:00 - calling stop_charge unsuccessfully, receiving hyundai_kia_connect_api.exceptions.DeviceIDError: Invalid request body - Invalid deviceId.

home-assistant_2023-08-14T11-01-08.179Z.log

Sab44 commented 1 year ago

Any update on this? Anything else I can test or supply from the logs? Currently the integration is not usable for me. Any help is appreciated.

Regarding the workaround mentioned above, it is unclear to me how to retrieve the device id to make it part of my automations. A temporary solution with this would be better than nothing at the moment. Nevermind, I understand now that this is the static device id available in the integration if you call e.g. start_charge and not the device id from the logs.

slayeri commented 1 year ago

For my 2023 European Hyundai Kona electric the integration works partly; I can lock or unlock the doors, home assistant can track the car, get the car statuses etc., but I can't for example set charging limits. If I try, it complains of the deviceId.

Integration is v 2.14.0

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 703, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 665, in _async_run_long_action
    return long_task.result()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 1974, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2011, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/config/custom_components/kia_uvo/services.py", line 114, in async_handle_set_charge_limit
    await coordinator.set_charge_limits(vehicle_id, int(ac), int(dc))
  File "/config/custom_components/kia_uvo/coordinator.py", line 226, in set_charge_limits
    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 157, in set_charge_limits
    return self.api.set_charge_limits(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 1088, in set_charge_limits
    _check_response_for_errors(response)
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 113, in _check_response_for_errors
    raise error_code_mapping[response["resCode"]](response["resMsg"])
hyundai_kia_connect_api.exceptions.DeviceIDError: Invalid request body - Invalid deviceId. 

I've added the automation using visual editor, call service, set charging limits, selected my car from the dropdown-list (which probably becomes the deviceId).

Sab44 commented 1 year ago

I have looked through to the logs now to try and figure out what's happening.

The first request (even before login) the integration sends during startup is "get device id" to a /notifications/register endpoint. I assume the deviceId parameter fetched here denotes the notification-receiving device. This parameter is then sent as part of the request for commands like e.g. start/stop charge - presumably those which trigger a notification. This parameter is then invalidated on Hyundai's servers for some reason, making subsequent requests fail as they send the now invalidated deviceId parameter, hence leading to the error response we are observing. Reloading the integration fixes this, as on (re-)start it will fetch a fresh deviceId.

I probably didn't get it entirely right and it'll be obvious to many here, still wanted to write it down to help clarify to other users like myself.

I guess the naive fix would be to fetch a new deviceId once the current one has been "used" but that's probably just a hack. Happy for some feedback and discussion to get this sorted.

cdnninja commented 1 year ago

I have looked through to the logs now to try and figure out what's happening.

The first request (even before login) the integration sends during startup is "get device id" to a /notifications/register endpoint. I assume the deviceId parameter fetched here denotes the notification-receiving device. This parameter is then sent as part of the request for commands like e.g. start/stop charge - presumably those which trigger a notification. This parameter is then invalidated on Hyundai's servers for some reason, making subsequent requests fail as they send the now invalidated deviceId parameter, hence leading to the error response we are observing. Reloading the integration fixes this, as on (re-)start it will fetch a fresh deviceId.

I probably didn't get it entirely right and it'll be obvious to many here, still wanted to write it down to help clarify to other users like myself.

I guess the naive fix would be to fetch a new deviceId once the current one has been "used" but that's probably just a hack. Happy for some feedback and discussion to get this sorted.

I think you are correct. This is new since the app ids changed a few months back.

The servers seem to pick up on the fact the push failed. I'm not in eu so can't test potential fixes. Someone will need to play with different push approaches to try make this not fail.

slayeri commented 1 year ago

I have looked through to the logs now to try and figure out what's happening.

The first request (even before login) the integration sends during startup is "get device id" to a /notifications/register endpoint. I assume the deviceId parameter fetched here denotes the notification-receiving device. This parameter is then sent as part of the request for commands like e.g. start/stop charge - presumably those which trigger a notification. This parameter is then invalidated on Hyundai's servers for some reason, making subsequent requests fail as they send the now invalidated deviceId parameter, hence leading to the error response we are observing. Reloading the integration fixes this, as on (re-)start it will fetch a fresh deviceId.

I probably didn't get it entirely right and it'll be obvious to many here, still wanted to write it down to help clarify to other users like myself.

I guess the naive fix would be to fetch a new deviceId once the current one has been "used" but that's probably just a hack. Happy for some feedback and discussion to get this sorted.

You're probably right, as the call to set charge limits seems to succeed after reloading the integration, at least once :).

Strauman commented 1 year ago

So how would you fix it @Sab44 ? Any clever ideas? Would the naive fix work? Is it too simplistic?

Sab44 commented 1 year ago

@Strauman the naive fix is basically what the current workaround does by reloading the integration. Fetching the deviceId is done as part of the login (actually not in this repo but in the underlying library). So sure, you could change the code to relogin everytime the "invalid device id" is encountered and then repeat the previous command and maybe that's a temporary fix that could be implemented. But to me it would feel like a band-aid solution that lacks proper understanding of what's happening rather than fixing the actual cause. Unfortunately as far as I know there is no documentation regarding the reverse engineered API so I personally don't know where to even begin to try and attempt a proper fix.

hogsterbogster commented 1 year ago

Hi all, I've just managed to install this integration, but have encountered the "Failed to call service kia_uvo/start_charge. Invalid request body - Invalid deviceId." error message after successfully starting the charge once.

Has anyone had an ideas of how to solve this issue?

Many thanks!

David

phantomsphorever commented 1 year ago

The only way I can make it work is to reload the config entry (see last part of this script) after I sent the start charge command.

alias: Ioniq 5 Start Charging sequence:

hogsterbogster commented 1 year ago

The only way I can make it work is to reload the config entry (see last part of this script) after I sent the start charge command.

alias: Ioniq 5 Start Charging sequence:

  • service: kia_uvo.start_charge data: device_id: ff0ff7d8286b819xxxxxxxxxxxxxxx
  • delay: hours: 0 minutes: 0 seconds: 30 milliseconds: 0
  • service: homeassistant.reload_config_entry data: {} target: device_id: ff0ff7d8286b819xxxxxxxxxxxxxxxxxxxx mode: single

Thanks so much for sharing your solution phantomsforever ☺️

Do I understand correctly that the device ID changes each time you refresh the integration? Or is it just that the integration needs to be reloaded and that the device ID stays the same?

Also, please forgive the newbie question, but to use your code, would I just add it here where I’ve put the red arrow?

Thanks so much!

David

IMG_0002

phantomsphorever commented 1 year ago

For some reason your approach did not work for me, so I actually wrote a script and the tap action only calls the script. The device ID is always the same - basically your KIA or Hyunday

Screenshot 2023-10-28 130039

phantomsphorever commented 1 year ago

Here is the Yaml version of the script: Screenshot 2023-10-28 130203

hogsterbogster commented 1 year ago

YES it works! Thank you so much phantomsforever! 🤩🙏