From cf9bae8cad585ae42b16191123de809bc57743f7 Mon Sep 17 00:00:00 2001 From: Awawa <69086569+awawa-dev@users.noreply.github.com> Date: Thu, 2 Jan 2025 02:02:22 +0100 Subject: [PATCH] Zigbee2mqtt: remove dynamic brightness to reduce communication load --- include/led-drivers/net/DriverNetZigbee2mqtt.h | 9 ++------- .../led-drivers/net/DriverNetZigbee2mqtt.cpp | 18 ++++++++++++------ .../schemas/schema-zigbee2mqtt.json | 4 ++-- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/include/led-drivers/net/DriverNetZigbee2mqtt.h b/include/led-drivers/net/DriverNetZigbee2mqtt.h index b2db5467..49af9700 100644 --- a/include/led-drivers/net/DriverNetZigbee2mqtt.h +++ b/include/led-drivers/net/DriverNetZigbee2mqtt.h @@ -7,11 +7,8 @@ #include #include #include - #include #endif -#include - #include #include @@ -24,7 +21,7 @@ class DriverNetZigbee2mqtt : public LedDevice struct Zigbee2mqttInstance { int transition; - int constantBrightness; + int constantBrightness = -1; std::list lamps; }; @@ -35,6 +32,7 @@ class DriverNetZigbee2mqtt : public LedDevice QString name; Mode colorModel; + int currentBrightness; }; public: @@ -63,9 +61,6 @@ public slots: QString _discoveryMessage; int _mqttId; - std::mutex _mtx; - std::condition_variable _cv; - static int mqttId; static bool isRegistered; }; diff --git a/sources/led-drivers/net/DriverNetZigbee2mqtt.cpp b/sources/led-drivers/net/DriverNetZigbee2mqtt.cpp index eacb1b42..d870bbe5 100644 --- a/sources/led-drivers/net/DriverNetZigbee2mqtt.cpp +++ b/sources/led-drivers/net/DriverNetZigbee2mqtt.cpp @@ -27,9 +27,13 @@ bool DriverNetZigbee2mqtt::init(const QJsonObject& deviceConfig) if (LedDevice::init(deviceConfig)) { + if (_zigInstance.constantBrightness < 0) + { + QThread::msleep(750); + } _zigInstance.transition = deviceConfig["transition"].toInt(0); - _zigInstance.constantBrightness = deviceConfig["constantBrightness"].toInt(0); + _zigInstance.constantBrightness = deviceConfig["constantBrightness"].toInt(1); Debug(_log, "Transition (ms) : %s", (_zigInstance.transition > 0) ? QSTRING_CSTR(QString::number(_zigInstance.transition)) : "disabled" ); Debug(_log, "ConstantBrightness : %s", (_zigInstance.constantBrightness > 0) ? QSTRING_CSTR(QString::number(_zigInstance.constantBrightness)) : "disabled"); @@ -117,7 +121,7 @@ int DriverNetZigbee2mqtt::write(const std::vector& ledValues) auto start = InternalClock::nowPrecise(); auto rgb = ledValues.begin(); - for (const auto& lamp : _zigInstance.lamps) + for (auto& lamp : _zigInstance.lamps) if (rgb != ledValues.end()) { QJsonObject row; @@ -148,12 +152,14 @@ int DriverNetZigbee2mqtt::write(const std::vector& ledValues) brightness = std::min(std::max(static_cast(std::roundl(v * 255.0)), 0), 255); } - if (brightness > 0 && _zigInstance.constantBrightness > 0) + if (lamp.currentBrightness <= 0 && brightness > 0) { - brightness = _zigInstance.constantBrightness; + row["brightness"] = lamp.currentBrightness = _zigInstance.constantBrightness; + } + else if (lamp.currentBrightness > 0 && brightness == 0) + { + row["brightness"] = lamp.currentBrightness = 0; } - - row["brightness"] = brightness; doc.setObject(row); emit GlobalSignals::getInstance()->SignalMqttPublish(topic, doc.toJson(QJsonDocument::Compact)); diff --git a/sources/led-drivers/schemas/schema-zigbee2mqtt.json b/sources/led-drivers/schemas/schema-zigbee2mqtt.json index 9841edfe..022c23e8 100644 --- a/sources/led-drivers/schemas/schema-zigbee2mqtt.json +++ b/sources/led-drivers/schemas/schema-zigbee2mqtt.json @@ -15,8 +15,8 @@ "constantBrightness": { "type": "integer", "title": "edt_dev_spec_constantBrightness_title", - "default": 0, - "minimum": 0, + "default": 255, + "minimum": 1, "maximum": 255, "required": true, "propertyOrder": 4