Python Webthing - passing values to perform_action

learning

(Kevin D) #1

So, I have an action that I want to run depending on the value of an on-off switch. How can I read whether it was turned on or off?

def perform_action(self):
    if self.input == 1:
        Actions to perform when the switch is 'on'
    elif self.input == 0:
        Actions to perform when the switch is 'off'

self.input isn’t working. self.input[‘0’] gives me an error for index out of range. self.<action> errors, as well. What is the correct way to reference the switch state that triggered the action?


(Michael Stegeman) #2

Action input types are defined when creating the thing itself. I’ve included an example thing below with two actions, one which accepts an object input, and another which accepts a numeric input.

from webthing import Action, SingleThing, Thing, WebThingServer
import json
import uuid


class ObjectAction(Action):
    def __init__(self, thing, input_):
        Action.__init__(self,
                        uuid.uuid4().hex,
                        thing,
                        'objectAction',
                        input_=input_)

    def perform_action(self):
        print('ObjectAction triggered: input={}'
              .format(json.dumps(self.input)))


class ValueAction(Action):
    def __init__(self, thing, input_):
        Action.__init__(self,
                        uuid.uuid4().hex,
                        thing,
                        'valueAction',
                        input_=input_)

    def perform_action(self):
        print('ValueAction triggered: input={}'.format(self.input))


def make_thing():
    thing = Thing('My Thing', [], 'A thing')

    thing.add_available_action(
        'objectAction',
        {
            'input': {
                'type': 'object',
                'required': [
                    'value1',
                    'value2',
                ],
                'properties': {
                    'value1': {
                        'type': 'integer',
                        'minimum': 0,
                        'maximum': 100,
                    },
                    'value2': {
                        'type': 'boolean',
                    },
                },
            },
        },
        ObjectAction)

    thing.add_available_action(
        'valueAction',
        {
            'input': {
                'type': 'number',
                'minimum': -50,
                'maximum': 50,
            },
        },
        ValueAction)

    return thing


def run_server():
    thing = make_thing()

    server = WebThingServer(SingleThing(thing), port=8888)
    try:
        server.start()
    except KeyboardInterrupt:
        server.stop()


if __name__ == '__main__':
    run_server()

To trigger objectAction:

curl \
    -X POST \
    --data '{"objectAction": {"input": {"value1": 25, "value2": false}}}' \
    http://localhost:8888/actions

To trigger valueAction:

curl \
    -X POST \
    --data '{"valueAction": {"input": 10}}' \
    http://localhost:8888/actions