jonkristian / alko

AL-KO Robolinho component for Home Assistant
MIT License
17 stars 3 forks source link

NOT an issue but important info #6

Closed michaeltryl closed 3 months ago

michaeltryl commented 5 months ago

I have contacted Al-ko today to inquire about opening up their API, and it is now possible to access it via this link: https://alko-garden.dk/api-adgang/

udosw commented 5 months ago

This is great news. I tried to find the same page on their German site, but https://alko-garden.de/api-zugang/ or similar only return 404. However, here are the terms in Englsih: https://alko-garden.de/api-terms-of-service/ . Home Assistant is mentioned explicitly! I filled in the form on the Danish site and will report about their reply here.

michaeltryl commented 5 months ago

great :-)

michaeltryl commented 5 months ago

Have you heard from al-ko I have also asked for api acces, but haven't heard anything yet.

udosw commented 5 months ago

I've written an email to gardentech@al-ko.com (which is their German service address) and asked, wether there is a german page similar to the Danish https://alko-garden.dk/api-adgang/.

Their reply was very disappointing:

Sehr geehrte Damen und Herren,
nein - wir können Ihnen keinen Zugang mitteilen.

Ihre Anfrage mit der Vorgangsnummer XXXXXX wurde abgeschlossen.

Beste Grüße aus Kötz
Ihr Kundenservice-Team – AL-KO Gardentech

Translated: "No, we can't tell you any access. The ticket was closed."

I will definitely open the ticket again and keep on asking them.

udosw commented 5 months ago

@michaeltryl: Did you apply for access at https://alko-garden.dk/api-adgang/ ? Any response from them?

michaeltryl commented 5 months ago

Yes have asked 3 days ago, but no answer yet

michaeltryl commented 5 months ago

I wrote to them yesterday because I had not received an answer to my api request. Received my api key today and info on how to use it. However, I don't have much sense On coding things for the home assistant. I write to teknik@al-ko.com in Danish but it was a English mail that came back

sikysikov commented 5 months ago

@michaeltryl Can you share just coding instructions to understand their way of integration?

sikysikov commented 5 months ago

Ok, I already received API access from AL-KO. General link to API request is here: https://alko-garden.com/api-access

API docs: https://sikysikov.github.io/alko-api/

Lets start with integration @jonkristian

jonkristian commented 5 months ago

Ok, I already received API access from AL-KO. General link to API requested is here: https://alko-garden.com/api-access

API docs: https://sikysikov.github.io/alko-api/

Lets start with integration @jonkristian

Nice, I've requested access now, so we will see how long it takes.

sikysikov commented 5 months ago

@jonkristian Will be fast, I requested yesterday...

michaeltryl commented 5 months ago

it is the same i have :-)

udosw commented 5 months ago

Today I've got my API access as well, without having asked again (apart from the original application on their Danish site). It is completely mad, that they sign the mail with the same German support (street) address. From that very address they told me a few days ago that they can't tell me any API access addres.

Anyway, I managed to get an access token and to request the basic details of my Robolinho. The replies come in plain json, I like that.

This is what I got from the "thingsGet" request (I try to anonymize it):

[{"thingName": "33XXXXXXXXXXXXXXXXXXXX8c1cXXXXXXXX2deaa", "thingType": "ALKO-ROBOLINHO", "thingAttributes": {"thingName": "335XXXXXXXXXXXXXXXXXdeaa", "thingType": "ALKO-ROBOLINHO", "articleNumber": "127618", "serialNumber": "1CXXXXXX8", "firmwareWifi": "1.9.8", "hardwareVersionWifi": "1D", "firmwareMain": "2402A-SW", "firmwareMainLocalization": "SW", "serialNumberMain": "1CXXXXXXXX8", "hardwareVersionMain": "1D", "thingModel": "Robolinho450", "firmwareWifiDriver": "V19.6.1", "serialNumberWifi": "0123XXXXXXXXXXXXXXEE", "fotaMode": "off", "betatester": "False"}, "accessInformation": {"accessId": "50XXXXXXXXXXXXXXXXXXXXXXX738", "thingName": "3XXXXXXXXXXXXXXXXXXXXXXXeaa", "userId": "00XXXXXXXXX2", "idpAccountId": "6XXXXXXXXXXXXXXXXXXX5d7", "userEmail": "uXXX@XXXXXX.XX", "accessAlias": "eSheep", "accessAdmin": true, "accessCreated": "2023-10-21T15:03:16.961Z"}}]

udosw commented 5 months ago

Friends, things are moving (or should I say: 'mowing') in big steps. This very moment I got my Robolinho to START MOWING via the API.

Their API documentation is not so informative regarding to get the mower to do things, but I did a guess. I used

TOKEN=eyJhbGciOCT...........................
thingname=335XXXXXXXXXXXXXXXeaa

curl -X GET -H "Authorization: Bearer $TOKEN" -H "Accept: application/json" https://api.al-ko.com/v1/iot/things/${thingname}/state

