Changing device types on-the-fly

This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
Forum rules
This is a legacy forum which is locked for new topics. New topics should be started in one of the other forums under Extending Indigo
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Changing device types on-the-fly

Post by berkinet »

I have a plugin in device that is defined as type="custom" in Devices.xml.

During the configuration dialog I would like to give the user an opportunity to recast the type as relay. Is that possible using getDeviceConfigUiXml. If so, does someone have an example?
User avatar
matt (support)
Site Admin
Posts: 21542
Joined: Mon Jan 27, 2003 1:17 pm
Location: Texas
Contact:

Re: Changing deice types on-the-fly

Post by matt (support) »

Be careful, but try using changeDeviceTypeId:

Code: Select all

dev = indigo.device.changeDeviceTypeId(dev, newDevTypeId)
Be sure you replace any instances/caches of the old device type with the new one returned by changeDeviceTypeId(). Older instances will be stale (and I think using them might cause the plugin to crater).

Note you'll have to define the typeId in the XML still. So in the XML you would have IDs likes customUnknown, customRelay, customerDimmer, etc:

Code: Select all

<Device type="custom" id="customUnknown"> ...
<Device type="relay" id="customRelay"> ...
<Device type="dimmer" id="customerDimmer"> ...
Initially you would use customUnknown, then you would switch it on validation to one of the other types via its ID.

Lastly, I'm not positive changeDeviceTypeId() will work from the normal plugin device UI (I've never tried it). The Indigo client UI for the device dialog may not handle the class switch correctly. I do know, however, that it works from the plugin device factory UI. The SDK has an example of using the device factory UI. In general, if your plugin needs to dynamically change the device type and/or use device groups (multiple devices in tabs) then you should use the device factory technique instead (as illustrated by the SDK example).
Image
User avatar
berkinet
Posts: 3441
Joined: Tue Nov 18, 2008 2:08 pm
Location: Berkeley, CA, USA & Mougins, France

Re: Changing device types on-the-fly

Post by berkinet »

Thanks Matt. I'll check out changeDeviceTypeId.

In case anyone is wondering why changing the device type would be useful, I can explain what I am trying to do.

The Phidgets plugin supports analog sensors connected to a Phidgets I/O board (InterfaceKit). The analog devices report a number from 0 to 1000 (actually a representation of a 0 to 5 vdc reading). The devices built for these sensors are like the EasyDAQ "mirror" devices and are instantiated as device type = custom. They are not real devices that can be communicated with, but just represent a reading received from the InterfaceKit. The plugin code uses a stored formula to convert the 0-1000 reading to a number appropriate for the sensor: degrees Celsius, Lumens, volts, pH, etc.

In some cases a user might wish to see the state of an analog sensor as a simple On/Off value (I.e. < or > some arbitrary value). The benefit of doing this is that the device type could be instantiated as a relay(although effectively read-only), allowing it to be seen in the iTouch app and also used as a trigger source for the Group Trigger plugin.

I would like the choice of analog device (custom) or boolean (relay) to be made when the device is configured. Thus, the need to be able to change the device type in the config dialog.
Locked

Return to “Extending Indigo with Plugins and Python”