Skip to content

Commit

Permalink
HomeAssistant: 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 cf9bae8 commit 3628e7a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 20 deletions.
4 changes: 3 additions & 1 deletion include/led-drivers/net/DriverNetHomeAssistant.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DriverNetHomeAssistant : public LedDevice
QString homeAssistantHost;
QString longLivedAccessToken;
int transition;
int constantBrightness;
int constantBrightness = 255;
bool restoreOriginalState;

std::list<HomeAssistantLamp> lamps;
Expand All @@ -34,6 +34,7 @@ class DriverNetHomeAssistant : public LedDevice

QString name;
Mode colorModel;
int currentBrightness = 255;

struct
{
Expand Down Expand Up @@ -63,6 +64,7 @@ class DriverNetHomeAssistant : public LedDevice
HomeAssistantInstance _haInstance;

std::unique_ptr<ProviderRestApi> _restApi;
long long _lastUpdate;

static bool isRegistered;
};
5 changes: 3 additions & 2 deletions include/led-drivers/net/DriverNetZigbee2mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DriverNetZigbee2mqtt : public LedDevice
struct Zigbee2mqttInstance
{
int transition;
int constantBrightness = -1;
int constantBrightness = 255;

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

QString name;
Mode colorModel;
int currentBrightness;
int currentBrightness = 255;
};

public:
Expand Down Expand Up @@ -60,6 +60,7 @@ public slots:
int _timeLogger;
QString _discoveryMessage;
int _mqttId;
long long _lastUpdate;

static int mqttId;
static bool isRegistered;
Expand Down
26 changes: 19 additions & 7 deletions sources/led-drivers/net/DriverNetHomeAssistant.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#include <led-drivers/net/DriverNetHomeAssistant.h>
#include <utils/InternalClock.h>

#include <HyperhdrConfig.h>
#ifdef ENABLE_BONJOUR
#include <bonjour/DiscoveryWrapper.h>
#endif

DriverNetHomeAssistant::DriverNetHomeAssistant(const QJsonObject& deviceConfig)
: LedDevice(deviceConfig)
: LedDevice(deviceConfig),
_lastUpdate(0)
{
}

Expand All @@ -25,7 +27,7 @@ bool DriverNetHomeAssistant::init(const QJsonObject& deviceConfig)
_haInstance.homeAssistantHost = deviceConfig["homeAssistantHost"].toString();
_haInstance.longLivedAccessToken = deviceConfig["longLivedAccessToken"].toString();
_haInstance.transition = deviceConfig["transition"].toInt(0);
_haInstance.constantBrightness = deviceConfig["constantBrightness"].toInt(0);
_haInstance.constantBrightness = deviceConfig["constantBrightness"].toInt(255);
_haInstance.restoreOriginalState = deviceConfig["restoreOriginalState"].toBool(false);
_maxRetry = deviceConfig["maxRetry"].toInt(60);

Expand Down Expand Up @@ -96,6 +98,7 @@ bool DriverNetHomeAssistant::powerOn()
if (!saveStates())
return false;
}
_lastUpdate = InternalClock::now() - 9000;
return powerOnOff(true);
}

Expand All @@ -112,9 +115,11 @@ bool DriverNetHomeAssistant::powerOff()
int DriverNetHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
{
QJsonDocument doc;
auto start = InternalClock::now();
auto lastUpdate = _lastUpdate;

auto rgb = ledValues.begin();
for (const auto& lamp : _haInstance.lamps)
for (auto& lamp : _haInstance.lamps)
if (rgb != ledValues.end())
{
QJsonObject row;
Expand Down Expand Up @@ -142,12 +147,19 @@ int DriverNetHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
brightness = std::min(std::max(static_cast<int>(std::roundl(v * 255.0)), 0), 255);
}

if (brightness > 0 && _haInstance.constantBrightness > 0)
if (lamp.currentBrightness <= 0 && brightness > 0)
{
brightness = _haInstance.constantBrightness;
row["brightness"] = lamp.currentBrightness = _haInstance.constantBrightness;
}
else if (lamp.currentBrightness > 0 && brightness == 0)
{
row["brightness"] = lamp.currentBrightness = 0;
}
else if (start - lastUpdate >= 10000)
{
_lastUpdate = start;
row["brightness"] = lamp.currentBrightness;
}

row["brightness"] = brightness;

doc.setObject(row);
QString message(doc.toJson(QJsonDocument::Compact));
Expand Down
19 changes: 11 additions & 8 deletions sources/led-drivers/net/DriverNetZigbee2mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ DriverNetZigbee2mqtt::DriverNetZigbee2mqtt(const QJsonObject& deviceConfig)
: LedDevice(deviceConfig),
_discoveryFinished(false),
_timeLogger(0),
_mqttId(mqttId++)
_mqttId(mqttId++),
_lastUpdate(0)
{
}

Expand All @@ -27,13 +28,8 @@ 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(1);
_zigInstance.constantBrightness = deviceConfig["constantBrightness"].toInt(255);

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 @@ -101,6 +97,7 @@ bool DriverNetZigbee2mqtt::powerOnOff(bool isOn)
}

_timeLogger = 0;
_lastUpdate = InternalClock::now() - 9000;

return true;
}
Expand All @@ -118,7 +115,8 @@ bool DriverNetZigbee2mqtt::powerOff()
int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& ledValues)
{
QJsonDocument doc;
auto start = InternalClock::nowPrecise();
auto start = InternalClock::now();
auto lastUpdate = _lastUpdate;

auto rgb = ledValues.begin();
for (auto& lamp : _zigInstance.lamps)
Expand Down Expand Up @@ -160,6 +158,11 @@ int DriverNetZigbee2mqtt::write(const std::vector<ColorRgb>& ledValues)
{
row["brightness"] = lamp.currentBrightness = 0;
}
else if (start - lastUpdate >= 10000)
{
_lastUpdate = start;
row["brightness"] = lamp.currentBrightness;
}

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-home_assistant.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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 3628e7a

Please sign in to comment.