How to stateCallback all customer's devices

Hello everyone,
I’m working on the integration of my company device in the smart things platform.

We have a cloud connected device.

The device has multiple capabilities: 5 energyMeter and 2 powerMeter.

I would like to update the state of the 5 energy meters each day (so if i’m not wrong this should be the default behaviour and smart things should call my cloud with the state refresh once per day)

While for the 2 power meters, i would like to update their values every 5 seconds.
To do this i need to push the updated state, but i’m having trouble understanding something.

I think i need to do the following requests:

  1. https://c2c-eu.smartthings.com/oauth/token
    accessTokenRequest
    To obtain the accessToken required for the stateCallback

  2. https://c2c-eu.smartthings.com/device/events
    To perform the stateCallback and update the data.

The problems i found are:
A. To do the first request i need a ‘code’ inside the callbackAutehntication

“callbackAuthentication”: {
“grantType”: “authorization_code”,
“code”: “device-related-code”,
“clientId”: “my-client-id”,
“clientSecret”: “my-client-secret”
}

The only way i found to obtain this code is during the “grantCallbackAccess” request i receive from smart things after the first login to the oauth client during the first device registration on the Smart thign app.
Is this code without expiration?
If it has an expiration, how can i obtain it again?

B. The other problem is related to the stateCallback.
I did some trials and what i discoverd is that, since i use a device related ‘code’ to obtain the accessToken , then the stateCallback request allows me to update the state of that device only.
So i need to retrieve the ‘code’ for each device and use it for the accessToken request and finally use for each device its own token to update its state every 5 seconds during the day. Is this correct or am i missing something? Is there a way to update all customers devices state with only one request?

In short, i need to refresh the state of all my customers base devices each 5 seconds, is there any guide or advice tou can give me to complete the integration?

Thank you so much for you time and patience

Hi!
I would need to check with the ST team if it has an expiration time defined, but, if you already have the connector installed and lost the Reciprocal token or something similar, you can trigger the grantCallback interaction again by sending requestGrantCallbackAccess set to True in the discovery response whose request executes every 24 hours as you mentioned, or when we go to Linked services and select the schema to refresh it.
Here’s the info about it:

This is also useful if you detect when a reciprocal access token’s refresh is consistently failing and correct this by sending the discovery response described above (only when needed, the overuse of this flag is highly disadviced).

I don’t quite understand this, do you mean you got the Access Token when your integration only had one device, then, you added another one and the same Access Token doesn’t work to update the new device’s state?
I just made a test and I was able to update the two devices that belong to my schema integration with the same access token.

For this, would you use the powerMeter capability? Is there a reason to update this value so often?

1 Like

Thank you for the answer, so if i’m not wrong, i can use the refresh token as ‘code’ in the oauth request to generate a new access token when it is expired? (I tried and seems to work)

The only thing i’m not sure is about updating the state of multiple devices (belonging to different users) with one single ‘stateCallback’ request at https://c2c-eu.smartthings.com/device/events .
To be clear, if i make these steps:

There is user one and register device1 on his ST app.
Then user two register device2 on his ST app.

If i want to update the state of both devices.

  1. I need to generate an access token so i call the https://c2c-eu.smartthings.com/oauth/token using as ‘code’ the one i obtained from one of the two devices grantCallbackAccess request or alternatively the refresh token i obtained from a previous call to https://c2c-eu.smartthings.com/oauth/token .
  2. I make the following POST stateCallback request at https://c2c-eu.smartthings.com/device/events :

{
“headers”: {
“schema”: “st-schema”,
“version”: “1.0”,
“interactionType”: “stateCallback”,
“requestId”: “abc-123”
},
“authentication”: {
“tokenType”: “Bearer”,
“token”: “access-token”
},
“deviceState”: [
{
“externalDeviceId”: “device1”,
“states”: [
{
“component”: “main”,
“capability”: “st.powerMeter”,
“attribute”: “power”,
“value”: 100.0,
“timestamp”: 1720094693000
},
{
“component”: “aux”,
“capability”: “st.powerMeter”,
“attribute”: “power”,
“value”: 100.0,
“timestamp”: 1720094693000
}
]
} ,

{
“externalDeviceId”: “device2”,
“states”: [
{
“component”: “main”,
“capability”: “st.powerMeter”,
“attribute”: “power”,
“value”: 200.0,
“timestamp”: 1720094693000
},
{
“component”: “aux”,
“capability”: “st.powerMeter”,
“attribute”: “power”,
“value”: 200.0,
“timestamp”: 1720094693000
}
]
}
]
}

Will this update the state of both devices?

When the Access Token is expired, you need to refresh the access token with this request:

curl --location 'https://c2c-us.smartthings.com/oauth/token' \
--header 'Content-Type: application/json' \
--data '{
    "headers": {
        "schema": "st-schema",
        "version": "1.0",
        "interactionType": "refreshAccessTokens",
        "requestId": "abc-123-456"
    },
    "callbackAuthentication": {
        "grantType": "refresh_token",
        "refreshToken": "...",
        "clientId": "...",
        "clientSecret": "..."
    }
}'

The Refresh Access Token is valid until the user deletes the integration or the session is closed because it requires a new login which generates a new grantCallback interaction

I don’t think this is possible because an Access Token is unique for every installed instance of the Schema Connector, but I’ll confirm with the engineering team.

Following up, @emanuele.d1994
The engineering team confirmed you cannot update the status of devices from different users in the same call because:

  1. The access token you get is not for your connector in general
  2. When a callback is done, the user/device that needs to receive the update is identified based on the access token. So, its scope is to the devices discovered by the instance it is tied to.
1 Like