Update Plugin

Posted on
Sun Jun 11, 2017 9:58 am
bkmar1192 offline
Posts: 274
Joined: Sep 12, 2015

Update Plugin

How do you automatically refresh the plugin properties after adding a new field in the PluginConfig.xml? I have my devices refresh if I makes changes but haven't figured out how to refresh the plugin itself.

Thanks.

Posted on
Sun Jun 11, 2017 10:19 am
autolog offline
Posts: 3989
Joined: Sep 10, 2013
Location: West Sussex, UK [GMT aka UTC]

Re: Update Plugin

What I do in the plugin __init__ method (at the end of the method) is:
Code: Select all
self.validatePrefsConfigUi(pluginPrefs)  # Validate the Plugin Config
In the validatePrefsConfigUi method I have code like:
Code: Select all
            if "missedPollLimit" in valuesDict:
                try:
                    self.globals['polling']['missedPollLimit'] = int(valuesDict["missedPollLimit"])
                except:
                    errorDict = indigo.Dict()
                    errorDict["missedPollLimit"] = "Invalid number for missed polls limit"
                    errorDict["showAlertText"] = "The number of missed polls limit must be specified as an integer e.g 2, 5 etc."
                    return (False, valuesDict, errorDict)
            else:
                self.globals['polling']['missedPollLimit'] = int(360)  # Default to 6 minutes
Therefore (as in this example) I am setting up a global plugin variable named self.globals['polling']['missedPollLimit'] and it will have been initialised before the plugin gets going. If the user changes the plugin config then the same validation gets called and the user value will be used.

Not sure if this is the best way but it works for my plugins. :)

Posted on
Sun Jun 11, 2017 12:42 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Update Plugin

bkmar1192 wrote:
How do you automatically refresh the plugin properties after adding a new field in the PluginConfig.xml? I have my devices refresh if I makes changes but haven't figured out how to refresh the plugin itself.


Just make sure you set the property (or test then set) the property before you use it - you can do that in the __init__method or any other place. The init method is the easiest place probably - there are a wide variety of ways to do it. For instance, in my plugins I just do:

Code: Select all
self.some_prop = pluginPrefs.get("someProp", SOME_GOOD_DEFAULT_VALUE)


Basically, it's going to try to get the value from the plugin's config properties, and if it's not in there it uses a known good default value. So it'll automatically work even if the user has never opened the config dialog, which might be the case if the property was added in a later revision.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jun 15, 2017 7:41 am
bkmar1192 offline
Posts: 274
Joined: Sep 12, 2015

Re: Update Plugin

So I tried adding the following to my _init_:

477 self.some_prop = pluginPrefs.get("test3", "true")
478 junk = self.pluginPrefs["test3"]
479 indigo.server.log(junk)

But still get the error:

Traceback (most recent call last):
File "<string>", line 1, in <module>
File "plugin.py", line 478, in __init__
KeyError: key test3 not found in dict

test3 is a field I added to the PluginConfig.xml file. Is there a way to force a save of the plugin itself to rebuild the dict?

Posted on
Thu Jun 15, 2017 9:19 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Update Plugin

Right - that's because you're trying to get an unknown key in line 2. You should be using self.some_prop which is set in line 1. When the user changes the property in the config, line 1 will return whatever they have it set to, but in the meantime line 1 will just return the default value.

Oh, be sure to set self.some_prop in the closedPrefsConfigUi method for your plugin config so that when the user does change the value it will immediately be used when the dialog closes. Make sure that you only do it if the user didn't cancel the dialog:

Code: Select all
def closedPrefsConfigUi(self, valuesDict, userCancelled):
    if not userCancelled:
        self.some_prop = valuesDict.get("test3", "true")

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jun 15, 2017 9:28 am
bkmar1192 offline
Posts: 274
Joined: Sep 12, 2015

Re: Update Plugin

That is what I am trying to resolve - if I add a new field after initial release in the PluginConfig.xml is there a way to refresh the dict without forcing the user to open the plugin and save it. Can I save the plugin from code or does the user have to manually open and save the plugin?

Example:

Version 1.0 has fields a,b,c
Version 1.1 has new field d
After upgrading does the user have to open and save the plugin before the plugin will recognize field d?


Sent from my iPhone using Tapatalk

Posted on
Thu Jun 15, 2017 11:19 am
FlyingDiver offline
User avatar
Posts: 7220
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Update Plugin

This should help:
Code: Select all
        instanceVers = int(device.pluginProps.get('devVersCount', 0))
        if instanceVers == kCurDevVersCount:
            self.logger.threaddebug(u"%s: Device is current version: %d" % (device.name ,instanceVers))
        elif instanceVers < kCurDevVersCount:
            newProps = device.pluginProps

            # do version specific updates here
           
            newProps["devVersCount"] = kCurDevVersCount
            device.replacePluginPropsOnServer(newProps)
            device.stateListOrDisplayStateIdChanged()
            self.logger.debug(u"%s: Updated device version: %d -> %d" % (device.name,  instanceVers, kCurDevVersCount))
        else:
            self.logger.warning(u"%s: Invalid device version: %d" % (device.name, instanceVers))

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

Posted on
Thu Jun 15, 2017 11:23 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Update Plugin

No - because rather than using the props directly throughout your plugin, you only get the property value in the __init__ method:

Code: Select all
self.prop_d = pluginPrefs.get("d", "true")


and use self.prop_d throughout your plugin. So, if the property isn't in the plugin preferences, then it uses "true" until the user changes it (or forever if she never does).

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jun 15, 2017 11:26 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Update Plugin

Alternately, you can just set the default value yourself in the __init__ method if it doesn't exist:

Code: Select all
if "d" not in pluginPrefs:
    pluginPrefs["d"] = "true"

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Thu Jun 15, 2017 9:25 pm
bkmar1192 offline
Posts: 274
Joined: Sep 12, 2015

Re: Update Plugin

Thank you! that last one did what I wanted it to do..

I think it would be nice though to be able to refresh the entire plugin automatically. With this approach, every time I add a new feature to my plugin that uses a pluginpref value I have to add 2 lines to my python code.

Posted on
Fri Jun 16, 2017 8:47 am
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Update Plugin

You'll be surprised at how little you'll need to update the plugin config once you're past the initial development, so adding a couple of lines of code for each change is much less a burden that it may appear... ;)

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Fri Jun 16, 2017 9:40 am
kw123 offline
User avatar
Posts: 8365
Joined: May 12, 2013
Location: Dallas, TX

Re: Update Plugin

my init section is > 100 lines long for the bigger plugins compared to 30k lines of total code .. that should not be a problem

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests