Pysmartthings example for setCode?

I’ve been able to hack status out of my environment with pysmarthings pretty easy, but using lockCodes:setCode is proving problematic. I can get it to work with CLI so I expect it’s just syntax for pysmartthings. Working CLI example:

smartthings devices:commands <dev_id> lockCodes:setCode(slot,“code”,“label”)

works great, but would like to do this from python using pysmartthings. The following does NOT work:
result = await device.command(“main”,“lockCodes”,“setCode”,[slot,“code”,“label”])

Any ideas? TIA!

Is this the library you’re referring to?

Are you getting a “body malformed error”?
Sometimes you have to escape the symbol ", it would be useful if you checked how that function is sending the request, especially in the function post_device_command because it is called from device.command

I was indeed getting the “body malformed error”. I no longer get that error, as it returns successful with the above argument formatting, but doesn’t actually have any impact on the device.

Do you know which controller is the device using? You can verify this by listing the devices using the ST CLI or a direct query to the SmartThings API

You can identify each type by these properties:

  1. DTH: deviceTypeName, DTH (which has other details), type: "DTH"
  2. Edge Driver: driverId (inside a property called “zigbee”, “zwave” or “lan”, depending on the protocol used by the device), type: "lan/zigbee/zwave"
  3. ST Schema: "viper", type: "VIPER"

If it’s a stock DTH, you can check its config in the official repo below and confirm there’s a handler for the command setCode.
This is because, despite the capability has the command on its properties, there needs to be a handler for this event to perform the required actions.

Thank you for your thoughtful response!

If it were the case that the handler wasn’t installed then why would the CLI work but not pysmartthings?

aah, sorry, I forgot you mentioned it worked on the CLI.

Well, then, there must be something else. The success message generated by pysmartthings is due to the “accepted” response from the API, right?

Have you verified the value sent in the command is correct? I mean the generated command before sending it to the ST API (I think it is in post_device_command).
We get the response from the API saying “accepted” and a 200 code when there were no obvious issues (syntax, capability’s name, etc.) but it doesn’t mean the execution inside the device handler was correct.

In your code, I only noticed some " symbols were missing in “slot”, have you corrected that?

device.command("main","lockCodes","setCode",["slot","code","label"])

Slot appears to be a numeric value, using a string value for “slot” produces a “The request is malformed” when using the CLI and pysmarthings.

And yes stepping through the code we get an “accepted” and a 200 under the covers.