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]