Skip to content
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

@app.on_topic decorator not working with special characters (* is the one I came across) #226

Open
Daenara opened this issue Jul 28, 2022 · 3 comments

Comments

@Daenara
Copy link
Contributor

Daenara commented Jul 28, 2022

Good Evening,

while trying to realize a small project I noticed some pretty strange behavior with the on_topic decorator. I need to receive non-hermes mqtt messages and after I tried to figure out what the best way is to do that, I thought I might try it that way first.

Here is what I tried:

@app.on_topic("weather/+/+/temperature_*C")
async def on_temperature_update(data: TopicData, payload: bytes):
    print(payload.decode('UTF-8'))

Does not work (HermesApp: Unexpected topic: weather/inside/wohnzimmer/temperature_*C)

@app.on_topic("weather/+/+/light_Lux")
async def on_temperature_update(data: TopicData, payload: bytes):
    print(payload.decode('UTF-8'))

Works

@app.on_topic("weather/outside/back/temperature_*C")
async def on_temperature_update(data: TopicData, payload: bytes):
    print(payload.decode('UTF-8'))

Works

Since it did work after I changed the temperature topic for the light one, I am pretty sure that it is the * in my topic that is the issue. According to the mqtt standard, only using + or # in a topic is not allowed, all other UTF-8 characters are fair game. Would it be possible to fix this?

I did some testing with escaping the * symbol when the regex is built and that got it to work, but I doubt my simple solution of replacing token + "$"with token.replace("*", "\*") + "$" in line 566 is a good solution. If this character causes problems when not escaped, others might too, so a more general approach of escaping characters might be the better solution for that.

Daenara

@Daenara Daenara changed the title app.on_topic decorator not working with special characters (* is the one I came across) @app.on_topic decorator not working with special characters (* is the one I came across) Jul 28, 2022
@koenvervloesem
Copy link
Member

If we're not sure which other characters might cause problems, your solution is fine for me for now. We can deal with other problems later. Can you create a pull request and document the reason for escaping in the code?

@Daenara
Copy link
Contributor Author

Daenara commented Jul 30, 2022

Document in the code or is there somewhere else you want it documented?

@koenvervloesem
Copy link
Member

You can document it on line 566.

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

No branches or pull requests

2 participants