openhab-scripters / openhab-helper-libraries

Scripts and modules for use with openHAB
Eclipse Public License 1.0
88 stars 70 forks source link

[triggers] Error creating trigger UID when new_state contains quotes #141

Closed tavalin closed 5 years ago

tavalin commented 5 years ago

Describe the bug

I've run into an issue whilst trying to convert some of my DSL rules into jython rules.

I am using the following rule decorator which when parsed causes an invalid rule:

@when("Item SONYReceiverSTRDN1080_Zone1_InputSource changed to \"netService:audio?service=spotify\"")

The error given is:

(INVALID_RULE): Validation of rule 144040b7-9cc8-4db4-95b0-6ad7fbadd717 has failed! Invalid module uid: Item-SONYReceiverSTRDN1080_Zone1_InputSource-changed-to-"netService:audio?service=spotify". It is null or not fit to the pattern: [A-Za-z0-9_-]*

It appears that the library is trying to convert the when clause into a trigger uid. As my when clause contains escaped quotes (which is valid) when converted into a trigger uid it violates the given regex pattern ([A-Za-z0-9_-]*) governing valid trigger uids.

Have looked through the code briefly (and not being a python expert) I think the issue is on this line or the following lines:

https://github.com/openhab-scripters/openhab-helper-libraries/blob/master/Core/automation/lib/python/core/triggers.py#L236

To Reproduce Steps to reproduce the behavior:

Create the following jython rule and place in the personal directory:

from core.rules import rule
from core.triggers import when

@rule("Change Sony AVR Sound Field (Spotify)", description="This is an example Item changed triggered rule using decorators", tags=["Exa$
@when("Item SONYReceiverSTRDN1080_Zone1_InputSource changed to \"netService:audio?service=spotify\"")
def hellowWorldItemUpdateDecorators(event):
    hellowWorldItemUpdateDecorators.log.info(event.itemName + " hello world" )

Expected behavior The library creates a rule with a valid trigger uid.

Environment (please complete the following information):

5iver commented 5 years ago

Have you tried without the escaped quotes? IIRC, everything after the 'changed to' is taken in as a string, so I don't think you need to escape them. Or are you saying the actual value has escaped quotes in it? I could see that being a problem.

tavalin commented 5 years ago

I've just tried it without quotes but still the same error, however this time it's due to the question mark in the string.

For reference, here is my rule as written in DSL:

rule "Audio Mode"
when
        Item SONYReceiverSTRDN1080_Zone1_InputSource changed to "netService:audio?service=spotify"
then
        SONYReceiverSTRDN1080_Master_SoundField.sendCommand("multiChStereo")
end
5iver commented 5 years ago

I'm seeing several issues when using StringItems. I never used them in my tests I did not fully test all the possibilities (spaces in new_state or old_state, invalid chars for the UID, etc.) and completely overlooked the implications. I'm wrapping up something else, but will look at this later tonight.