Actions

openhab2
binding
Tags: #<Tag:0x00007fe0663123f8> #<Tag:0x00007fe066311a20>

(Alex Krasno) #1

Hey, I’m quite new to OH development and I’m trying to create an action for my binding (similar to the publishMQTT from MQQT).
I implemented the following:

@ThingActionsScope(name = "mybinding")
@NonNullByDefault
public class MyAction implements ThingActions {

    private @Nullable MyHandler handler;

    @RuleAction(label = "@text/actionLabel", description = "@text/actionDesc")
    public void sendMessage(
            @ActionInput(name = "text", label = "@text/actionLabel", description = "@text/actionDesc") @Nullable String text) {
        System.out.println("successful");
    }

    @Override
    public void setThingHandler(@Nullable ThingHandler handler) {
        this.handler = (MyHandler) handler;

    }

    @Override
    public @Nullable ThingHandler getThingHandler() {
        return handler;
    }
}

And my rule:

rule "Test rule"
when
    Item TestSwitch received command
then
   val actions = getActions("mybinding","mybinding:sub:98655a49")
    if(null === actions) {
            logInfo("actions", "Actions not found, check thing ID")
            return
     }
     logInfo("actions", " " + actions)
     actions.sendMessage("TestTest")
end

Actually, the last logInfo returns me the correct type org.openhab.binding.telegram.bot.MyAction@aa871b8, but calling the “sendMessage” returns Rule 'Test rule': 'sendMessage' is not a member of 'org.eclipse.smarthome.core.thing.binding.ThingActions'; line 13, column 6, length 40

This is very strange, it first recognizes the correct type and then it tries to find a method on the super type. What am I doing wrong here?


New Telegram Binding and updated Action - Tester and Feedback welcome
(David Graeff) #2

Your service is registered in the thing handler via Collection<Class> getServices() is it?


(Alex Krasno) #3

Thanks for having a look at this! Yes it’s implemented:

    @Override
    public Collection<Class<? extends ThingHandlerService>> getServices() {
        return Collections.singleton(MyAction.class);
    }

And in the debugger I can also see that it is called.


(Hilbrand Bouwkamp) #4

To use it in a DSL rule you need to add a static method:

    public static void sendMessage(@Nullable ThingActions actions, @Nullable String text) {
        if (actions instanceof MyAction) {
            ((MyAction) actions).sendMessage(text);
        } else {
            throw new IllegalArgumentException("Instance is not an MyAction class.");
        }
    }

[SOLVED] Trying to implement ThingActions into binding
(David Graeff) #5

Yup. Like in the MQTT binding as reference.

Cheers, David


(Alex Krasno) #6

Thanks, that worked immediately!

I looked at the MQQT example already however I didn’t care much about this static method as it had no special annotations and thought this is some kind of additional method not related to the ESH/OH framework.

I also read https://github.com/eclipse/smarthome/blob/master/bundles/automation/org.eclipse.smarthome.automation.module.core/README.md but this static method wasn’t mentioned there either or is there any other documentation?

But thanks again for your help!


(David Graeff) #7

The possibility to use new rule actions in old DSL rules was added hastily just before the OH 2.4 release.
Documentation was of least concern at that point. But now that the documentation is slowly migrated back to openHAB from Eclipse Smarthome, a paragraph should be added. We should maybe comment the method in MQTT as well.