to see the states. This reports something like:

{"desired": {}, "reported": {"isConnected": true, "rssi": -58, "batteryLevel": 97, "operationState": "WORKING", "operationSubState": "READY", "operationSituation": "OPERATION_PERMITTED", "operationError": {"code": 999, "type": "ISSUE", "description": "UNKNOWN"}, "nextOperation": ... (and lots more info)

I then operated the mower via the inTOUCH app and noticed that operationState changes between:

I then used this script and got the mower from stopped (IDLE) to actually mowing:

TOKEN=eyJhbGciOiXXXXXXXX........
thingname=335XXXXXXXXXXXXXXXXXXXXXXaa

curl -X PATCH -H "Authorization: Bearer $TOKEN" -H "Accept: application/json" https://api.al-ko.com/v1/iot/things/${thingname}/state/desired -d '{"operationState": "WORKING"}'

So, the desired state seems to be the way to get going ....

udosw commented 5 months ago

After sending the mower home via the API, there is the operationState

CitrixAdmin commented 4 months ago

I have my details from Al-Ko but I can't get my access token to work - keep getting invalid_client error. Would someone be kind enough to show me an example CURL to get my token?

udosw commented 4 months ago

I request my token successfully with a little shell script: First define the variables as in the email from AL-KO. Then use them in your curl request.

CLIENT_ID="you_at_doamin.xyz"  <=== from AL-KOs email
CLIENT_SECRET="XXXXXXXXXXXXXX" <=== from AL-KOs email
USERNAME="you@domain.xyz" <=== as used in your InTouch App
PASSWORD="YYYYYYYY"  <=== as used in your InTouch App
curl -s -X POST https://idp.al-ko.com/connect/token -d "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&grant_type=password&username=$USERNAME&password=$PASSWORD"|jq

I pipe the curl request through jq, which gives a nice output like this:

{
  "access_token": "XXXXX-----veeeeeeeeeeeeeeeery-long-secret-string-------XXXXXXXXXXX",
  "expires_in": 3600,
  "token_type": "Bearer",
  "refresh_token": "XXX-short-secret-string-XXX",
  "scope": "alkoCulture alkoCustomerId introspection offline_access openid profile roles"
}

HTH, Udo

CitrixAdmin commented 4 months ago

I'll try that - thank you

MiG-41 commented 4 months ago

So what to do step by step with received datas in HA ? Still it is possible to use AL-KO Robolinho component for Home Assistant or you talk about something different ?

the-smart-home-maker commented 4 months ago

I think the AL-KO integration has to be adjusted slightly. Instead of asking for an access and refresh token it should rather just ask for the client id and client secret and then be able to request the token on its own.

CitrixAdmin commented 4 months ago

Thank you to @udosw for your help getting my access token - that worked. Cheers, Scott

jonkristian commented 4 months ago

I've started to look at the api today, and will begin work to update the code asap, time is a little limited at the moment.

EDIT: For today, I've updated the pyalko repository to be compatible with this new api.

https://github.com/jonkristian/pyalko

CitrixAdmin commented 4 months ago

Anyone worked out the CURL to disable a mowingWindow?

udosw commented 4 months ago

I started with requesting the mowingWindows to see the structure of the json: curl -s -X GET -H "Authorization: Bearer $TOKEN" -H "Accept: application/json" https://api.al-ko.com/v1/iot/things/${thingname}/state/reported | jq -r .mowingWindows This results in:

{
  "monday": {
[...]
  "sunday": {
    "window_1": {
      "activityMode": false,
      "marginMode": false,
      "startHour": 8,
      "startMinute": 0,
      "duration": 330,
      "entryPoint": 0,
      "narrowPassageMode": false
    },
    "window_2": {
      "activityMode": false,
      "marginMode": false,
      "startHour": 13,
      "startMinute": 30,
      "duration": 330,
      "entryPoint": 0,
      "narrowPassageMode": false
    }
  }
}

So the code to set a mowingWindow to active is:

curl -s -X PATCH -H "Authorization: Bearer $TOKEN" -H "Accept: application/json" https://api.al-ko.com/v1/iot/things/${thingname}/state/desired -d '{"mowingWindows": {"sunday": {"window_2": {"activityMode": true}}}}' You can even see the marker change from grey to red in the inTouch app realtime.

CitrixAdmin commented 4 months ago

@udosw Thank you - I was missing some of the curly brackets! Cheers, Scott

jonkristian commented 4 months ago

Good news, I've made some headway on the integration.

I've got a functional pyalko library and the integration works. But, I've had major headache with the config_flow.py because of the way alko want's us to authorize with additional username/password. When trying to add in username and password to the oauth handler, I could not for the life of me get it to work, so I had to come up with a hack which seems to work, but I would bet that there's a better way to do it. Currently, for this to work you will first have to create the application credentials and then run the integration setup.

I will push the code to a new dev branch and maybe someone with more experience could take a look at it, but also test.

EDIT: New code is now in the develop branch, expect bugs :laughing:

  1. Set up application credentials on this endpoint https://HA-URL/config/application_credentials
  2. Install from integrations screen.
sikysikov commented 3 months ago

Getting following error after successfully added integration:

Error fetching alko_coordinator data: 400, message='Bad Request', url=URL('https://idp.al-ko.com/connect/token')

jonkristian commented 3 months ago

@sikysikov Did you restart HA after installation, and also set up app credentials beforehand? The config flow is a little hack'ish at the moment.

sikysikov commented 3 months ago

@jonkristian Restarted, added credentials. After setup I got data for few seconds. After while got the error.

jonkristian commented 3 months ago

Hmm, it's working fine for me. if you haven't done so already, can you try to remove everything and re-install? Also if possible, could you enable debugging and see if you can get some more logs?

Curious to know if anyone else is experiencing this.

sikysikov commented 3 months ago

@jonkristian Uninstalled, updated, added integration, still same error:

2024-07-11 16:55:29.837 DEBUG (MainThread) [custom_components.alko] Finished fetching alko_coordinator data in 0.242 seconds (success: False)

sikysikov commented 3 months ago

You can see here, that first attempt after adding integration was successful:

2024-07-11 17:58:03.525 DEBUG (MainThread) [custom_components.alko.configflow] Implementations found: {'alko***': <custom_components.alko.api.AlkoLocalOAuth2Implementation object at 0xffff81b356a0>} 2024-07-11 17:58:04.375 DEBUG (MainThread) [custom_components.alko] Fetched devices: [<pyalko.objects.device.AlkoDevice object at 0xffff3afa2180>] 2024-07-11 17:58:04.375 DEBUG (MainThread) [custom_components.alko] Finished fetching alko_coordinator data in 0.563 seconds (success: True) 2024-07-11 17:58:04.858 DEBUG (MainThread) [custom_components.alko] Fetched devices: [<pyalko.objects.device.AlkoDevice object at 0xffff552effb0>] 2024-07-11 17:58:04.858 DEBUG (MainThread) [custom_components.alko] Finished fetching alko_coordinator data in 0.481 seconds (success: True)

jonkristian commented 3 months ago

Strange indeed, mine has been stable for days now.

Which model mower do you have?

Can you grab the access_token from .storage/core.config_entries and try and use it in a curl command? While you're reading that file, can you also verify that auth_implementation matches the one you created in application credentials?

EDIT: I remember I had some issue with these arguments when working on the pyalko code. If the token works, maybe you can test them with and without to check if they work for you or not?

?pimInfo=true&thingState=true&accesses=true

udosw commented 3 months ago

@sikysikov I had the exact same behavior in the first place, when I didn't realize how to enter the credentials like @jonkristian stated: Before you add the integration, you'll need to manually add your [application credentials](https://www.home-assistant.io/integrations/application_credentials/). There you have to supply the credential you've got from AL-KO:

CLIENT_ID="xxx_at_xxxxxx.xxx" (enter as OAuth client ID)
CLIENT_SECRET="xxxxxxxxxxxxxxxx" (enter as OAuth client secret)

Then you proceed to the al-ko integration and you have to enter the first two fields again:

CLIENT_ID="xxx_at_xxxxxx.xxx" 
CLIENT_SECRET="xxxxxxxxxxxxxxxx"
USERNAME="xxxx@xxxxxx.xxx"
PASSWORD="xxxxxxxxxxxxx"

Hth

sikysikov commented 3 months ago

I also believe there is an issue with saved credentials. What should be filled in "Name", "ID OAuth" and "Key OAuth"?

sikysikov commented 3 months ago

@udosw I missed your post and YES, that worked. I filled my login in credentials, which was wrong. In credentials need to be used client_id and client_secret! Thanks @udosw @jonkristian

MiG-41 commented 3 months ago

I also believe there is an issue with saved credentials. What should be filled in "Name", "ID OAuth" and "Key OAuth"?

So finally: "ID OAuth" is CLIENT_ID "Key OAuth" is CLIENT_SECRET and what about "Name" ? Empty , or any ?

the-smart-home-maker commented 3 months ago

I also believe there is an issue with saved credentials. What should be filled in "Name", "ID OAuth" and "Key OAuth"?

So finally:

"ID OAuth" is CLIENT_ID

"Key OAuth" is CLIENT_SECRET

and what about "Name" ? Empty , or any ?

I think the name is just for you to memorize for what those app credentials are used

DIYtechie commented 3 months ago

So, I added this repository and restarted Home Assistant. Before adding the integration I tried to add the credentials using the client id and client secret, but I got the following error:

Skærmbillede 2024-08-02 kl  11 18 17

So what to do now?

DIYtechie commented 3 months ago

Restarted Home Assistant two times more and now it works! Thanks!