How to programmatically recreate a device?

I’m writing an Edge driver for integration of an alarm system (Vanderbilt SPC). The driver creates automatically ST-devices for the Panel, all Alarm Areas (partitions) and all Alarm Zones. The data from the alarm system lacks information about the specific zone type (Contact sensor or Motion detector). Therefore it up to user to configure the type for each ‘imported’ zone. Only chance to change the type (profile) on an already created device, I think, would be to remove the device and create it again with a new profile.
I use the function try_create_device() for creation but can’t find any function for deletion of a existing device. How can I do that?

PS. From UX perspective, the best should be to be able to change the capability of the device from device itself (eg from contactSensor to motionSensor), but I can’t see that that is possible.

Why don’t you give it a try to the try_update_metadata API, which is intended for cases like this?

1 Like

@erickv
Actually, I have tried to replace the profile, using the try_update_metadata, without any success.
But if you are sure it should work I can give it another try.

Are you getting any specific error messages?


UPDATE

Just for reference, I tested the implementation, and it works as expected. What I did was to implement the API in response to a simple device command, i.e.:

...
local function handle_cmd(_, device, command)
  if command.component == 'main' then
    local success, msg = pcall(
      device.try_update_metadata,
      device,
      {profile='any-other-profile', vendor_provided_label='erickv'}) -- profile reference
  end
end

Note: notice that the profile referred must be pre-packaged.

1 Like

@erickv
Thank you very much for your help. I really appreciate your support. Probably I have used the function incorrectly. Just wondering… the parameter “vendor” you have in your example, shouldn’t be “vendor_provided_label”?

2 Likes

@gol I’m glad that it worked on your side as well!

You got me there :yum:

1 Like

@erickv
It seems that it isn’t possible to change the icon (the open lock in your case). Do you think you can confirm that?

1 Like

@gol It is possible, but it would be necessary to do an additional step which is to restart the app:

1 Like

@erickv
Is there any way to get current loaded profile?

@zambobmaz You can keep track of the profile Id under the device object / table (i.e. device.profile.id).

@erickv
I got it.
Thanks

1 Like