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
421 stars 85 forks source link

Car lock/unlock integration refresh #876

Closed Jurgen-DOUCHY closed 3 months ago

Jurgen-DOUCHY commented 4 months ago

Version of the integration 2.24.2

Describe the bug When trying to lock/unlock the car, The car locks/unlocks as expected. But the integration doas not update until you refresh manually again.

To Reproduce lock/unlock via the lock._door_lock

Expected behavior The switch should update to the current state

igalg99 commented 3 months ago

I believe that the workaround is to create automation uses the service to update the sensor everytime it is locked or unlocked via HA. However it won't really work if you lock it manually. I have also noticed that the integration does not update when something changes . Only every set number of minutes that is configured .. not ideal.. I hope it will be improved/solved.

rjantoine commented 3 months ago

Creating an automation would be quite the workaround. What could be done, is to use the check_action_status from the underlying API to update the status of the locks based on success or failure of the action when calling it from the coordinator. I would try forking it myself, but I don't know much about the async aspect of Home Assistant.

cdnninja commented 3 months ago

If interested in learning and trying I am happy to answer questions and point you in the right direction. As I don't own one of these cars I can't test changes like that so don't want to bite them off.

rjantoine commented 3 months ago

That would be great. I think I see two ways of doing this (none of which are tested yet). Does one way seem better advised? Do you see any errors in my code? I haven't used Python before, but I've done PHP, Javascript, C#, Java and more in the past. So it's all just syntax. 😛

To test this, I'm guessing I just SSH into my box and start changing the code? And then, to get it added to the project, fork it, commit the changes and do a pull request?

Method 1 doing a simple wait and update. This would be easy to copy/paste to every request

async def async_start_climate(
        self, vehicle_id: str, climate_options: ClimateRequestOptions
    ):
        await self.async_check_and_refresh_token()
+      """Add this to simply wait until action is complete, then update vehicles with cached state"""
-      await self.hass.async_add_executor_job(
+        action_id = await self.hass.async_add_executor_job(
            self.vehicle_manager.start_climate, vehicle_id, climate_options
        )
+     await self.hass.async_add_executor_job(
+           self.vehicle_manager.check_action_status, vehicle_id, action_id, true
+     )
+     await self.hass.async_add_executor_job(
+           self.vehicle_manager.update_all_vehicles_with_cached_state
+      )
        await self.async_request_refresh()

Method 2 updates the state manually

async def async_start_climate(
        self, vehicle_id: str, climate_options: ClimateRequestOptions
    ):
        await self.async_check_and_refresh_token()
+      """Add this to check status and update state accordingly"""
-      await self.hass.async_add_executor_job(
+        action_id = await self.hass.async_add_executor_job(
            self.vehicle_manager.start_climate, vehicle_id, climate_options
        )
+     res = await self.hass.async_add_executor_job(
+           self.vehicle_manager.check_action_status, vehicle_id, action_id, true
+     )
+     if res == "success":
+          vehicle = self.vehicle_manager.get_vehicle(vehicle_id)
+          vehicle.engine_is_running = True
+          self.async_write_ha_state()
        await self.async_request_refresh()
cdnninja commented 3 months ago

You will need to pause before calling the check action since it won't be done yet. You most likely need to loop on that a few times until it is complete, then call request refresh. Request refresh I think updates the car so no need to call the API. Method 2 I don't think is ideal.

This feature was actually dropped from the 2.x branch but existed in the old design. That code is here: https://github.com/Hyundai-Kia-Connect/kia_uvo/blob/v1.8.2/custom_components/kia_uvo/Vehicle.py

cdnninja commented 3 months ago

As for testing, you can add the samba share add on. You can browse to the folder. I often change within my visual studio / github and copy the folder to home assistant. You can also use dev containers but more work to setup, faster and less prod impact though.

cdnninja commented 3 months ago

This was also someone working on this check status feature but I don't think aligned to the API. https://github.com/Hyundai-Kia-Connect/kia_uvo/pull/511

rjantoine commented 3 months ago

After going through the API code of the different countries, it seems self.vehicle_manager.check_action_status does not work as intended in Canada and the US. It seems, any response code, even pending or error, returns True, and the synchronous feature isn't implemented.

I figured out that I can edit the kia_uvo code in Home Assistant, but need to restart HA every time I make any change. I don't see how I can edit the underlying API to try and get self.vehicle_manager.check_action_status working as intended. If I can get the correct apiStatusCodes for pending and success, are you @cdnninja able to update the API to make KiaUvoApiCA work more like KiaUvoApiEU? If not, could you suggest how I might be able to try developing the underlying API?

Thanks

cdnninja commented 3 months ago

Wouldn't doubt it if the API needs some work. You would have two aspects of the check action. 1. Did the action finish? 2. Was it successful? Ideally all regions should function the same way from a abstraction standpoint so that home assistant can function correctly no matter the region.

To edit the API ideally just work in python locally, calling the package locally until you have it working how you want. Once done create a pull request to the API, I can merge it and then to update home assistant edit the manifest.json file with the version number that github creates on the PR merge. Home assistant auto downloads that version of package.

Correct on the restart of HA each edit, bit painful, that is where a dev container helps as it starts really fast since it doesn't have multiple integrations to load.

cdnninja commented 3 months ago

Can this be closed?

rjantoine commented 3 months ago

I believe so 😃