Hi All.
I’ve been successfully developing my webhook smart app in Python.
All is working well outside of subscriptions.
I’m trying to set up multiple subscriptions to switches I select in my app.
From looking at examples and the smart things API docs, I cannot pass multiple device id’s in a single POST request for a DEVICE subscription.
Here’s the API snippet.
Here’s my device subscription function. Works perfectly when passing in a single device ID as per the API.
def switchsubscription(authtoken,component,id,AppID)
BaseURL = 'https://api.smartthings.com/v1/installedapps/'
headers = {'Authorization': 'Bearer '+authtoken}
EndURL = '/subscriptions'
#debug to print what is passed in
print('Heres the full request within the function......'+BaseURL+str(AppID)+EndURL)
datasub = {
"sourceType": "DEVICE",
"device":{
"componentId": component,
"deviceId": id,
"capability": "switch",
"attribute": "switch",
"stateChangeOnly": "true",
"subscriptionName": "switch_subscription",
"value": "*",
}
}
r = requests.post(BaseURL+str(AppID)+EndURL,headers=headers,json=datasub)
#error checking for creation of subscription
print(r.status_code)
print('The below is the content for the fault code')
if r.status_code == 200:
print('Individual Subscription successfully created')
else:
print('Error Creating individual subscription')
This is called in the INSTALL AND UPDATE Lifecycle as per the Smart App Flow.
Ive tried calling it multiple times with multiple device ID’s as below but I keep getting a 409 error from the API after the first device subscription is successfully subscribed too.
Update Lifecycle Code with multiple subscription calls (Step 02)…
elif (content['lifecycle'] == 'UPDATE'):
print(content['lifecycle'])
print('The auth token is:' + str(content['updateData']['authToken']))
print('The app id is:' + str(content['updateData']['installedApp']['installedAppId']))
AppID = content['updateData']['installedApp']['installedAppId']
#Assign Token to variable
authtoken = content['updateData']['authToken']
BaseURL = 'https://api.smartthings.com/v1/installedapps/'
headers = {'Authorization': 'Bearer '+authtoken}
component = content['updateData']['installedApp']['config']['Switch'][0]['deviceConfig']['componentId']
#assign the selected sensors to a variable for ID
id = content['updateData']['installedApp']['config']['Switch'][0]['deviceConfig']['deviceId']
id1 = content['updateData']['installedApp']['config']['Switch'][1]['deviceConfig']['deviceId']
id2 = content['updateData']['installedApp']['config']['Switch'][2]['deviceConfig']['deviceId']
id3 = content['updateData']['installedApp']['config']['Switch'][3]['deviceConfig']['deviceId']
print('Heres the full request for the delete......'+BaseURL+AppID+'/subscriptions')
#Step 01 - Delete all subscriptions
r = requests.delete(BaseURL+AppID+'/subscriptions',headers=headers)
#Check if the delete sub done?
print(r.content)
if r.status_code == 200:
print('Subscription successfully deleted')
else:
print('Error deleting subscription')
#Step 02 - Call subscription functions for multiple devices
switchsubscription(authtoken,component,id,AppID)
switchsubscription(authtoken,component,id1,AppID)
switchsubscription(authtoken,component,id2,AppID)
switchsubscription(authtoken,component,id3,AppID)
Here’s the response. 200 for the first subscription call (Switch[0]) but then a 409 (conflictingfor the other 3.
Note that I don’t want to subscribe to All devices or use a capability subscription.
Any help would really be appreciated as all I really need is subscriptions working for selected devices and im happy :).
Thanks in advance guys.