I am writing (in python) a custom integration of SmartThings for HomeAssitant.
I need to receive events from SmartThings for this I want to use “WEBHOOK_SMART_APP” as appType.
My app is created and confirmed (I tried both via PING and via CONFIGURATION flow) but when I create the authorization url and authorize the location I am redirected to the “redirect_uri” url without authorization code “code” but with errors in query string “error=invalid_request&error_description=The request is malformed.”.
Installing a WEBHOOK_SMART_APP doesn’t involve an authorization code. You create/register, verify and publish the app via the Developer Workspace and install instances of the app via the mobile apps. Renewable access and refresh tokens are delivered via the INSTALL and UPGRADE lifecycles, and five minute access tokens via the EVENT lifecycle. There are ways of bypassing the Developer Workspace and apps but they aren’t publicly documented.
The authorization code is involved in the OAuth-In app flow (with the API_ONLY app type).
The OAuth-In (API_ONLY) apps have a Webhook just like the WEBHOOK_SMART_APP. Instead of ‘lifecycle’ messages they receive ‘messageType’ messages, in particular the CONFIRMATION message for verification and the EVENT for status messages from SmartThings. The structure is identical where relevant.
You subscribe to the events in the same way you would with the Webhook SmartApps, you just don’t have the CONFIGURATION stuff to contend with.
Adding to what @orangebucket mentioned, SmartApps are not the same as OAuth (API) Access Apps, they cannot be created through the Developer Workspace.
These are the instructions to create an API Access app:
The option to create OAuth integrations cannot be found in the Developer Workspace.
You need to use the SmartThings CLI to create this type of app
Then, you need to start the OAuth 2.0 process which consists on:
The Access Token you get expires in 24 hours.
The Refresh Token expires in 29 days if not used. We suggest you refresh the token before this time, otherwise, you’ll lose the Refresh token and the User will need to re-authorize.
NOTE: Remember the OAuth integration has alimit of 500 installations by default. Each time a user authorizes access to one of his/her locations, it will count as 1 installation. This means, if a user has 3 locations and authorizes access to each of them, he/she will use 3 installations.
To refresh the Access Token, you need to use the same endpoint but the grant_type is different, here’s an example about this:
SmartApps don’t have the field of “redirect_uri”, only “target_url” so, maybe the redirect URI is set by default to the URL we saw to continue with the normal SmartApp lifecycle events and the engineering team saw that in the logs.
The goal is that every installation of HomeAssistant integration will create a SmartApp using http request (no command line) using personal access token and oauth flow.
On the documentation i cant find a section to subscribe for devices update using API_ONLY, but only for installed apps
The subscription method is the same. You receive the installedAppId along with your access and refresh tokens.
The device events will be POSTed to your app’s target URL in a similar fashion to the documented EVENT lifecycle for Webhook and Lambda apps. You will see that they will have a messageType rather than a lifecycle and they don’t include a temporary access token.
I would recommend installing the CLI, setting it to debug mode, and then creating the app as Itati described above. It will show you the API calls it is making.
However I believe the following, which Itati also pointed you towards, illustrates the JSON to create an app with the target URL:
I’m curious about this, are you trying to achieve something with Home Assistant? Because there’s already an official integration about this that the HA and ST teams worked on and was announced here: New Integration Between Home Assistant and SmartThings
This is to avoid duplicated efforts, but as mentioned above, if your flow of the API_ONLY app is successful, then, you’ll get an installedAppId in the same access token object, for example:
If you haven’t registered a targetURL, you should edit the app to do so.
Remember this will also trigger a CONFIRMATION request (similar to a SmartApp) which means you need to verify the app to start receiving the events. This means you don’t need the an API_ONLY app to create another SmartApp for the subscriptions.