Devices.xml ConfigUI - how to get Option value name?

Posted on
Sun Jul 02, 2017 2:29 pm
zurich offline
Posts: 103
Joined: Aug 11, 2014

Devices.xml ConfigUI - how to get Option value name?

Greetings,

in Devices.xml ConfigUI I have this code below.

Code: Select all
<Field type="menu" id="city" defaultValue="">
            <Label>Weather for city:</Label>
            <List>
               <Option value=“47.3769, 8.5417”>Zurich</Option>
            </List>
         </Field>

To use the Option value “47.3769, 8.5417” in my plugin I get it with:

city = dev.pluginProps["city"]

How can I get the name of the city "Zurich"?

kind regards,

Z

Posted on
Sun Jul 02, 2017 2:59 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Devices.xml ConfigUI - how to get Option value name?

The easiest way is to construct the list dynamically rather than hard coding it in the Devices.xml file. Then you can just look up the label from the source used to generate the List values. For instance, you could define the following dictionary in the plugin.py file (but outside the Plugin subclass):

Code: Select all
# Add this to the top with other imports
from operator import itemgetter

# Add this somewhere before you define your Plugin subclass at the top of the file.
CITY_MAP = {
    “47.3769, 8.5417”: "Zurich",
    # add one line per city
}


Then define the following method in your Plugin subclass that will use that to generate the list:

Code: Select all
def get_city_list(self):
    # Dictionaries are unordered, so convert the dict to a list of tuples and sort it based on the city name label
    return sorted(CITY_MAP.items(), key=itemgetter(1))


Finally, in your Devices.xml:

Code: Select all
<Field type="menu" id="city" defaultValue="">
    <Label>Weather for city:</Label>
    <List class="self" filter="" method="get_city_list"/>
</Field>


Untested, but that's close. Later, when you need to figure out the label:

Code: Select all
# Somewhere in your code
# if the value city_coords is equal to "47.3769, 8.5417"
city_label = CITY_MAP[city_coords]

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Mon Jul 03, 2017 12:49 am
autolog offline
Posts: 3989
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Devices.xml ConfigUI - how to get Option value name?

Another simple option (excuse the pun) is to duplicate the city name in the value e.g.
Code: Select all
<Field type="menu" id="city" defaultValue="">
            <Label>Weather for city:</Label>
            <List>
               <Option value=“Zurich,47.3769,8.5417”>Zurich</Option>
            </List>
         </Field>
Then when you process the returned value you can split it on a comma e.g.
Code: Select all
city, latitude, longitude = returnedValue.split(',')
You could of course use a different separator character such as '$' if the comma becomes an issue. :)

Posted on
Mon Jul 03, 2017 1:07 pm
zurich offline
Posts: 103
Joined: Aug 11, 2014

Re: Devices.xml ConfigUI - how to get Option value name?

Greetings Jay,

Your suggestion works well. I did run into one small problem when creating the CITY_MAP, the comma in between the lat. and long. gave this error:

UI dynamic list function returned illegal ID string (ignoring): 47.3769, 8.5417


I just removed the comma in the CITY_MAP then added it later when needed.

Kind regards,

Z

Posted on
Mon Jul 03, 2017 1:56 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Devices.xml ConfigUI - how to get Option value name?

Glad it's working.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 4 guests