Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Instructions how to use #12

Open
PierreLevres opened this issue Jan 8, 2022 · 39 comments
Open

Instructions how to use #12

PierreLevres opened this issue Jan 8, 2022 · 39 comments

Comments

@PierreLevres
Copy link

Please add some pointers in the usage documentation as in how to you the module.

Apart from hyundai_kia_connect_api :

  • sample code for the parameters (especially region and brand)
  • sample code to initialise the module
  • sample code to eg. request status
@fuatakgun
Copy link
Member

This is still work in progress, a good starting point: home-assistant/core#62451

@fuatakgun
Copy link
Member

Let's keep this issue open to come up with basic documentation.

@cdnninja
Copy link
Collaborator

cdnninja commented Jan 8, 2022

I'll try come up with some of the basics in the next few days. Any example format you both like?

@cdnninja cdnninja closed this as completed Jan 8, 2022
@cdnninja cdnninja reopened this Jan 8, 2022
@cdnninja
Copy link
Collaborator

cdnninja commented Jan 8, 2022

Misclick on the close. I started on the homepage to document this. When I have more time I will add examples of initializing the module and a few basics calls. As a quick note we have done a recent refactor of the code, so far initial pass is done on EU and CA regions. USA hasn't been done yet, I hope to do that this weekend but don't have anyone lined up to test. The refactor allows all these APIs to work with multiple cars where as the previous versions only worked on the first car on an account.

I am interested to hear what you are working on if willing to share! It may help us ensure this API works for others as well.

@PierreLevres
Copy link
Author

I am interested to hear what you are working on if willing to share! It may help us ensure this API works for others as well.

My fist intention would to make sure that all API endpoints the app uses are made available. That is not to hard. That way this library will be up to par with Bluelinky.
Secondly I have some ideas for minor additions to the HA integration, such as setting charge schedule, adjust temperature for preheating, and retrieve information about trips and store that info in a database..

@PierreLevres
Copy link
Author

Any example format you both like?

Maybe a working sample code:
importing the module
connecting to the vehiclemanager
loop through a menu for

  1. get a cached status
  2. get a refreshed status
  3. get the location
  4. lock the door
  5. stop charging
  6. set the climate and start it
  7. display information on car and owner, service etc.

@fuatakgun
Copy link
Member

Did you go through the open home assistant PR? You will find answers for almost of these questions.

@PierreLevres
Copy link
Author

Did you go through the open home assistant PR? You will find answers for almost of these questions.
I don't have any questions. @cdnninja asked what documentation of this repo I would like to see, I answered his question..

@fuatakgun
Copy link
Member

Got it, until documentation is ready, that will give you a brief idea how we are using this.

@cdnninja
Copy link
Collaborator

@PierreLevres Any chance are your car is from the USA? Hoping to have someone help test and update either the Kia or Hyundai API to help get it working.

@PierreLevres
Copy link
Author

PierreLevres commented Jan 10, 2022

@PierreLevres Any chance are your car is from the USA? Hoping to have someone help test and update either the Kia or Hyundai API to help get it working.

Sorry no, European. But you can find some USA car owners in the discord server for BlueLinky. They might be willing to test.

@Empor-co
Copy link
Contributor

Empor-co commented Jan 14, 2022

Hi All,

I got the following so far:
from hyundai_kia_connect_api import *
vm = VehicleManager(region=1, brand=2, username="[email protected]", password="secret", pin="1234")
print(vm.check_and_refresh_token())
print(vm.vehicles)

Which returns the vehicle id, name, model and registration_date - the rest is pretty much "None".

@fuatakgun
Copy link
Member

Did you check the open PR link above? It will help you a lot?

@cdnninja
Copy link
Collaborator

Hi All,

I got the following so far: from hyundai_kia_connect_api import * vm = VehicleManager(region=1, brand=2, username="[email protected]", password="secret", pin="1234") print(vm.check_and_refresh_token()) print(vm.vehicles)

Which returns the vehicle id, name, model and registration_date - the rest is pretty much "None".

Try vm.update_all_vehicles_with_cached_state() right after you update vehicle manager. You will then have the data for the car.

@fuatakgun
Copy link
Member

Or call check_and_force_update_vehicles​, it will loop through all vehicles, get cached data from cloud and depending on force_refresh interval you supplied, it will try to force refresh stale data.

All data will be stored and updated inside Manager.vehicles dictionary

@cdnninja
Copy link
Collaborator

@PierreLevres the home page now has this above example listed plus the other methods vehicle manager offers.

@Empor-co
Copy link
Contributor

Hi all,

Thank you for posting the example.
I updated my code and got the following results:

vm.check_and_force_update_vehicles(60)
gives me the following error:

Traceback (most recent call last):
  File "/home/user/PycharmProjects/bluelinx/main.py", line 7, in <module>
    vm.check_and_force_update_vehicles(60)
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 65, in check_and_force_update_vehicles
    f"time diff - {(started_at_utc - vehicle.last_updated_at).total_seconds()}"
TypeError: can't subtract offset-naive and offset-aware datetimes

and
vm.update_all_vehicles_with_cached_state()
seems to struggle with the TEMPERATURE_UNITS being empty:

Traceback (most recent call last):
  File "/home/user/PycharmProjects/bluelinx/main.py", line 6, in <module>
    vm.update_all_vehicles_with_cached_state()
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 55, in update_all_vehicles_with_cached_state
    self.update_vehicle_with_cached_state(self.get_vehicle(vehicle_id))
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 58, in update_vehicle_with_cached_state
    self.api.update_vehicle_with_cached_state(self.token, vehicle)
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 202, in update_vehicle_with_cached_state
    TEMPERATURE_UNITS[
KeyError: 0

Potentially python-dateutil is interfering - any ideas?

@PierreLevres
Copy link
Author

PierreLevres commented Jan 15, 2022 via email

@cdnninja
Copy link
Collaborator

Hi all,

Thank you for posting the example. I updated my code and got the following results:

vm.check_and_force_update_vehicles(60) gives me the following error:

Traceback (most recent call last):
  File "/home/user/PycharmProjects/bluelinx/main.py", line 7, in <module>
    vm.check_and_force_update_vehicles(60)
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 65, in check_and_force_update_vehicles
    f"time diff - {(started_at_utc - vehicle.last_updated_at).total_seconds()}"
TypeError: can't subtract offset-naive and offset-aware datetimes

and vm.update_all_vehicles_with_cached_state() seems to struggle with the TEMPERATURE_UNITS being empty:

Traceback (most recent call last):
  File "/home/user/PycharmProjects/bluelinx/main.py", line 6, in <module>
    vm.update_all_vehicles_with_cached_state()
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 55, in update_all_vehicles_with_cached_state
    self.update_vehicle_with_cached_state(self.get_vehicle(vehicle_id))
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/VehicleManager.py", line 58, in update_vehicle_with_cached_state
    self.api.update_vehicle_with_cached_state(self.token, vehicle)
  File "/home/user/PycharmProjects/bluelinx/venv/lib/python3.10/site-packages/hyundai_kia_connect_api/KiaUvoApiEU.py", line 202, in update_vehicle_with_cached_state
    TEMPERATURE_UNITS[
KeyError: 0

Potentially python-dateutil is interfering - any ideas?

v1.18.8 should resolve that second keyError. Please retry and let me know if it is solved.

@cdnninja
Copy link
Collaborator

I have also cut a PR #15 for the first issue. This is caused by it having a last updated yet since we haven't called for vehicle data. It should work if you first call for cached state and then force the update. That should be resolved soon too though.

@Empor-co
Copy link
Contributor

@cdnninja Thank you very much - v1.18.8 fixed it: I know see plenty of details.
Is there also a way to e.g. activate the defrost remotely or set the per-conditioning timer?

@cdnninja
Copy link
Collaborator

@Empor-co Remote start does exist in the base APIs. We haven't exposed it yet to vehicleManager. It does work in the older version of this for home assistant though: https://github.com/fuatakgun/kia_uvo. I don't think implementing this would take long at all. I will cut a separate issue for remote start. For each feature like this could you cut a issue and we can track separately? Testers like you are key to this growing and being robust so thanks for playing with it!

This API is the early days of prepping for core integration. It eventually will be used by the above custom as well once it has feature parity.

@CreasolTech
Copy link

Hi,
thanks for this great module: I've start using it to make a plugin for Domoticz, https://github.com/CreasolTech/domoticz-hyundai-kia
I also think that some more comments in the VehicleManager.py will help who, as me, don't know enough about python programming and kia/hyundai API/restrictions/....
Every N minutes, I call:

vm.check_and_refresh_token()
vm.force_refresh_all_vehicles_states()
vm.update_all_vehicles_with_cached_state()

Is this sequence good to get a complete refreshed status from the vehicle?
While some status are freshed (e.g. is_charging, ev_range, ....), the location is 3 hours old. I tried several times to refresh, but location keeps old. Then I open Kia Connect app on my smartphone, check the location (that is old), enter pin, and location get refreshed to the new one on both smartphone and Domoticz.
What is the right way to get location refreshed?
Thanks a lot !!
Paolo

@PierreLevres
Copy link
Author

PierreLevres commented Feb 21, 2022 via email

@CreasolTech
Copy link

CreasolTech commented Feb 23, 2022

Thanks for the answer.
In this hyundai_kia_connect_api I cannot find a call to the url /location :
I've just checked the API from PierreLevres and found that, to get a refresh, he call, in sequence,
/status #to get all data and ask for vehicle fresh data
/status/latest # to read new data from cloud
then compare the first with second odometer values: if differ, call the url
/location # to get fresh coordinates
Is it useful to call /location url to get a fresh location, or is it useless?
Thanks.

@cdnninja
Copy link
Collaborator

cdnninja commented Feb 23, 2022 via email

@CreasolTech
Copy link

Which region? I assume EU? I don’t think location update is implemented for EU. That will be a quick fix if you can test it for me since I am in CA.

Yes, /location URL exists in EU region.
This is the response:
{'retCode': 'S', 'resCode': '0000', 'resMsg': {'gpsDetail': {'coord': {'lat': 45.8815, 'lon': 12.189481, 'alt': 0, 'type': 0}, 'head': 0, 'speed': {'value': 0, 'unit': 0}, 'accuracy': {'hdop': 0, 'pdop': 0}, 'time': '20220223205017'}, 'drvDistance': {'rangeByFuel': {'evModeRange': {'value': 394, 'unit': 1}, 'totalAvailableRange': {'value': 394, 'unit': 1}}, 'type': 2}}, 'msgId': '285475a6-b1db-48db-ab2c-9827b37f43fc'}

@cdnninja
Copy link
Collaborator

#51 adds this calling method. Interested on feedback. Current approach automatically forces this location update if the odometer has changed on the cached vehicle call. Would you rather to have the control available to you to update or are you okay with us checking if odometer changed and updating.

In a future PR I am also thinking of limiting since the last location call so it will only update location lets say once an hour.

@CreasolTech
Copy link

#51 adds this calling method. Interested on feedback. Current approach automatically forces this location update if the odometer has changed on the cached vehicle call. Would you rather to have the control available to you to update or are you okay with us checking if odometer changed and updating.

Thanks @cdnninja , I have just fetch your PR , but I have to move the car to know if it really works (and here is midnight!).
Probably the speed variable should also be updated...

@CreasolTech
Copy link

Hi @cdnninja ,
I've checked the lib with your PR, and I've also added

def get_location(self, vehicle_id: str) -> bool:
        self.api.get_location(self.token, self.get_vehicle(vehicle_id))

to VehicleManager.py so I can call vm.get_location().
If I call vm.force_refresh_all_vehicles_states() , I can see that API get /status and /status/latest , but they are exactly the same with the same odometer (when car is moving, of course).
If then I call vm.get_location(VEHICLEID) which call api.get_location(TOKEN, VEHICLE_ID)
then I get the updated location and speed.

So, the main problem is that vm.force_refresh_all_vehicles_states() does not poll data from the car, but always from the cache, so odometer does not change and api.get_location() is not called.
What is the method I should call to ask Kia to fetch data from the car? If you prefer, my Telegram nick is @CreasolTech

@ultima-originem
Copy link

Any update to the question posted directly above: "What is the method I should call to ask Kia to fetch data from the car?"

@PierreLevres
Copy link
Author

PierreLevres commented Mar 17, 2022

For EU users it is already there https://github.com/Hyundai-Kia-Connect/hyundai_kia_connect_api/blob/master/hyundai_kia_connect_api/KiaUvoApiEU.py#L328:
def force_refresh_vehicle_state(self, token: Token, vehicle: Vehicle) -> None:
url = self.SPA_API_URL + "vehicles/" + vehicle.id + "/status"

@ultima-originem
Copy link

That's what I thought but either there's a problem with my code or my understanding. Here's the essential part of the code:

def fetch_vehicle():
    vm = VehicleManager(
        brand=1,
        region=1,
        username=kia.username,
        password=kia.password,
        pin=kia.pin
    )
    vm.check_and_refresh_token()
    my_vehicle = vm.get_vehicle(kia.vehicle_id)
    vm.force_refresh_vehicle_state(my_vehicle)
    return my_vehicle

which returns (formatted):

2022-03-17 16:53:50 - primary battery: 45%, secondary battery: 91%, range: 193km, odometer: 560.4km

The problem is that the timestamp does not get updated when I run the code. Can you see the problem?

@PierreLevres
Copy link
Author

PierreLevres commented Mar 18, 2022 via email

@ultima-originem
Copy link

That seems to fix it. Current code:

def fetch_vehicle():
    vm = VehicleManager(
        brand=1,
        region=1,
        username=kia.username,
        password=kia.password,
        pin=kia.pin
    )
    vm.check_and_refresh_token()
    my_vehicle = vm.get_vehicle(kia.vehicle_id)
    vm.force_refresh_vehicle_state(my_vehicle)
    vm.update_vehicle_with_cached_state(my_vehicle)
    return my_vehicle

with result:

2022-03-18 14:08:23 - primary battery: 45%, secondary battery: 91%, range: 193km, odometer: 560.4km

It still seems to me a strange sequence of API calls and that a single call should suffice. Thanks for your advice; this was something I hadn't tried yet.

@fuatakgun
Copy link
Member

Thanks for sharing the updates here.

After creating VehicleManager object, you can call initialize() function which would generate the vehicle data from cached source. If you want, you can call check_and_force_update_vehicles regularly to keep the data fresh. One parameter here is the interval you want to force update vs cache update.

Force update would consume your daily api call capacity but cache update is just getting latest data from cloud, without any incur on your account and we want this decision to happen explicitly rather than opaque to consumer.

@CreasolTech
Copy link

CreasolTech commented Mar 18, 2022

Please can you explain in details (with a source code) what is the sequence to:

  1. initialize the vm object and get the vehicles status
  2. periodically, maybe every 30 minutes, to renew the tokens (if needed) and force the update vehicles state

As I don't know how the kia/hyundai cloud works and there is not much comments inside the library, maybe some doc about how to use the library methods is very helpful.
Thanks a lot.

@Fredrik-Bjuren
Copy link

How are you setting kia.vehicle_id? I try to set it to the first keyvalue in vehicles from the VehicleManager but I get KeyError. To the VIN-number of the car, still error. Can't understand what I'm doing wrong. Can someone give me a hint?

@cdnninja
Copy link
Collaborator

How are you setting kia.vehicle_id? I try to set it to the first keyvalue in vehicles from the VehicleManager but I get KeyError. To the VIN-number of the car, still error. Can't understand what I'm doing wrong. Can someone give me a hint?

Vehicle.id is where you get the id for a future all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants