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

Charge Limits for CA #323

Closed zadigre closed 1 year ago

zadigre commented 2 years ago

Hyundai Kona Electric 2021, Canada­ 

Thank you for adding support of setting the charge limite through HA. it works really fine now.

There's only one feature "missing"... which might also be missing for other supported regions. Besides looking at the log every time, there's no way to know limits were applied successfully. Similar to the regular data which is updating on a regular basis, we could also update the charge limits and have the values set on some sensors. Or maybe get the results after setting the limit and push theses values to the sensors.

Is this something that can be "easily" implemented?

I'm currently receiving this in the log after setting the limit:

2022-04-21 09:21:39 DEBUG (SyncWorker_5) [custom_components.kia_uvo.KiaUvoApiCA] kia_uvo - Received Pin validation response <Response [200]>
2022-04-21 09:21:48 DEBUG (SyncWorker_5) [custom_components.kia_uvo.KiaUvoApiCA] kia_uvo - Received set_charge_limits response {'responseHeader': {'responseCode': 0, 'responseDesc': 'Success'}, 'result': [{'plugType': 0, 'level': 80, 'dte': {'rangeByFuel': {'gasModeRange': {'value': 0.0, 'unit': 1}, 'evModeRange': {'value': 274.0, 'unit': 1}, 'totalAvailableRange': {'value': 274.0, 'unit': 1}}, 'type': 2}}, {'plugType': 1, 'level': 80, 'dte': {'rangeByFuel': {'gasModeRange': {'value': 0.0, 'unit': 1}, 'evModeRange': {'value': 274.0, 'unit': 1}, 'totalAvailableRange': {'value': 274.0, 'unit': 1}}, 'type': 2}}]}

with my Mac on safari, I see two requests when accessing the set limit page on mybluelink.ca... I don't have access to an android device to sniff the trafic.

POST /tods/api/lstvhclsts HTTP/1.1
Content-Type: application/json;charset=UTF-8
Content-Length: 0
Accept: application/json, text/plain, */*
Accept-Language: fr-CA,fr;q=0.9
Accept-Encoding: gzip, deflate, br
Host: mybluelink.ca
Origin: https://mybluelink.ca
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15
Referer: https://mybluelink.ca/remote/charge
Cookie: <................cookie here.................>
from: CWP
accessToken: <................token here.................>
offset: -4
vehicleId: <................vehicule id here.................>
language: 0
POST /tods/api/evc/selsoc HTTP/1.1
Content-Type: application/json;charset=UTF-8
Content-Length: 0
Accept: application/json, text/plain, */*
Accept-Language: fr-CA,fr;q=0.9
Accept-Encoding: gzip, deflate, br
Host: mybluelink.ca
Origin: https://mybluelink.ca
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15
Referer: https://mybluelink.ca/remote/charge
Cookie: <................cookie here.................>
from: CWP
accessToken: <................token here.................>
offset: -4
vehicleId: <................vehicule id here.................>
language: 0
Kevr0n228 commented 2 years ago

Definitely would love a feature like this. Would be happy to test on my '19 kona (CAD).

cdnninja commented 1 year ago

When not setting the limits do you see any traffic showing the limits? Ideally in /status/latest. This would allow us to keep the data current event when changed via the kia app. It may be a separate API call to "view" the limits but that is fine.

cdnninja commented 1 year ago

As a note take a look at these steps. In Canada the API for the website is adequate, no android needed for sniffing: https://github.com/fuatakgun/kia_uvo/wiki/How-to-sniff-API-via-MS-Edge

Similar steps could be used in Safari. As a note selsoc endpoint is one we don't use today so interested to see what data that contains.

zadigre commented 1 year ago

yes I do see the limit on the selsoc response... both limits are currently set to 80

{"responseHeader":{"responseCode":0,"responseDesc":"Success"},"result":[{"plugType":0,"level":80,"dte":{"rangeByFuel":{"gasModeRange":{"value":0.0,"unit":1},"evModeRange":{"value":393.0,"unit":1},"totalAvailableRange":{"value":393.0,"unit":1}},"type":2}},{"plugType":1,"level":80,"dte":{"rangeByFuel":{"gasModeRange":{"value":0.0,"unit":1},"evModeRange":{"value":393.0,"unit":1},"totalAvailableRange":{"value":393.0,"unit":1}},"type":2}}]}

cdnninja commented 1 year ago

Perfect! That is exactly the data we need. To confirm selsoc is a view only command so I assume it isn't passing parameters behind car details? Separate from "evc/setsoc" which I believe sets the soc.

zadigre commented 1 year ago

I just clicked on "target state of charge" tab... and this what I've got. yes evc/setsoc is separate and this is what set the target soc.

just before selsoc, there is also https://mybluelink.ca/tods/api/lstvhclsts with the following response: {"responseHeader":{"responseCode":0,"responseDesc":"Success"},"result":{"status":{"lastStatusDate":"20221030204620","airCtrlOn":false,"engine":false,"doorLock":true,"doorOpen":{"frontLeft":0,"frontRight":0,"backLeft":0,"backRight":0},"trunkOpen":false,"airTempUnit":"C","airTemp":{"value":"00H","unit":0},"defrost":false,"acc":false,"evStatus":{"batteryCharge":false,"batteryStatus":78,"batteryPlugin":2,"remainTime2":{"etc1":{"value":4,"unit":1},"etc2":{"value":65,"unit":1},"etc3":{"value":15,"unit":1},"atc":{"value":0,"unit":1}},"drvDistance":[{"rangeByFuel":{"gasModeRange":{"value":0.0,"unit":1},"evModeRange":{"value":393.0,"unit":1},"totalAvailableRange":{"value":393.0,"unit":1}},"type":2}]},"hoodOpen":false,"transCond":true,"steerWheelHeat":0,"sideBackWindowHeat":0,"dte":{},"tirePressureLamp":{"tirePressureLampAll":0},"battery":{"batSoc":80,"batState":0,"batSignalReferenceValue":{}},"remoteIgnition":true,"seatHeaterVentInfo":{},"sleepModeCheck":false,"lampWireStatus":{"headLamp":{},"stopLamp":{},"turnSignalLamp":{}},"windowOpen":{},"engineRuntime":{}}}}

after setsoc, both lstvhclsts and selsoc are used to update what is shown on the screen.

cdnninja commented 1 year ago

The start of this. https://github.com/Hyundai-Kia-Connect/hyundai_kia_connect_api/pull/149 a bit more work needed, I should be able to get a version out in the next day or so with this ready for testing.

zadigre commented 1 year ago

For sure, I’ll be there to test it! 😉

cdnninja commented 1 year ago

Could you give v2.0.38 a go? This adds charge sliders to the main page. And feedback on functionality is appreciated.

zadigre commented 1 year ago

sliders are there... but if does nothing if I move them.

that said, there's an issue with force update service... I'll open a separate issue.

cdnninja commented 1 year ago

Any errors in the logs? I would expect it to sense the set command but jump back until the next refresh.

cdnninja commented 1 year ago

v2.0.39 is out.

zadigre commented 1 year ago

Any errors in the logs? I would expect it to sense the set command but jump back until the next refresh.

Sorry for not answering on this… to answer you question, no there was nothing in the log.

But now with 2.0.39, it works somewhat fine.

If I set one slider at a time, it’s fine. But if I want to set both slider one after the other, second slider will give an error in the log and will not set.

I guess there are two options… Wait a few seconds before sending the first command to see if an other one needs to be sent at the same time or wait at least 90 seconds before sending the second command.

See log: 2022-11-30 19:10:52.281 DEBUG (SyncWorker_3) [hyundai_kia_connect_api.KiaUvoApiCA] hyundai_kia_connect_api - Received forced vehicle data {'error': {'errorCode': '6533', 'errorDesc': 'We are currently processing an earlier inquiry for your vehicle. Please wait 90 seconds before issuing next request.'}, 'responseHeader': {'responseCode': 1, 'responseDesc': 'Failure'}} 2022-11-30 19:10:52.283 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140577671080080] 'result' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/websocket_api/commands.py", line 200, in handle_call_service await hass.services.async_call( File "/usr/src/homeassistant/homeassistant/core.py", line 1744, in async_call task.result() File "/usr/src/homeassistant/homeassistant/core.py", line 1781, in _execute_service await cast(Callable[[ServiceCall], Awaitable[None]], handler.job.target)( File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 208, in handle_service await service.entity_service_call( File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 678, in entity_service_call future.result() # pop exception if have File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 943, in async_request_call await coro File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 715, in _handle_entity_call await result File "/usr/src/homeassistant/homeassistant/components/number/__init__.py", line 109, in async_set_value await entity.async_set_native_value(native_value) File "/config/custom_components/kia_uvo/number.py", line 86, in async_set_native_value await self.coordinator.async_force_update_all() File "/config/custom_components/kia_uvo/coordinator.py", line 139, in async_force_update_all await self.hass.async_add_executor_job( File "/usr/local/lib/python3.10/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/usr/local/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 84, in force_refresh_all_vehicles_states self.force_refresh_vehicle_state(self.get_vehicle(vehicle_id)) File "/usr/local/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 87, in force_refresh_vehicle_state self.api.force_refresh_vehicle_state(self.token, vehicle) File "/usr/local/lib/python3.10/site-packages/hyundai_kia_connect_api/KiaUvoApiCA.py", line 141, in force_refresh_vehicle_state state = self._get_forced_vehicle_state(token, vehicle) File "/usr/local/lib/python3.10/site-packages/hyundai_kia_connect_api/KiaUvoApiCA.py", line 358, in _get_forced_vehicle_state response = response["result"]["status"] KeyError: 'result' 2022-11-30 19:11:21.769 DEBUG (SyncWorker_5) [hyundai_kia_connect_api.KiaUvoApiCA

cdnninja commented 1 year ago

Well in general this is good news! I am not sure how I can tell if a second command gets sent as this is part of home assistants event loop. That may be over my skill. I could put some form of lock or delay in though. Let me think on this.

zadigre commented 1 year ago

Good news indeed… I rarely modify the dc charge limit… so it’s fine for me if it stays they way it is right now. Would it be possible to catch the 90 seconds error and retry the last command after this 90 seconds? Or wait 90 seconds (or some other timeout we can configure within the integration) between all commands by implementing some kind of queue?

cdnninja commented 1 year ago

Yes retries are something we can look at. How about I close this for now since the basics of charge limits are working. I also have #428 for charge limits so this lets me shorten my list.

I am thinking we are close to getting 2.X out as the main release. I want to pause on new features to get that done, since I suspect once I remove that pre-release flag I will get more issues created for things we haven't thought of yet.