Skip to content

add @mqtt_trigger #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 2, 2020
Merged

Conversation

dlashua
Copy link
Contributor

@dlashua dlashua commented Dec 1, 2020

This is a quick first attempt. The simplest of test scripts seems to work:

@time_trigger('startup')
def mqtt_counter():
    task.unique('mqtt_counter')
    cnt = 0
    while True:
        cnt = cnt + 1
        log.info(cnt)
        mqtt.publish(topic='pyscript/test', payload=cnt)
        mqtt.publish(topic='pyscript/testx10', payload=(cnt * 10))
        task.sleep(1)

@mqtt_trigger('pyscript/test')
def mqtt_recv(**data):
    log.info(f'got one {data}')

@mqtt_trigger('pyscript/test', 'topic == "notthis"')
def mqtt_never(**data):
    log.info(f'this should never happen {data}')

@mqtt_trigger('pyscript/test', 'int(payload) % 2 == 0')
def mqtt_even(**data):
    log.info(f'EVEN {data}')

@mqtt_trigger('pyscript/#')
def mqtt_wildcard(**data):
    log.info(f'got one from wildcard {data}')

@mqtt_trigger('pyscript/test', 'payload == "5"')
def mqtt_5_then_10():
    log.info('got 5, waiting for 10')
    task.wait_until(mqtt_trigger=['pyscript/test', 'payload == "10"'])
    log.info('got 10')

Tests are needed. Though... I'm not sure how to write tests without an mqtt server to test against?

Docs are needed.

Feel free to make edits directly in my repo even if you are unable to test them.

Will fix #98 .

@craigbarratt
Copy link
Member

I reviewed this and it looks great! I don't have any comments. Really nice work.

The next two parts are docs and tests. I realize the tests are not easy to understand, so I can take a crack at that if you want.

@dlashua
Copy link
Contributor Author

dlashua commented Dec 2, 2020

@craigbarratt absolutely! I can handle docs if you can work out whatever the tests for this might look like.

@dlashua
Copy link
Contributor Author

dlashua commented Dec 2, 2020

Docs have been added.

As it is now, payload_json is only set in func_args if the payload is valid JSON. Otherwise, it doesn't exist. Could that cause problems? Should we set it to None instead when it is unparsable?

@craigbarratt
Copy link
Member

Looks great.

I think the payload_json name is confusing. To me it sounds like that's the string json form, not the deserialized object. How about payload_obj? (Names like payload_from_json or payload_deserialized are too long for my liking.)

I'm ok with your current approach of it being undefined if payload isn't a valid json string.

@craigbarratt craigbarratt changed the title WIP: mqtt_trigger add @mqtt_trigger Dec 2, 2020
@dlashua
Copy link
Contributor Author

dlashua commented Dec 2, 2020

Yes. payload_obj is closer. It's a weird thing. For instance, let's say the payload is "2".

A payload will always be a string. So payload = "2". However, payload_json = 2... the integer. But, in most cases, it will be an object, so, that's better than what we have. I'll make that change until we can come up with something better.

@craigbarratt craigbarratt merged commit cfbada0 into custom-components:master Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: MQTT trigger
2 participants