Action button to execute action in plugin.py?

Posted on
Thu Jan 28, 2021 10:23 pm
whmoorejr offline
User avatar
Posts: 522
Joined: Jan 15, 2013
Location: Houston, TX

Action button to execute action in plugin.py?

I'm writing an action that has a callbackmethod that works. When I execute the action, it does what I want it to do.
I've added a button to the action.xml with the goal of making it execute the action when the dialog box is open.
(So a user has the option of running the action while the action dialog is open, like when you hit the "Run" button in a script action.) Is that possible?

This is what this code does...
Gives the user a dialog to select the device created with the plugin from a menu. The first button populates the 2nd menu with all the different states and state values for the selected device. Then there is a textfield where the user can enter a new state value for the state they selected. I'd like the option of running the action within the dialog so the user could update a state, select another state, update, select another state, etc... versus save, execute all, click, edit, select another state, save, execute all, etc.

This is what I have so far...Action.xml:
Code: Select all
<!-- Experiment Actions -->   
   <Action id="setAnyState" uiPath="Combo Actions">
      <Name>Set Any State</Name>
      <CallbackMethod>setAnyState</CallbackMethod>
      <ConfigUI>
         <Field type="menu" id="setADeviceField" filter="self" devfaultValue="0" tooltip="">
            <Label>Room to modify:</Label>
            <List class="self" filter="self" method="filterDevices" dynamicReload="false"/>
         </Field>
         <Field id="buttonConfirmDevice" type="button" tooltip="">
            <Label>To Select Device:</Label><Title>CLICK</Title> <CallbackMethod>buttonConfirmDevice</CallbackMethod>
         </Field>   
         <Field type="menu" id="setAnyStateField" defaultValue="0" tooltip="">
            <Label>Select State:</Label>
            <List class="self" filter="new" method="filterDevStates" dynamicReload="true"/>
         </Field>
         <Field type="textfield" id="newStateValueField"><Label>Enter New Value:</Label></Field>
         <Field id="label" type="label" fontSize="small" fontColor="darkgray">
            <Label>You can insert text, a variable substitution %%v:VARIABLEID%% or device state %%d:DEVICEID:STATEKEY%%</Label>
         </Field>
         <Field id="buttonConfirmNewValue" type="button" tooltip="">
            <Label>To Make Change Now:</Label><Title>CLICK</Title> <CallbackMethod>buttonConfirmNewValue</CallbackMethod>
         </Field>   
      </ConfigUI>   
   </Action>
plugin.py:
Code: Select all
   def setAnyState(self, pluginAction, dev):
   
      requestedDev = str(pluginAction.props.get(u"setADeviceField"))
      requestedState = str(pluginAction.props.get(u"setAnyStateField"))
      substitutedValue = self.substitute(pluginAction.props.get("newStateValueField", ""))
      thisRoomDev = indigo.devices[int(requestedDev)]
      thisDevName = thisRoomDev.name
      thisRoomDev.updateStateOnServer(key=requestedState, value=substitutedValue)
      self.debugLog(thisDevName + " . " + requestedState + "has been changed to: " + substitutedValue)
   
      
   def filterDevices(self, filter, valuesDict, typeId, targetId):
      xList =[]
      for dev in indigo.devices.iter(filter="com.whmoorejr.my-rooms"):
         xList.append(( unicode(dev.id),dev.name))
      xList.append(("0","==== off, do not use ===="))
      return xList
      
   def buttonConfirmDevice(self, valuesDict, typeID, devId):
      return valuesDict

   def filterDevStates(self, filter, valuesDict, typeId, targetId):
      xList =[]
      if len(valuesDict) < 2:                         return [("0","")]
      if "setADeviceField" not in valuesDict:       return [("0","")]
      if valuesDict["setADeviceField"] in ["0",""]: return [("0","")]
      dev = indigo.devices[int(valuesDict["setADeviceField"])]
      for state in dev.states:
         xList.append((state,state+"   ; currentV: "+unicode(dev.states[state]) ))
      xList.append(("0","==== off, do not use ===="))
      return xList   
      self.debuglog("filterDevStates" + state + " and currentV:  "+unicode(dev.states[state])+ " for " +setADeviceField)
      
   def buttonConfirmNewValue(self, valuesDict, typeId, devId):
      self.debugLog("I Pushed the Confirm Button")
      #theRoomDev = indigo.devices[int(devId)]
      #theRoomName = theRoomDev.name
      self.debugLog("Check Variables: devId: " + str(devId) + " typeId: " + str(typeId))
      return valuesDict


Any pointers would be greatly appreciated.

Bill
My Plugin: My People

Posted on
Thu Jan 28, 2021 11:51 pm
whmoorejr offline
User avatar
Posts: 522
Joined: Jan 15, 2013
Location: Houston, TX

Re: Action button to execute action in plugin.py?

Figured it out... Mostly.....
Code: Select all
def buttonConfirmNewValue(self, valuesDict, typeId, devId):
      self.debugLog("I Pushed the Confirm Button")
      requestedDev = valuesDict["setADeviceField"]
      requestedState = valuesDict["setAnyStateField"]
      newValue = valuesDict["newStateValueField"]
      subNewValue = self.substitute(valuesDict["newStateValueField"])
      thisRoomDev = indigo.devices[int(requestedDev)]
      thisDevName = thisRoomDev.name
      self.debugLog("Check Variables: thisDevName: " + thisDevName + " requested state: " + requestedState + " requested Value: " + newValue)
      self.debugLog("subNewValue: " + subNewValue)
      return valuesDict

I should just have to add a updateStateOnServer line, and presto.... I guess. I'll mess with it more tomorrow, I'm going to bed.

Bill
My Plugin: My People

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests

cron