Domochip / gardena2mqtt

Gateway to control GARDENA Smart system devices through MQTT
GNU General Public License v3.0
10 stars 4 forks source link

No success with compose file #11

Closed pellenbeck closed 2 months ago

pellenbeck commented 1 year ago

I am using a compose file to start docker container.

Compose file here on github is:

version: '3'
services:
  gardena2mqtt:
    container_name: gardena2mqtt
    image: domochip/gardena2mqtt
    devices:
    - /dev/serial/by-id/usb-HUAWEI_HUAWEI_Mobile-if00-port0:/dev/mobile
    environment:
    - GARDENA_USER=GardenaUserName
    - GARDENA_PASSWORD=GardenaPassword
    - GARDENA_APIKEY=GardenaApiKey
    - HOST=192.168.1.x
    - PORT=1883
    - PREFIX=gardena2mqtt
    - CLIENTID=gardena2mqttclid
    - USER=mqtt_username
    - PASSWORD=mqtt_password
    restart: always

Questions on that:

  1. Is the device definition needed? I do not think so.
  2. Where can I provide the api application secret?

If I try to start a docker container with the above compose file (without device information) and I got the following log response:

13:17:09: ===== gardena2mqtt v1.0.2 =====
13:17:09: ===== Prepare MQTT Client =====
13:17:09: ===== Prepare SmartSystem Client =====
13:17:09:  - create
13:17:09:  - authenticate
Traceback (most recent call last):
  File "/app/gardena2mqtt.py", line 210, in <module>
    smart_system.authenticate()
  File "/usr/local/lib/python3.9/site-packages/gardena/smart_system.py", line 115, in authenticate
    self.token = self.oauth_session.fetch_token(
  File "/usr/local/lib/python3.9/site-packages/requests_oauthlib/oauth2_session.py", line 360, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/usr/local/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 429, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/usr/local/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 425, in parse_token_response
    validate_token_parameters(params)
  File "/usr/local/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 432, in validate_token_parameters
    raise_from_error(params.get('error'), params)
  File "/usr/local/lib/python3.9/site-packages/oauthlib/oauth2/rfc6749/errors.py", line 402, in raise_from_error
    raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.UnauthorizedClientError: (unauthorized_client) Unauthorized client: `grant_type` is invalid

Without providing the application secret that can not work I guess ...

Domochip commented 1 year ago

Hi,

thanks for your feedback.

  1. you're right. device is not needed (copy-paste...)
  2. As per doc, you need to put Application key in corresponding variable image
pellenbeck commented 1 year ago

By unsing the Husqvarna Developer Portal and creating an own application you have the following parameters:

I guess "GARDENA_USER" and "GARDENA_PASSWORD" are the Portal credentials. For oauth the application credentials are needed - or am I wrong?

Domochip commented 1 year ago

"GARDENA_USER" and "GARDENA_PASSWORD" are the Portal credentials : yes And then you need the "Application key" in GARDENA_APIKEY : image Don't need the Application secret

pellenbeck commented 1 year ago

Okay, fine. I used exactly those values in the composer file. So the problem is not related to my values.

Domochip commented 1 year ago

do you still have the issue? Could you then give it a try with the docker line instead of the compose for testing purpose?

pellenbeck commented 1 year ago

Yes, the logging is the same.

Domochip commented 1 year ago

Do you have both API connected to your app? image

pellenbeck commented 1 year ago

Yes. Both APIs are active in my account.

pellenbeck commented 1 year ago

I played around with Gardena credentials and updated password for API etc. but the problem is still the same. @Domochip should I open an issue on https://github.com/py-smart-gardena/py-smart-gardena ?

Domochip commented 1 year ago

I think so because the authentication is done in this library yes

pellenbeck commented 1 year ago

@Domochip I have now been able to do some debugging. The current version of the Husqvarna/Gardena API only works with authorization via "application key" and "application secret". This is also already considered in the current version of smart_system.py.

I have entered "client_id" and "client_secret" in line 208 of gardena2mqtt.py with the correct values and when starting the newly created image, the authorization runs through - no errors in the log file.

Unfortunately, the next error is now found in the log files in line 221:

05:31:33: ===== gardena2mqtt v1.0.2 =====
05:31:33: ===== Prepare MQTT Client =====
05:31:33: ===== Prepare SmartSystem Client =====
05:31:33:  - create
05:31:33:  - authenticate
/app/gardena2mqtt.py:212: RuntimeWarning: coroutine 'SmartSystem.authenticate' was never awaited
  smart_system.authenticate()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
05:31:33:  - update location list
/app/gardena2mqtt.py:214: RuntimeWarning: coroutine 'SmartSystem.update_locations' was never awaited
  smart_system.update_locations()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last):
  File "/app/gardena2mqtt.py", line 221, in <module>
    for device in location.devices.values():
                  ^^^^^^^^
NameError: name 'location' is not defined

I will stop my attempts now. I don not know Python well enough to continue debugging here. I hope you find time to look at the necessary changes ... Good luck!

Domochip commented 2 months ago

Hey, sorry for the late reply 😄

I've adjusted the code for the new authentication and asynchronous methodology. remark : I removed the location in the topic structure too

The new version is now available as "domochip/gardena2mqtt:latest-dev" It would be great if you can test it before I release.

thanks