ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
mqtt_topic_match.h
Go to the documentation of this file.
1#pragma once
2
3namespace esphome::mqtt {
4
7inline bool topic_match(const char *message, const char *subscription) {
8 // First-level wildcards do not match topics that begin with '$'
9 bool wildcards = *message != '$';
10 while (*message != '\0' && *subscription != '\0') {
11 if (wildcards && *subscription == '#')
12 return true;
13 if (wildcards && *subscription == '+') {
14 subscription++;
15 while (*message != '\0' && *message != '/')
16 message++;
17 continue;
18 }
19 if (*message != *subscription)
20 return false;
21 wildcards |= *subscription == '/';
22 message++;
23 subscription++;
24 }
25 return *message == *subscription; // a match only if both ended
26}
27
28} // namespace esphome::mqtt
const LogString * message
Definition component.cpp:35
bool topic_match(const char *message, const char *subscription)
True if the message topic matches the subscription, with + and # wildcards.