How to pass parameters into a subscription event function


(Johnathan Fullman) #1

I’m trying to pass a parameter into a subscription function so it works like this:

def initialize()
{
subscribe(MySwitch1, "switch", SwitchHandler("some param"))
subscribe(MySwitch2, "switch", SwitchHandler("some other param"))
}

def SwitchHandler(evt, my_custom_param)
{
}

Anyone know how to accomplish this? I thought about creating an intermediate (like below) handler but that seems tacky and not very scalable. Also doesn’t work for floating point values… just tiny integers and enums.

def initialize()
{
subscribe(MySwitch1, "switch", SwitchHandlerA)
subscribe(MySwitch2, "switch", SwitchHandlerB)
}

def SwitchHandlerA(evt)
{
SwitchHandler(evt, "some param")
}

def SwitchHandlerB(evt)
{
SwitchHandler(evt, "some other param")
}

def SwitchHandler(evt, my_custom_param)
{
}

(John S) #2

You can’t get there from here.

Subscribe just takes device/event/handler method, and handlermethod just gets called with the event.

What were you hoping to pass into the hander? And why wouldn’t your tacky version not work?


(Johnathan Fullman) #3

Thanks John. My tacky fix does work. I’m a software engineer by trade so I’m really just wondering if a more elegant solution exists.


(Brice; SmartRulesApp.com) #4

This bothered me too, but I haven’t been able to find a solution. Closures could fix this, but SmartThings seems to block closures for security purposes I think.


(John S) #5

[quote=“johnathan398, post:3, topic:17597”]
a more elegant solution exists.
[/quote]You could have the fan out fan in calls, and look up the method name of the calling method on the stack, and do different things in the fan-in method based on the caller. But that seems less clear than just doing it the other way.

What’s the purpose of the parameter? You can do a switch statement in the event handler based on the device name/id/type/event… if you just want a single handler