From 452921d19885647067bcc0ee0fddf4a5b9c43574 Mon Sep 17 00:00:00 2001 From: proddy Date: Wed, 3 Nov 2021 22:12:21 +0100 Subject: [PATCH] MQTT subscribe to devices - Refactor MQTT subscriptions and API calls #173 --- src/emsesp.cpp | 11 ++++++----- src/system.cpp | 4 ++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 0574f2bd1..c3ce96e97 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -985,10 +985,10 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, std:: auto flags = device_p->flags; // empty reply to version, read a generic device from database - if (product_id == 0) { + if (product_id == 0) { // check for known device IDs if (device_id == 0x40) { - name = "rf room temperature sensor"; + name = "rf room temperature sensor"; } else if (device_id == 0x17) { name = "generic thermostat"; device_type = DeviceType::THERMOSTAT; @@ -1006,7 +1006,7 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, std:: name = "modem"; device_type = DeviceType::CONNECT; } else if (device_id == 0x0E) { - name = "converter"; + name = "converter"; } else { return false; } @@ -1049,9 +1049,10 @@ bool EMSESP::add_device(const uint8_t device_id, const uint8_t product_id, std:: [device_type](const char * value, const int8_t id, JsonObject & output) { return command_entities(device_type, output, id); }, F_(entities_cmd)); - // MQTT subscribe to the device top-level, e.g. "ems-esp/boiler" and subs + // MQTT subscribe to the device + Mqtt::subscribe(device_type, EMSdevice::device_type_2_device_name(device_type), nullptr); // e.g. "ems-esp/boiler" std::string topic = EMSdevice::device_type_2_device_name(device_type) + "/#"; - Mqtt::subscribe(device_type, topic, nullptr); // use empty function callback + Mqtt::subscribe(device_type, topic, nullptr); // e.g. "ems-esp/boiler/#" // Print to LOG showing we've added a new device LOG_INFO(F("Recognized new %s with device ID 0x%02X"), EMSdevice::device_type_2_device_name(device_type).c_str(), device_id); diff --git a/src/system.cpp b/src/system.cpp index 9d3a0a69e..5d0ac1c5e 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -699,9 +699,9 @@ void System::commands_init() { Command::add(EMSdevice::DeviceType::SYSTEM, F("test"), System::command_test, F("run tests")); #endif - // MQTT subscribe "ems-esp/system/#" - // There is no need to subscribe only to 'ems-esp/system' as the commands send and pin require the fullpath for security + // MQTT subscribe "ems-esp/system/#" and "ems-esp/system" Mqtt::subscribe(EMSdevice::DeviceType::SYSTEM, "system/#", nullptr); // use empty function callback + Mqtt::subscribe(EMSdevice::DeviceType::SYSTEM, "system", nullptr); // use empty function callback } // flashes the LED