Adding new states to a device plugin

Posted on
Thu Jan 21, 2021 3:59 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Adding new states to a device plugin

I'm updating my plugin and I've created a few new custom states. I want the newly created states to show up on devices created before the update. I've tried wording this a few different ways, but it always throws an error.... in my plugin.py after the def shutdown(self): ..., I added
Code: Select all
   def deviceStartComm(self, dev)
      for dev in indigo.devices(filter="com.whmoorejr.my-people"):
         dev.stateListOrDisplayStateIdChanged()      # will call getDeviceStateList() and getDeviceDisplayStateId()
or
Code: Select all
   def deviceStartComm(self, dev)         
         dev = indigo.devices[for dev in indigo.devices.iter(filter="com.whmoorejr.my-people")]
         dev.stateListOrDisplayStateIdChanged()     


I tried to base it off Matt's example https://forums.indigodomo.com/viewtopic.php?f=108&t=7559#p45965, but obviously, I'm off target somewhere.

FYI, the states are all just blank text strings and I don't need a default value for these.

Any pointers in the right direction would be much appreciated.

Bill
My Plugin: My People

Posted on
Thu Jan 21, 2021 9:08 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Adding new states to a device plugin

It looks like you are iterating over all the devices in the deviceStartComm, which is called for each device... you only need to call it for the device provided in that routine. That is:
Code: Select all
 def deviceStartComm(self, dev)
         dev.stateListOrDisplayStateIdChanged()

For what it's worth, to avoid forcing it to reload every time, I have a routine that checks to see if it is required (you can ignore the initialization of the communication, that is specific to my plugin framework):
Code: Select all
... in the init I initialize a list of states/properties that have changed on the device since creation ...
self.upgradedDeviceStates = list()
self.upgradedDeviceStates.append(u'activeTunerChannel')
self.upgradedDeviceProperties = list()      
self.upgradedDeviceProperties.append((u'updateInterval', '10'))

   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   # This call will be made from the plugin in order to start the communications with the
   # hardware device... this will spin up the concurrent processing thread.
   #-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
   def initiateCommunications(self, initializeConnect=True):
      # determine if this device is missing any properties that were added
      # during device/plugin upgrades
      propertiesDictUpdateRequired = False
      pluginPropsCopy = self.indigoDevice.pluginProps
      for newPropertyDefn in self.upgradedDeviceProperties:
         if not (newPropertyDefn[0] in pluginPropsCopy):
            self.hostPlugin.logger.info(u'Triggering property update due to missing device property: {0}'.format(RPFrameworkUtils.to_unicode(newPropertyDefn[0])))
            pluginPropsCopy[newPropertyDefn[0]] = newPropertyDefn[1]
            propertiesDictUpdateRequired = True
            
            # safeguard in case the device doesn't get updated...
            self.indigoDevice.pluginProps[newPropertyDefn[0]] = newPropertyDefn[1]
      if propertiesDictUpdateRequired == True:
         self.indigoDevice.replacePluginPropsOnServer(pluginPropsCopy)
   
      # determine if this device is missing any states that were defined in upgrades
      stateReloadRequired = False
      for newStateName in self.upgradedDeviceStates:
         if not (newStateName in self.indigoDevice.states):
            self.hostPlugin.logger.info(u'Triggering state reload due to missing device state: {0}'.format(RPFrameworkUtils.to_unicode(newStateName)))
            stateReloadRequired = True   
      if stateReloadRequired == True:
         self.indigoDevice.stateListOrDisplayStateIdChanged()
      
      # start concurrent processing thread by injecting a placeholder
      # command to the queue
      if initializeConnect == True:
         self.queueDeviceCommand(RPFrameworkCommand.RPFrameworkCommand(RPFrameworkCommand.CMD_INITIALIZE_CONNECTION))

Adam

Posted on
Fri Jan 22, 2021 10:35 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Adding new states to a device plugin

Thank you Adam.... but I'm still stuck....
When I try to do it using the first bit
RogueProeliator wrote:
Code: Select all
 def deviceStartComm(self, dev)
         dev.stateListOrDisplayStateIdChanged()

I get
Code: Select all
Starting plugin "My People 1.2.66" (pid 35825)
   My People Error                 Error in plugin execution InitializeMain:

Traceback (most recent call last):
SyntaxError: ('invalid syntax', ('plugin.py', 42, 35, '\tdef deviceStartComm(self, dev)\t\t\t\n'))


If I try to do it with the other method (The longer version you posted that checks stuff)... I either get
Code: Select all
Traceback (most recent call last):
SyntaxError: ('invalid syntax', ('plugin.py', 42, 2, '\t... in the init I initialize a list of states/properties that have changed on the device since creation ...\n'))
or
Code: Select all
Traceback (most recent call last):
IndentationError: ('unindent does not match any outer indentation level', ('plugin.py', 46, 61, '   def initiateCommunications(self, initializeConnect=True):\n'))
I'm guessing I'm putting it in the right location? I looked at the contents of your Roku and Plex plugin to find where you put it... I didn't see it. (I didn't want to ask for help again without doing a little more research first.) FYI, "Research" is a fancy synonym for "I have no idea what I'm doing"
Attachments
Screen Shot 2021-01-22 at 10.24.24 AM.png
Screen Shot 2021-01-22 at 10.24.24 AM.png (238.03 KiB) Viewed 1183 times

Bill
My Plugin: My People

Posted on
Fri Jan 22, 2021 12:51 pm
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Adding new states to a device plugin

SyntaxError: ('invalid syntax', ('plugin.py', 42, 35, '\tdef deviceStartComm(self, dev)\t\t\t\n'))

You are missing the colon in the function definition... probably my fault as I can cut out from my copy and went one character too far:

Code: Select all
def deviceStartComm(self, dev):
         dev.stateListOrDisplayStateIdChanged()

Give that simple one a try and we can go from there if needed...

I'm guessing I'm putting it in the right location? I looked at the contents of your Roku and Plex plugin to find where you put it... I didn't see it. (I didn't want to ask for help again without doing a little more research first.) FYI, "Research" is a fancy synonym for "I have no idea what I'm doing"

Yeah, code is in the reusable framework portion (RPFramework directory inside the plugin) so that I don't have to re-code it for each plugin. Great for a developer comfortable with that stuff, not so great from someone trying to learn and pick it up. :-)

Adam

Posted on
Fri Jan 22, 2021 6:26 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: Adding new states to a device plugin

RogueProeliator wrote:
Give that simple one a try and we can go from there if needed...

That worked, so a new version of my plugin should be out soon. Your Roku plugin was updated today.... I'm now taking your Plugin Developer Documenter plugin out for a spin. At what point would it be easier for me to just ship you my indigo machine and let you put stuff on it directly? :lol:

Bill
My Plugin: My People

Posted on
Sat Jan 23, 2021 11:09 am
RogueProeliator offline
User avatar
Posts: 2501
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Adding new states to a device plugin

That worked, so a new version of my plugin should be out soon. Your Roku plugin was updated today.... I'm now taking your Plugin Developer Documenter plugin out for a spin. At what point would it be easier for me to just ship you my indigo machine and let you put stuff on it directly?

LOL, at the point where you have added about 6 hours to my day (I am in need of this already...) :-)

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests