How to trigger an event when a property value is changed?

(Simon) #1

I have a simple web thing written in python with an ‘on’ property -

def make_thing():
thing = Thing('My Lamp', ['OnOffSwitch', 'Light'], 'A web connected lamp')

                 '@type': 'OnOffProperty',
                 'title': 'On/Off',
                 'type': 'boolean',
                 'description': 'Whether the lamp is turned on',

Is it possible to attach an event to this on property so another piece of code can be run when the user changes the sate of My Lamp?

(Martin Giger) #2

The web thing API packages are designed to be primarily used for writing out, since you should know more about when exactly these state transitions happen. So you’d do it there.

If you’re talking about doing something that involves multiple devices, you should check out rules in the gateway, where you can define a condition and actions.

(Michael Stegeman) #3

You can pass a function to the Value() constructor which will be called when the value is set. From there, you could probably trigger an event.

(Simon) #4

Thanks for replying. I’ve got this so far, all I want is to see the debug output ‘get’ / ‘set’ to be displayed but this isn’t happening. I’m new to this sorry, where am I going wrong?

Many thanks.

class LightThing(Thing):

def __init__(self):
        'Light Thing',
        ['OnOffSwitch', 'Light'],
        'A web connected light thing')

    self.state = False
                 Value(self.get_state(), self.set_state()),
                     '@type': 'OnOffProperty',
                     'title': 'On/Off',
                     'type': 'boolean',
                     'description': 'Whether the lamp is turned on'

def get_state(self):
    return True
def set_state(self):

(Martin Giger) #5

You probably only want Value(self.get_state(), self.set_state) so you pass the function and not the return value of the function.

(Simon) #6

Thanks Martin, that did it!
I see how the brackets work now.