MenuItem ConfigUI saving/loading of values

Posted on
Tue Mar 27, 2018 12:27 pm
vtmikel offline
Posts: 338
Joined: Aug 31, 2012
Location: Boston, MA

MenuItem ConfigUI saving/loading of values

I need help with something I've never done before in a plugin and I couldn't find an example to look at.

I want to build a MenuItem with a ConfigUI that essentially contains advanced configuration values for my plugin. I noticed a few things:

Question 1: The fields in MenuItems ConfigUI don't seem to save the values automatically like they do in the Plugin Configuration...

Question 2: If I have to save the values of the fields in the MenuItem configUI myself, I can do that in the CallbackMethod, but where and how would I load those values back up, particularly when I need to change the selected item of lists or menus?

Thanks.

Posted on
Tue Mar 27, 2018 12:48 pm
FlyingDiver online
Posts: 2438
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: MenuItem ConfigUI saving/loading of values

First question - why a separate menu item and not just an advanced section of the normal config dialog? Perhaps a section hidden by an "Advanced" checkbox?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Mar 27, 2018 12:51 pm
vtmikel offline
Posts: 338
Joined: Aug 31, 2012
Location: Boston, MA

Re: MenuItem ConfigUI saving/loading of values

FlyingDiver wrote:
First question - why a separate menu item and not just an advanced section of the normal config dialog? Perhaps a section hidden by an "Advanced" checkbox?


I'll go this way if I need to. I already had a fairly advanced configuration so consider this "level 3", but still something that every user will need to access. This plugin has no devices, so everything will be in the plugin menu. This was my attempt to break it up.

Posted on
Tue Mar 27, 2018 12:57 pm
FlyingDiver online
Posts: 2438
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: MenuItem ConfigUI saving/loading of values

vtmikel wrote:
Question 1: The fields in MenuItems ConfigUI don't seem to save the values automatically like they do in the Plugin Configuration...


Where would it save them to? It's not the plugin config dialog, so it doesn't know you're trying to save those fields as pluginPrefs...

vtmikel wrote:
Question 2: If I have to save the values of the fields in the MenuItem configUI myself, I can do that in the CallbackMethod, but where and how would I load those values back up, particularly when I need to change the selected item of lists or menus?


You could add them to the pluginPrefs yourself, then fetch them as you would any other pluginPrefs data.

For example, in one of my plugins I save a list of seen messages to the pluginPrefs:

Code: Select all
            indigo.activePlugin.pluginPrefs[u"readMessages"] = newMessageList


And read it back in later:

Code: Select all
        oldMessageList = indigo.activePlugin.pluginPrefs.get(u"readMessages", indigo.List())

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Mar 27, 2018 12:59 pm
vtmikel offline
Posts: 338
Joined: Aug 31, 2012
Location: Boston, MA

Re: MenuItem ConfigUI saving/loading of values

That's what I thought. And, for a menu or list, how do I tell the UI which values should be selected?

Posted on
Tue Mar 27, 2018 1:00 pm
DaveL17 offline
User avatar
Posts: 4196
Joined: Aug 20, 2013

Re: MenuItem ConfigUI saving/loading of values

This is what I do.

Have a look at the Multitool plugin for an example. Look specifically at the Subscribe to Changes tool. The user manages those prefs through a menu config UI and the plugin saves them to hidden fields in the plugin config.

Works really well.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Mar 27, 2018 1:01 pm
FlyingDiver online
Posts: 2438
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: MenuItem ConfigUI saving/loading of values

vtmikel wrote:
That's what I thought. And, for a menu or list, how do I tell the UI which values should be selected?


I don't think I understand the question. If you have a menu item in the ConfigUI, you either hardcode a list into the Menu.xml or you provide a callback method that provides the list. Are you asking how to set the default entry in the list?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Mar 27, 2018 1:07 pm
vtmikel offline
Posts: 338
Joined: Aug 31, 2012
Location: Boston, MA

Re: MenuItem ConfigUI saving/loading of values

Example:

User visits the MenuItem UI for the first time, A Menu control is in the UI containing a list of devices. Default selection is hard coded into the MenuItems.xml.

User changes the list of selected devices in the control, saves, and the plugin saves the list of selected items to the pluginPrefs, as you suggested.

When the user goes back into the dialog, I provide the list of devices as I did on the initial load, but I need to select items based on the previous save, and not the defaults. <--- How do I do this last part?

Posted on
Tue Mar 27, 2018 1:12 pm
Colorado4Wheeler offline
User avatar
Posts: 2715
Joined: Jul 20, 2009
Location: Colorado

Re: MenuItem ConfigUI saving/loading of values

vtmikel wrote:
I need to select items based on the previous save, and not the defaults.

It's an unfortunate issue in the menu based config UI, if I really need to do that then I simply have a button a the top to refresh or I make the entire form invisible until they select an option, in which case that option has a callback that populates the fields. Now if you do this in the main plugin config then it'll pull from the dictionary of config prefs that is saved each time, but a custom menu based UI this is the only way I've found to make this happen.

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Posted on
Tue Mar 27, 2018 1:18 pm
vtmikel offline
Posts: 338
Joined: Aug 31, 2012
Location: Boston, MA

Re: MenuItem ConfigUI saving/loading of values

Colorado4Wheeler wrote:
vtmikel wrote:
I need to select items based on the previous save, and not the defaults.

It's an unfortunate issue in the menu based config UI, if I really need to do that then I simply have a button a the top to refresh or I make the entire form invisible until they select an option, in which case that option has a callback that populates the fields. Now if you do this in the main plugin config then it'll pull from the dictionary of config prefs that is saved each time, but a custom menu based UI this is the only way I've found to make this happen.


Okay, thanks. I'm glad I'm not going crazy. I'll head back to using the Config dialog.

Posted on
Tue Mar 27, 2018 1:23 pm
FlyingDiver online
Posts: 2438
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: MenuItem ConfigUI saving/loading of values

vtmikel wrote:
Example:

User visits the MenuItem UI for the first time, A Menu control is in the UI containing a list of devices. Default selection is hard coded into the MenuItems.xml.

User changes the list of selected devices in the control, saves, and the plugin saves the list of selected items to the pluginPrefs, as you suggested.

When the user goes back into the dialog, I provide the list of devices as I did on the initial load, but I need to select items based on the previous save, and not the defaults. <--- How do I do this last part?


I'm still not sure I get the UI. Is this a single selection menu popup (type="menu")? Or a multi-select list (type="list")? I'm confused because you used the plural "select items", which implies a list.

Are you trying to do something like the "Manage Devices" in the LIFX Bridge plugin menu?

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Mar 27, 2018 1:29 pm
vtmikel offline
Posts: 338
Joined: Aug 31, 2012
Location: Boston, MA

Re: MenuItem ConfigUI saving/loading of values

FlyingDiver wrote:
vtmikel wrote:
Example:

User visits the MenuItem UI for the first time, A Menu control is in the UI containing a list of devices. Default selection is hard coded into the MenuItems.xml.

User changes the list of selected devices in the control, saves, and the plugin saves the list of selected items to the pluginPrefs, as you suggested.

When the user goes back into the dialog, I provide the list of devices as I did on the initial load, but I need to select items based on the previous save, and not the defaults. <--- How do I do this last part?


I'm still not sure I get the UI. Is this a single selection menu popup (type="menu")? Or a multi-select list (type="list")? I'm confused because you used the plural "select items", which implies a list.

Are you trying to do something like the "Manage Devices" in the LIFX Bridge plugin menu?


You're right, I said "menu" when I meant "list" for the example I gave, but I have the same problem for both. In both cases, setting the selected items in the UI based on past saves is where I'm stuck.

Posted on
Tue Mar 27, 2018 1:43 pm
FlyingDiver online
Posts: 2438
Joined: Jun 07, 2014
Location: Cape Coral, FL

Re: MenuItem ConfigUI saving/loading of values

See this thread: viewtopic.php?f=108&t=12530&p=84214&hilit=dynamic+list+defaultValue#p84214

Basically, you need:

Code: Select all
   def getMenuActionConfigUiValues(self, menuId):
      valuesDict = indigo.Dict()
      errorMsgDict = indigo.Dict()
      if menuId == "yourMenuItemId":
         valuesDict["someFieldId"] = someDefaultValue
      return (valuesDict, errorMsgDict)


That should fill in the "defaultValue=" data for a specific menu field, which you can do from your saved data.
Last edited by FlyingDiver on Tue Mar 27, 2018 1:44 pm, edited 1 time in total.

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Mar 27, 2018 1:44 pm
DaveL17 offline
User avatar
Posts: 4196
Joined: Aug 20, 2013

Re: MenuItem ConfigUI saving/loading of values

There's a standard Indigo method for this.

Code: Select all
    def getMenuActionConfigUiValues(self, menuId):

        # Grab the setting values for the Subscribe to Changes tool
        if menuId == 'subscribeToChanges':

            changes_dict = indigo.Dict()
            changes_dict['enableSubscribeToChanges'] = self.pluginPrefs.get('enableSubscribeToChanges', False)
            changes_dict['subscribedDevices'] = self.pluginPrefs.get('subscribedDevices', '')

            return changes_dict

        else:
            return indigo.Dict()


it's called whenever a menu config UI is opened. I filter by the menu id. It works great.

Code: Select all
    <MenuItem id="subscribeToChanges">
        <Name>Subscribe to Changes...</Name>
        <CallbackMethod>subscribedToChanges</CallbackMethod>
            <ConfigUI>
                <Field id="subscribeToChangesLabel" type="Label">
                    <Label>The Subscribe to Changes tool only subscribes to object updates (not objects created or deleted). If initially disabled, enabling subscriptions (or vice versa) will cause a plugin restart to effect the change. This in normal.</Label>
                </Field>
                <Field id="subscriptionChangeLabel" type="Label" fontColor="red">
                    <Label>It's recommended that you disable subscriptions when you're done using the tool in order to reduce traffic between the server and plugin.</Label>
                </Field>
                <Field id="enableSubscribeToChanges" type="checkbox">
                    <Label>Subscribe to Changes:</Label>
                </Field>
                <Field id="subscribedDevices" type="textfield">
                    <Label>Subscribed Devices:</Label>
                </Field>
                <Field id="subscribedDevicesLabel" type="Label" alignWithControl="true">
                    <Label>Enter a list of devices to subscribe to. The list should be Indigo device and/or variable IDs (separated by a comma). Subscriptions to other object types are not supported.</Label>
                </Field>
            </ConfigUI>
    </MenuItem>


Code: Select all
    def subscribedToChanges(self, valuesDict, typeId):

        # If user changes subscription preference, set flag for plugin restart (see __init__)
        if self.pluginPrefs['enableSubscribeToChanges'] == valuesDict['enableSubscribeToChanges']:
            restart_required = False
        else:
            restart_required = True

        # Save preferences to plugin config for storage
        self.pluginPrefs['enableSubscribeToChanges'] = valuesDict['enableSubscribeToChanges']
        self.pluginPrefs['subscribedDevices']        = valuesDict['subscribedDevices']

        if restart_required:
            indigo.server.log(u"Preparing to restart plugin...")
            self.sleep(2)

            plugin = indigo.server.getPlugin("com.fogbert.indigoplugin.multitool")
            plugin.restart(waitUntilDone=False)

        return True


If the item is a list, I think you'll have to store the list as a string and convert it back to a list (if I'm understanding your scenario). Ignore the plugin restart bit--that was a special case because the tool is loading and unloading the subscribeToChanges() method which can only be done with a restart.

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Mar 27, 2018 3:16 pm
Colorado4Wheeler offline
User avatar
Posts: 2715
Joined: Jul 20, 2009
Location: Colorado

Re: MenuItem ConfigUI saving/loading of values

I'm so confused now... Are we talking about Plugin Prefs, a generic menu item that opens a config UI or are we talking about the Actions UI? Maybe better to ask, where is this config UI - in Devices.xml, Actions.xml, PluginConfig.xml or Menuitems.xml?

My Modest Contributions to Indigo:

HomeKit Bridge | Device Extensions | Security Manager | LCD Creator | Room-O-Matic | Smart Dimmer | Scene Toggle | Powermiser | Homebridge Buddy

Check Them Out Here

Who is online

Users browsing this forum: No registered users and 1 guest