mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 09:17:46 +01:00
mqtt_client: Added MQTTClientComponent::unsubscribe() (#1672)
Co-authored-by: Otto Winter <otto@otto-winter.com>
This commit is contained in:
parent
bf01c22e1f
commit
76a6c39f25
2 changed files with 29 additions and 0 deletions
|
@ -347,6 +347,26 @@ void MQTTClientComponent::subscribe_json(const std::string &topic, mqtt_json_cal
|
||||||
this->subscriptions_.push_back(subscription);
|
this->subscriptions_.push_back(subscription);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MQTTClientComponent::unsubscribe(const std::string &topic) {
|
||||||
|
uint16_t ret = this->mqtt_client_.unsubscribe(topic.c_str());
|
||||||
|
yield();
|
||||||
|
if (ret != 0) {
|
||||||
|
ESP_LOGV(TAG, "unsubscribe(topic='%s')", topic.c_str());
|
||||||
|
} else {
|
||||||
|
delay(5);
|
||||||
|
ESP_LOGV(TAG, "Unsubscribe failed for topic='%s'.", topic.c_str());
|
||||||
|
this->status_momentary_warning("unsubscribe", 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto it = subscriptions_.begin();
|
||||||
|
while (it != subscriptions_.end()) {
|
||||||
|
if (it->topic == topic)
|
||||||
|
it = subscriptions_.erase(it);
|
||||||
|
else
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Publish
|
// Publish
|
||||||
bool MQTTClientComponent::publish(const std::string &topic, const std::string &payload, uint8_t qos, bool retain) {
|
bool MQTTClientComponent::publish(const std::string &topic, const std::string &payload, uint8_t qos, bool retain) {
|
||||||
return this->publish(topic, payload.data(), payload.size(), qos, retain);
|
return this->publish(topic, payload.data(), payload.size(), qos, retain);
|
||||||
|
|
|
@ -159,6 +159,15 @@ class MQTTClientComponent : public Component {
|
||||||
*/
|
*/
|
||||||
void subscribe_json(const std::string &topic, mqtt_json_callback_t callback, uint8_t qos = 0);
|
void subscribe_json(const std::string &topic, mqtt_json_callback_t callback, uint8_t qos = 0);
|
||||||
|
|
||||||
|
/** Unsubscribe from an MQTT topic.
|
||||||
|
*
|
||||||
|
* If multiple existing subscriptions to the same topic exist, all of them will be removed.
|
||||||
|
*
|
||||||
|
* @param topic The topic to unsubscribe from.
|
||||||
|
* Must match the topic in the original subscribe or subscribe_json call exactly.
|
||||||
|
*/
|
||||||
|
void unsubscribe(const std::string &topic);
|
||||||
|
|
||||||
/** Publish a MQTTMessage
|
/** Publish a MQTTMessage
|
||||||
*
|
*
|
||||||
* @param message The message.
|
* @param message The message.
|
||||||
|
|
Loading…
Reference in a new issue