Dynamic Lists - Pre-selecting Default ?

Posted on
Tue Sep 02, 2014 1:30 pm
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Dynamic Lists - Pre-selecting Default ?

If creating a Dynamic List in an Action.XML, how do you specify which entry is pre-selected :?:

Specifically, I want to avoid the '- no selection -' entry in the list. :)

Posted on
Tue Sep 02, 2014 3:09 pm
kw123 offline
User avatar
Posts: 8360
Joined: May 12, 2013
Location: Dallas, TX

Re: Dynamic Lists - Pre-selecting Default ?

after experimenting for a long time:

def theListGenerator( .... ):
retList =[]

##create your list here
retList.append((100,"this is your text for item 100")
retList.append((99,"this is your text for item 99")
retList.append((90,"this is your text for item 90")
retList.append((80,"this is your text for item 80")
retList.append((0,"this is your text for item 90")) # insert your default here with number 0 , could be listed twice, or you exclude # 90
return retList

you have to manage the list 0...100 yourself to know which is default, which was picked last time ..


seems to work for me

Karl

Posted on
Wed Sep 03, 2014 3:32 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Dynamic Lists - Pre-selecting Default ?

kw123 wrote:
after experimenting for a long time ...
... seems to work for me

Thanks for putting the time in to investigate this :)
However, I am using the dynamic List in a Menu (which maybe I should have mentioned in the original post :oops: )

I have just looked at what you suggested and tried it out on a type="list" (dynamic list) and AFAIK you can specify a static defaultValue in the xml and if you want a dynamic entry selected as default when the list is displayed you have to give the dynamic entry the same default value as specified in the xml. Ideally there needs to be a way to specify the default dynamically (as the list is dynamic!) - I can't see how to do this (or if it is possible?) from the documentation.

As far as the dynamic list in type="menu" is concerned, on first entry to the dialogue the dynamic list (in the menu) is showing "- no selection -" - I want to have it show a pre-selected entry. I want the default selection to be set on first entry into the dialogue and then to be able to amend the default on a dynamic reload (in my use case the default will vary according to user input). :)

Posted on
Wed Sep 03, 2014 6:18 am
kw123 offline
User avatar
Posts: 8360
Joined: May 12, 2013
Location: Dallas, TX

Re: Dynamic Lists - Pre-selecting Default ?

I believe put default value=0 there should do it. Then put ((0,"this is the default text ")) into the callback.


Sent from my iPhone using Tapatalk

Posted on
Wed Sep 03, 2014 8:49 am
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Dynamic Lists - Pre-selecting Default ?

As far as the dynamic list in type="menu" is concerned, on first entry to the dialogue the dynamic list (in the menu) is showing "- no selection -" - I want to have it show a pre-selected entry. I want the default selection to be set on first entry into the dialogue and then to be able to amend the default on a dynamic reload (in my use case the default will vary according to user input).

Haven't tried this, but couldn't you set the value that you want to be selected in the valuesDict that you get (with the current values of the form fields)? If I am thinking about this correctly, you would need to do it on the initial load (for new devices not already having a value) as well as callbacks that are forcing changing the menu's default.

Adam

Posted on
Wed Sep 03, 2014 9:50 am
matt (support) offline
Site Admin
User avatar
Posts: 21416
Joined: Jan 27, 2003
Location: Texas

Re: Dynamic Lists - Pre-selecting Default ?

I think Adam's approach is what you want. Try something like:

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

Image

Posted on
Wed Sep 03, 2014 11:38 am
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Dynamic Lists - Pre-selecting Default ?

Can't get it to work but maybe I am doing something wrong :?

Thought it best to expand in a bit more detail what my xml and code is doing. :)

For example in my Actions.xml, I have a field entry thus:
Code: Select all
         <Field id="testLifxLamp" type="menu">
            <Label>Test LIFX Lamp:</Label>
            <List class="self" method="actionConfigListLifxLampTestDevices"/>
            <CallbackMethod>actionConfigTestLamp</CallbackMethod>
         </Field>

The plugin method is as follows:
Code: Select all
    def actionConfigListLifxLampTestDevices(self, filter="", valuesDict=None, typeId="", targetId=0):
        self.myArray = []
        self.myArray.append((0, "No Lamp Testing"))  # First entry (which I want to be the default)
        for device in sorted(indigo.devices):
            if device.deviceTypeId == "lifxLamp":
                if device.address.lower() == "all" or device.address.lower() == "group":  # Ignore groups of lamps
                    pass
                else:
                    self.myArray.append((device.id, str("Test using %s" % (device.name)) ))
        return self.myArray

I would like the list to display like this, e.g.:
Code: Select all
No Lamp Testing
Test Using Dining Room Lamp
Test Using Study Lamp

At the moment the list entry initially displays with - no selection -.

When I try putting a
Code: Select all
<Field id="testLifxLamp" type="menu" defaultValue="0">
that doesn't work either, which I would have thought it should do if the first entry has a value of Zero. I have even tried changing the zero to "AAAA" (as a string).

I want to be able to dynamically specify the value of defaultValue and for that to be actioned.

I put the getMenuActionConfigUiValues def into my code and put an indigo log message in to check it is being invoked and it isn't.

Is the field I want to change called "defaultValue" and how do I discover what my menuID is?

Have I missed something here :?: - guess the answer is yes :lol:

Posted on
Wed Sep 03, 2014 11:57 am
matt (support) offline
Site Admin
User avatar
Posts: 21416
Joined: Jan 27, 2003
Location: Texas

Re: Dynamic Lists - Pre-selecting Default ?

I thought you were defining a menu action inside MenuItems.xml, but since you are defining an action inside Actions.xml you'll want to define this function instead:

Code: Select all
   def getActionConfigUiValues(self, pluginProps, typeId, devId):
      valuesDict = pluginProps
      errorMsgDict = indigo.Dict()
      if typeId == "yourActionId":  # where yourActionId is the ID for the action used in Actions.xml
         valuesDict["testLifxLamp"] = someDefaultValue  # probably based on devId?
      return (valuesDict, errorMsgDict)

Image

Posted on
Wed Sep 03, 2014 1:35 pm
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Dynamic Lists - Pre-selecting Default ?

matt (support) wrote:
I thought you were defining a menu action inside MenuItems.xml, but since you are defining an action inside Actions.xml you'll want to define this function instead ....


Brilliant - a one line addition - it is now coming up exactly as I want :D

It was a one line addition because you had already pointed me in the direction in this early post on Dialog Close Notification equivalent for open? and I was using it initialise text field values.

RogueProeliator wrote:
... as well as callbacks that are forcing changing the menu's default.


Thanks for this suggestion, I implemented it in a test callback and it working as I want So, I can now dynamically change the default later on once a button has been pressed :)

