Store rolling codes under device mqtt_topic_prefix

This commit is contained in:
2024-05-10 20:56:23 +02:00
parent 5461ff8889
commit 04ea807105
+4 -3
View File
@@ -7,7 +7,6 @@ namespace {
static const uint8_t CMD_UP = 0x02; static const uint8_t CMD_UP = 0x02;
static const uint8_t CMD_DOWN = 0x04; static const uint8_t CMD_DOWN = 0x04;
static const uint8_t CMD_PROG = 0x08; static const uint8_t CMD_PROG = 0x08;
static const std::string ROLLING_CODE_PREFIX = "rolling_code/";
static const int RF_SYMBOL = 640; static const int RF_SYMBOL = 640;
} }
@@ -18,12 +17,13 @@ class SomfyRTS : public Component, public Cover {
this->rfPin = rfPin; this->rfPin = rfPin;
this->remoteId = remoteId; this->remoteId = remoteId;
this->mqtt = mqtt; this->mqtt = mqtt;
this->mqttTopicPrefix = mqtt->get_topic_prefix() + "/rolling_code/";
} }
void setup() override { void setup() override {
pinMode(rfPin, OUTPUT); pinMode(rfPin, OUTPUT);
digitalWrite(rfPin, LOW); digitalWrite(rfPin, LOW);
mqtt->subscribe(ROLLING_CODE_PREFIX + std::to_string(remoteId), mqtt->subscribe(mqttTopicPrefix + std::to_string(remoteId),
[=](const std::string &topic, const std::string &payload) { [=](const std::string &topic, const std::string &payload) {
if (rollingCode == 0) { if (rollingCode == 0) {
ESP_LOGI(TAG, "Received rolling code for remote #%d from MQTT: %s", ESP_LOGI(TAG, "Received rolling code for remote #%d from MQTT: %s",
@@ -58,6 +58,7 @@ class SomfyRTS : public Component, public Cover {
int remoteId; int remoteId;
int rollingCode = 0; int rollingCode = 0;
esphome::mqtt::MQTTClientComponent* mqtt; esphome::mqtt::MQTTClientComponent* mqtt;
std::string mqttTopicPrefix;
/* Wake up the blinds motor controller and send the command. /* Wake up the blinds motor controller and send the command.
* Overall, with repeats and syncs, takes about 510ms. * Overall, with repeats and syncs, takes about 510ms.
@@ -153,7 +154,7 @@ class SomfyRTS : public Component, public Cover {
// Publish the new rolling code at the end to ensure fast reaction time // Publish the new rolling code at the end to ensure fast reaction time
// when the component is called. This method may be synchronous and // when the component is called. This method may be synchronous and
// introduces an unpredictable delay. // introduces an unpredictable delay.
mqtt->publish(ROLLING_CODE_PREFIX + std::to_string(remoteId), mqtt->publish(mqttTopicPrefix + std::to_string(remoteId),
std::to_string(rollingCode), /*qos=*/1, /*retain=*/true); std::to_string(rollingCode), /*qos=*/1, /*retain=*/true);
} }
}; };