I am working with TP-Link Kasa dimmer devices. What I want to do is distinguish changes to a dimmer's on/off state that are initiated by Indigo from changes initiated by the switch itself. So I added an additional state to the device called buttonState (I actually created buttonState and buttonStateA as a way of further troubleshooting and will use in my examples below).
- Code: Select all
buttonPress = self.tpLink_self.getDeviceStateDictForNumberType("buttonPress", "buttonPress", "buttonPress")
statesDict.append(buttonPress)
buttonPressA = self.tpLink_self.getDeviceStateDictForNumberType("buttonPressA", "buttonPressA", "buttonPressA")
statesDict.append(buttonPressA)
The plugin runs a polling loop for each device which starts with buttonPress=0 and ButtonPressA=0. The polling loop looks for instances of changes to on/off state(if devState != lastState), and when it finds one, updates the devices states on the server. As the plugin works now, buttonPress is set to 1 if the dimmer goes on and buttonPress is set to 2 if the dimmer goes off. I have tried to add the additional condition that if buttonPressA is less than 0, then do not change buttonPress to 1 or 2 but skip that step.
I also have an action defined where Indigo is told to change the dimmer status (on/off/toggle), and it changes buttonPressA to -1 when that happens. On/off changes initiated by pressing the button on the dimmer itself (and not by Indigo) do not change the value of state buttonPressA (so it should remain 0). So when the action is called, this code is initiated:
- Code: Select all
state_update_list = [
{'key':'buttonPressA, 'value':-1},
]
dev.updateStatesOnServer(state_update_list)
dev.stateListOrDisplayStateIdChanged()
indigo.server.log("state:"+str(dev.states['buttonPressA'])) #### I can tell here that buttonPressA value is indeed -1
The goal is that device changes initiated by Indigo should have buttonPress=-1 and changes initiated by the device itself have buttonPress=1 or buttonPress =2. I do this because I have no way of actually triggering an action by a button press - the devices API does not sense button presses at the device - Indigo can only react to state changes.
Here is the problem: When buttonPressA becomes -1 (via the action), the polling loop still sees its value as 0, so it will treat changes to device on/off state the same whether initiated by dimmer or Indigo. The value gets updated to -1 only after the loop processs the state change:
- Code: Select all
if devState != lastState:
self.logger.info("second state:"+str(buttonPressA'])) ###### just so I can follow the value changes - "0" gets displayed instead of -1
if buttonPressA >-1:
if logState=="On":
indigo.server.log("button press on (dimmer)")
buttonPress=1
else:
indigo.server.log("button press off (dimmer)")
buttonPress=2
lastState = devState
After processing the device changes, I look at the Indigo main screen and under my device buttonPressA=-1 and buttonPress=1(or2). So the buttonPressA's value was updated from 0 to -1, but my look did not detect the change, even though it detected the on/off state change. From my log, I get an alert that buttonPressA is -1 ("state:-1") but then I get a second alert that it is 0 ("second state:0"). But when I look at the GUI of the device, it lists the state as -1.
Can anyone explain why the mis-timing? My goal is to have the "action" update something (I chose a device state but could be anything) that the polling loop will detect. Thanks in advance and I hope this question can be followed - I tried to be clear.