Thanks for everyone's input on this - I am hoping you will agree the effort is worthwhile when V2 of my LIFX plugin hits the streets :)

Posted on
Fri May 29, 2015 7:25 pm
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Dynamic Lists - Pre-selecting Default ?

I think I'm missing something between this thread and the docs. I have a PluginConfig field element:
Code: Select all
   <Field id = "foo"
      type = "menu"
      defaultValue = "None"
      <List class = "self" filter = "" method = "varListGenerator" dynamicReload = "true"/>
      <Label>Label Text</Label>
   </Field>

"varListGenerator is very simple. It just generates a list of all Indigo variables:
Code: Select all
   def varListGenerator(self, filter="", valuesDict=None, typeId="", targetId=0):

      List = [(var.id,  var.name) for var in indigo.variables]
      List.append((u'None', u'None'))
      return List


I'd like to have "None" as the default value of the dynamic list in the config dialog, but I get - select an item - instead.

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

[My Plugins] - [My Forums]

Posted on
Fri May 29, 2015 11:49 pm
autolog offline
Posts: 3988
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Dynamic Lists - Pre-selecting Default ?

Hi Dave,
Thanks to Adam you have an answer :D

It has made it so much easier to test and answer this using the Plugin Developer Documenter as a model. :)

Basically, you need to explicitly set the default value in getPrefsConfigUiValues and here's how:

First off your xml should look like this (it was missing a trailing '>' on the first tag :wink: ):
Code: Select all
      <Field id = "foo" type = "menu" defaultValue = "None" >
         <List class = "self" filter = "" method = "varListGenerator" dynamicReload = "true"/>
         <Label>Label Text</Label>
      </Field>
Then in your plugin you need, this code:
Code: Select all
   def varListGenerator(self, filter="", valuesDict=None, typeId="", targetId=0):

      List = [(var.id,  var.name) for var in indigo.variables]
      List.append((u'None', u'None'))
      return List

   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   # This routine returns the UI values for the configuration dialog; the default is to
   # simply return the self.pluginPrefs dictionary. It can be used to dynamically set
   # defaults at run time
   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   def getPrefsConfigUiValues(self):
      self.debugLog(u'Called getPrefsConfigUiValues(self):')
      prefsConfigUiValues = self.pluginPrefs
      prefsConfigUiValues['foo'] = u'None'
      return prefsConfigUiValues
The result is this:
Forum Answer 2015-05-30.png
Forum Answer 2015-05-30.png (64.15 KiB) Viewed 4998 times


I'll leave Adam to comment whether this is the best way to do this as I have replaced the code:
Code: Select all
return super(Plugin, self).getPrefsConfigUiValues()
with this (in order to return the modified dictionary):
Code: Select all
return prefsConfigUiValues
The really useful thing about using the Plugin Developer Documenter as a model to answer questions is that everyone using it gets to work off the same base. In addition I think the answers to questions could be incorporated into the model as examples. :)

Posted on
Sat May 30, 2015 5:06 am
DaveL17 offline
User avatar
Posts: 6751
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Dynamic Lists - Pre-selecting Default ?

This is brilliant. Thank you Jon (and Adam!)

The lost '>' was actually a cut and paste error on my part. I had cut out a few lines for clarity is all.
Code: Select all
   <Field id = "foo"
      type = "menu"
      defaultValue = "None"
      visibleBindingId = "space06"
      visibleBindingValue = "true"
      tooltip = "A very important message goes here.">
      <List class = "self" filter = "" method = "varListGenerator" dynamicReload = "true"/>
      <Label>Label Text</Label>
   </Field>

So, within this particular plugin, I have 53 such props that I want to set. So rather than have 53 of these:
Code: Select all
prefsConfigUiValues['foo'] = u'None'

or worry about when I come back later and add a field, I have modified the method thusly:
Code: Select all
   def getPrefsConfigUiValues(self):
      self.debugLog(u'getPrefsConfigUiValues(self) method called:')
      prefsConfigUiValues = self.pluginPrefs
      for key in prefsConfigUiValues:
         if prefsConfigUiValues[key] == '':
            prefsConfigUiValues[key] = u'None'
      return prefsConfigUiValues

This of course sets all empty key:value pairs to 'None', but for me it works. :)

Cheers!
Dave

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

[My Plugins] - [My Forums]

Posted on
Sat May 30, 2015 7:35 am
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Dynamic Lists - Pre-selecting Default ?

I'll leave Adam to comment whether this is the best way to do this as I have replaced the code:
return super(Plugin, self).getPrefsConfigUiValues()

You are fine with how you did that, the default in that base class routine is to simply return the self.pluginPrefs (since the defaults should be what is currently there) and an empty error message dictionary. As long as you grab the existing pluginPrefs, which you did in that code, before applying defaults, you should be good to go.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests