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

Setup of sensor platform kia_uvo is taking over 10 seconds. #726

Open capandcamera opened 11 months ago

capandcamera commented 11 months ago

Region and Brand of car UK, Hyundai

Version of the integration Integration Version: v.2.14.1 Home Assistant Version: 2023.9.3

Describe the bug A clear and concise description of what the bug is.

Recently the kia_uvo integration has been slowing down the startup of my home assistant installation. I've noticed the below error logs (included the DEBUG entries prior to this occurring too)

Debug logs if an error occurred

2023-09-30 15:11:37.095 DEBUG (SyncWorker_9) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - get_driving_info responseAlltime {'retCode': 'S', 'resCode': '0000', 'resMsg': {'drivingInfo': [{'drivingPeriod': 0, 'totalPwrCsp': 11784, 'motorPwrCsp': 7041, 'climatePwrCsp': 1503, 'eDPwrCsp': 3240, 'batteryMgPwrCsp': 0, 'regenPwr': 9429, 'calculativeOdo': 96}, {'drivingPeriod': 1, 'totalPwrCsp': 1071, 'motorPwrCsp': 640, 'climatePwrCsp': 136, 'eDPwrCsp': 294, 'batteryMgPwrCsp': 0, 'regenPwr': 857, 'calculativeOdo': 8.727272727272727}], 'drivingInfoDetail': [{'drivingPeriod': 1, 'drivingDate': '202309', 'totalPwrCsp': 1103, 'motorPwrCsp': 604, 'climatePwrCsp': 99, 'eDPwrCsp': 400, 'batteryMgPwrCsp': 0, 'regenPwr': 824, 'calculativeOdo': 8}, {'drivingPeriod': 1, 'drivingDate': '202308', 'totalPwrCsp': 6677, 'motorPwrCsp': 4458, 'climatePwrCsp': 749, 'eDPwrCsp': 1470, 'batteryMgPwrCsp': 0, 'regenPwr': 5901, 'calculativeOdo': 62}, {'drivingPeriod': 1, 'drivingDate': '202307', 'totalPwrCsp': 4004, 'motorPwrCsp': 1979, 'climatePwrCsp': 655, 'eDPwrCsp': 1370, 'batteryMgPwrCsp': 0, 'regenPwr': 2704, 'calculativeOdo': 26}]}, 'msgId': '8ed7df3c-0da2-4b99-9e8c-791149a69b9e'}
2023-09-30 15:11:37.229 DEBUG (SyncWorker_9) [hyundai_kia_connect_api.KiaUvoApiEU] hyundai_kia_connect_api - get_driving_info response30d {'retCode': 'S', 'resCode': '0000', 'resMsg': {'drivingInfo': [{'drivingPeriod': 0, 'totalPwrCsp': 210, 'motorPwrCsp': 10, 'climatePwrCsp': 0, 'eDPwrCsp': 200, 'batteryMgPwrCsp': 0, 'regenPwr': 0, 'calculativeOdo': 0}, {'drivingPeriod': 1, 'totalPwrCsp': 105, 'motorPwrCsp': 5, 'climatePwrCsp': 0, 'eDPwrCsp': 100, 'batteryMgPwrCsp': 0, 'regenPwr': 0, 'calculativeOdo': 0}, {'drivingPeriod': 2}], 'drivingInfoDetail': [{'drivingPeriod': 0, 'drivingDate': '20230913', 'totalPwrCsp': 106, 'motorPwrCsp': 6, 'climatePwrCsp': 0, 'eDPwrCsp': 100, 'batteryMgPwrCsp': 0, 'regenPwr': 0, 'calculativeOdo': 0}, {'drivingPeriod': 0, 'drivingDate': '20230912', 'totalPwrCsp': 104, 'motorPwrCsp': 4, 'climatePwrCsp': 0, 'eDPwrCsp': 100, 'batteryMgPwrCsp': 0, 'regenPwr': 0, 'calculativeOdo': 0}, {'drivingPeriod': 0, 'drivingDate': '20230901', 'totalPwrCsp': 893, 'motorPwrCsp': 594, 'climatePwrCsp': 99, 'eDPwrCsp': 200, 'batteryMgPwrCsp': 0, 'regenPwr': 824, 'calculativeOdo': 8}]}, 'msgId': '9bf7e4c0-ace3-4171-b887-e88e51b1a9e6'}
2023-09-30 15:11:37.229 ERROR (SyncWorker_9) [hyundai_kia_connect_api.KiaUvoApiEU] Failed to parse driving info. Possible reasons:
                                    - incompatible vehicle (ICE)
                                    - new API format
                                    - API outage

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 331, in update_vehicle_with_cached_state
    state = self._get_driving_info(token, vehicle)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 1055, in _get_driving_info
    drivingInfoItem["totalPwrCsp"]
ZeroDivisionError: division by zero

To Reproduce Restart homeassistant

Expected behavior Integration to load without error and within acceptable timeframe

dbolger commented 10 months ago

Also experiencing this. My guess is that it's due to the component loading all of the vehicle data on startup. Those requests take some time to go through on my end.

Maybe to mitigate this, we store the vehicle data in a table. We can then use a last updated time stamp to determine whether or not we should request new data from BlueLink, rather than load it into memory each time the program starts. Introducing a table or some way to persist vehicle data would also lower the total number of requests from someone who keeps restarting home assistant.

Another way could be by making the request happen asynchronously, since waiting for it blocks HA from finishing startup.

fuatakgun commented 9 months ago

great suggestions, I would go with option 1 because home assistant is signaling all integrations about readiness of the system (if everything is loaded and initialized)

If we skip the initial loading and initialize afterwards in unknown timeframe, we would not give the right signal to the rest of the ecosystem.

Agree on option 1, store the last updated at and check if it is in limits. If yes, use the cached values, if not ask for a new update.

Other than that, I am personally not bothered with this at all.

but the issue coming from @capandcamera is not related to a warning but an actual bug in the code, we should never divide something to zero :)