Skip to content

Commit

Permalink
Zigbee2mqtt: remove dynamic brightness to reduce communication load
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Jan 2, 2025
1 parent 2504bca commit cf9bae8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
9 changes: 2 additions & 7 deletions include/led-drivers/net/DriverNetZigbee2mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@
#include <list>
#include <atomic>
#include <chrono>
#include <mutex>
#endif

#include <condition_variable>

#include <led-drivers/LedDevice.h>
#include <linalg.h>

Expand All @@ -24,7 +21,7 @@ class DriverNetZigbee2mqtt : public LedDevice
struct Zigbee2mqttInstance
{
int transition;
int constantBrightness;
int constantBrightness = -1;

std::list<Zigbee2mqttLamp> lamps;
};
Expand All @@ -35,6 +32,7 @@ class DriverNetZigbee2mqtt : public LedDevice

QString name;
Mode colorModel;
int currentBrightness;
};

public:
Expand Down Expand Up @@ -63,9 +61,6 @@ public slots:
QString _discoveryMessage;
int _mqttId;

std::mutex _mtx;
std::condition_variable _cv;

static int mqttId;
static bool isRegistered;
};
18 changes: 12 additions & 6 deletions sources/led-drivers/net/DriverNetZigbee2mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -117,7 +121,7 @@ int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& 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;
Expand Down Expand Up @@ -148,12 +152,14 @@ int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& ledValues)
brightness = std::min(std::max(static_cast<int>(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));
Expand Down
4 changes: 2 additions & 2 deletions sources/led-drivers/schemas/schema-zigbee2mqtt.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cf9bae8

Please sign in to comment.