1414
1515// Constants
1616namespace {
17- const bool verbose = false ;
18-
19- // Configuration settings
20- const char CONFIG_HOST[] = " host" ;
21- const char CONFIG_PORT[] = " port" ;
22- const char CONFIG_AUTH_TOKEN[] = " token" ;
23- const char CONFIG_ENITYIDS[] = " entityIds" ;
24- const char CONFIG_BRIGHTNESS[] = " brightness" ;
25- const char CONFIG_BRIGHTNESS_OVERWRITE[] = " overwriteBrightness" ;
26- const char CONFIG_FULL_BRIGHTNESS_AT_START[] = " fullBrightnessAtStart" ;
27- const char CONFIG_ON_OFF_BLACK[] = " switchOffOnBlack" ;
28- const char CONFIG_TRANSITIONTIME[] = " transitionTime" ;
29-
30- const bool DEFAULT_IS_BRIGHTNESS_OVERWRITE = true ;
31- const bool DEFAULT_IS_FULL_BRIGHTNESS_AT_START = true ;
32- const int BRI_MAX = 255 ;
33- const bool DEFAULT_IS_SWITCH_OFF_ON_BLACK = false ;
34-
35- // Home Assistant API
36- const int API_DEFAULT_PORT = 8123 ;
37- const char API_BASE_PATH[] = " /api/" ;
38- const char API_STATES[] = " states" ;
39- const char API_LIGHT_TURN_ON[] = " services/light/turn_on" ;
40- const char API_LIGHT_TURN_OFF[] = " services/light/turn_off" ;
41-
42- const char ENTITY_ID[] = " entity_id" ;
43- const char RGB_COLOR[] = " rgb_color" ;
44- const char BRIGHTNESS[] = " brightness" ;
45- const char TRANSITION[] = " transition" ;
46- const char FLASH[] = " flash" ;
47-
48- // // Home Assistant ssdp services
49- const char SSDP_ID[] = " ssdp:all" ;
50- const char SSDP_FILTER_HEADER[] = " ST" ;
51- const char SSDP_FILTER[] = " (.*)home-assistant.io(.*)" ;
17+ const bool verbose = false ;
18+
19+ // Configuration settings
20+ const char CONFIG_HOST[] = " host" ;
21+ const char CONFIG_PORT[] = " port" ;
22+ const char CONFIG_AUTH_TOKEN[] = " token" ;
23+ const char CONFIG_ENITYIDS[] = " entityIds" ;
24+ const char CONFIG_BRIGHTNESS[] = " brightness" ;
25+ const char CONFIG_BRIGHTNESS_OVERWRITE[] = " overwriteBrightness" ;
26+ const char CONFIG_FULL_BRIGHTNESS_AT_START[] = " fullBrightnessAtStart" ;
27+ const char CONFIG_TRANSITIONTIME[] = " transitionTime" ;
28+
29+ const bool DEFAULT_IS_BRIGHTNESS_OVERWRITE = true ;
30+ const bool DEFAULT_IS_FULL_BRIGHTNESS_AT_START = true ;
31+ const int BRI_MAX = 255 ;
32+
33+ // Home Assistant API
34+ const int API_DEFAULT_PORT = 8123 ;
35+ const char API_BASE_PATH[] = " /api/" ;
36+ const char API_STATES[] = " states" ;
37+ const char API_LIGHT_TURN_ON[] = " services/light/turn_on" ;
38+ const char API_LIGHT_TURN_OFF[] = " services/light/turn_off" ;
39+
40+ const char ENTITY_ID[] = " entity_id" ;
41+ const char RGB_COLOR[] = " rgb_color" ;
42+ const char BRIGHTNESS[] = " brightness" ;
43+ const char TRANSITION[] = " transition" ;
44+ const char FLASH[] = " flash" ;
45+
46+ // // Home Assistant ssdp services
47+ const char SSDP_ID[] = " ssdp:all" ;
48+ const char SSDP_FILTER_HEADER[] = " ST" ;
49+ const char SSDP_FILTER[] = " (.*)home-assistant.io(.*)" ;
5250
5351} // End of constants
5452
@@ -58,11 +56,12 @@ LedDeviceHomeAssistant::LedDeviceHomeAssistant(const QJsonObject& deviceConfig)
5856 , _apiPort(API_DEFAULT_PORT)
5957 , _isBrightnessOverwrite(DEFAULT_IS_BRIGHTNESS_OVERWRITE)
6058 , _isFullBrightnessAtStart(DEFAULT_IS_FULL_BRIGHTNESS_AT_START)
61- , _brightness (BRI_MAX)
59+ , _brightness(BRI_MAX)
60+ , _transitionTime(0 )
6261{
6362#ifdef ENABLE_MDNS
6463 QMetaObject::invokeMethod (MdnsBrowser::getInstance ().data (), " browseForServiceType" ,
65- Qt::QueuedConnection, Q_ARG (QByteArray, MdnsServiceRegister::getServiceType (_activeDeviceType)));
64+ Qt::QueuedConnection, Q_ARG (QByteArray, MdnsServiceRegister::getServiceType (_activeDeviceType)));
6665#endif
6766}
6867
@@ -81,7 +80,7 @@ bool LedDeviceHomeAssistant::init(const QJsonObject& deviceConfig)
8180{
8281 bool isInitOK{ false };
8382
84- if ( LedDevice::init (deviceConfig) )
83+ if (LedDevice::init (deviceConfig))
8584 {
8685 // Overwrite non supported/required features
8786 if (deviceConfig[" rewriteTime" ].toInt (0 ) > 0 )
@@ -99,30 +98,28 @@ bool LedDeviceHomeAssistant::init(const QJsonObject& deviceConfig)
9998 _isBrightnessOverwrite = _devConfig[CONFIG_BRIGHTNESS_OVERWRITE].toBool (DEFAULT_IS_BRIGHTNESS_OVERWRITE);
10099 _isFullBrightnessAtStart = _devConfig[CONFIG_FULL_BRIGHTNESS_AT_START].toBool (DEFAULT_IS_FULL_BRIGHTNESS_AT_START);
101100 _brightness = _devConfig[CONFIG_BRIGHTNESS].toInt (BRI_MAX);
102- _switchOffOnBlack = _devConfig[CONFIG_ON_OFF_BLACK].toBool (DEFAULT_IS_SWITCH_OFF_ON_BLACK);
103101 int transitionTimeMs = _devConfig[CONFIG_TRANSITIONTIME].toInt (0 );
104102 _transitionTime = transitionTimeMs / 1000.0 ;
105103
106104 Debug (_log, " Hostname/IP : %s" , QSTRING_CSTR (_hostName));
107- Debug (_log, " Port : %d" , _apiPort );
105+ Debug (_log, " Port : %d" , _apiPort);
108106
109- Debug (_log, " Overwrite Brightn.: %s" , _isBrightnessOverwrite ? " Yes" : " No" );
107+ Debug (_log, " Overwrite Brightn.: %s" , _isBrightnessOverwrite ? " Yes" : " No" );
110108 Debug (_log, " Set Brightness to : %d" , _brightness);
111- Debug (_log, " Full Bri. at start: %s" , _isFullBrightnessAtStart ? " Yes" : " No" );
112- Debug (_log, " Off on Black : %s" , _switchOffOnBlack ? " Yes" : " No" );
113- Debug (_log, " Transition Time : %d ms" , transitionTimeMs );
109+ Debug (_log, " Full Bri. at start: %s" , _isFullBrightnessAtStart ? " Yes" : " No" );
110+ Debug (_log, " Transition Time : %d ms" , transitionTimeMs);
114111
115- _lightEntityIds = _devConfig[ CONFIG_ENITYIDS ].toVariant ().toStringList ();
112+ _lightEntityIds = _devConfig[CONFIG_ENITYIDS].toVariant ().toStringList ();
116113 int configuredLightsCount = _lightEntityIds.size ();
117114
118- if ( configuredLightsCount == 0 )
115+ if (configuredLightsCount == 0 )
119116 {
120- this ->setInError ( " No light entity-ids configured" );
117+ this ->setInError (" No light entity-ids configured" );
121118 isInitOK = false ;
122119 }
123120 else
124121 {
125- Debug (_log, " Lights configured : %d" , configuredLightsCount );
122+ Debug (_log, " Lights configured : %d" , configuredLightsCount);
126123 isInitOK = true ;
127124 }
128125 }
@@ -138,11 +135,11 @@ bool LedDeviceHomeAssistant::initLedsConfiguration()
138135 QString lightEntityId = _lightEntityIds[0 ];
139136
140137 // Get properties for configured light entitiy to check availability
141- _restApi->setPath ({ API_STATES, lightEntityId});
138+ _restApi->setPath ({ API_STATES, lightEntityId });
142139 httpResponse response = _restApi->get ();
143140 if (response.error ())
144141 {
145- QString errorReason = QString (" %1 get properties failed with error: '%2'" ).arg (_activeDeviceType,response.getErrorReason ());
142+ QString errorReason = QString (" %1 get properties failed with error: '%2'" ).arg (_activeDeviceType, response.getErrorReason ());
146143 this ->setInError (errorReason);
147144 }
148145 else
@@ -239,10 +236,10 @@ QJsonObject LedDeviceHomeAssistant::discover(const QJsonObject& /*params*/)
239236#ifdef ENABLE_MDNS
240237 QString discoveryMethod (" mDNS" );
241238 deviceList = MdnsBrowser::getInstance ().data ()->getServicesDiscoveredJson (
242- MdnsServiceRegister::getServiceType (_activeDeviceType),
243- MdnsServiceRegister::getServiceNameFilter (_activeDeviceType),
244- DEFAULT_DISCOVER_TIMEOUT
245- );
239+ MdnsServiceRegister::getServiceType (_activeDeviceType),
240+ MdnsServiceRegister::getServiceNameFilter (_activeDeviceType),
241+ DEFAULT_DISCOVER_TIMEOUT
242+ );
246243#else
247244 QString discoveryMethod (" ssdp" );
248245 deviceList = discoverSsdp ();
@@ -288,7 +285,7 @@ QJsonObject LedDeviceHomeAssistant::getProperties(const QJsonObject& params)
288285 QVector<QJsonValue> filteredVector;
289286
290287 // Iterate over the array and filter objects with entity_id starting with "light."
291- for (const QJsonValue & value : jsonArray)
288+ for (const QJsonValue& value : jsonArray)
292289 {
293290 QJsonObject obj = value.toObject ();
294291 QString entityId = obj[ENTITY_ID].toString ();
@@ -300,14 +297,14 @@ QJsonObject LedDeviceHomeAssistant::getProperties(const QJsonObject& params)
300297 }
301298
302299 // Sort the filtered vector by "friendly_name" in ascending order
303- std::sort (filteredVector.begin (), filteredVector.end (), [](const QJsonValue & a, const QJsonValue & b) {
300+ std::sort (filteredVector.begin (), filteredVector.end (), [](const QJsonValue& a, const QJsonValue& b) {
304301 QString nameA = a.toObject ()[" attributes" ].toObject ()[" friendly_name" ].toString ();
305302 QString nameB = b.toObject ()[" attributes" ].toObject ()[" friendly_name" ].toString ();
306303 return nameA < nameB; // Ascending order
307- });
304+ });
308305 // Convert the sorted vector back to a QJsonArray
309306 QJsonArray sortedArray;
310- for (const QJsonValue & value : filteredVector) {
307+ for (const QJsonValue& value : filteredVector) {
311308 sortedArray.append (value);
312309 }
313310
@@ -341,10 +338,10 @@ void LedDeviceHomeAssistant::identify(const QJsonObject& params)
341338 {
342339 if (openRestAPI ())
343340 {
344- QJsonArray lightEntityIds = params[ ENTITY_ID ].toArray ();
341+ QJsonArray lightEntityIds = params[ENTITY_ID].toArray ();
345342
346343 _restApi->setPath (API_LIGHT_TURN_ON);
347- QJsonObject serviceAttributes{{ENTITY_ID, lightEntityIds}};
344+ QJsonObject serviceAttributes{ {ENTITY_ID, lightEntityIds} };
348345 serviceAttributes.insert (FLASH, " short" );
349346
350347 httpResponse response = _restApi->post (serviceAttributes);
@@ -362,7 +359,7 @@ bool LedDeviceHomeAssistant::powerOn()
362359 if (_isDeviceReady)
363360 {
364361 _restApi->setPath (API_LIGHT_TURN_ON);
365- QJsonObject serviceAttributes {{ ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)}};
362+ QJsonObject serviceAttributes{ { ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)} };
366363
367364 if (_isFullBrightnessAtStart)
368365 {
@@ -389,7 +386,7 @@ bool LedDeviceHomeAssistant::powerOff()
389386 if (_isDeviceReady)
390387 {
391388 _restApi->setPath (API_LIGHT_TURN_OFF);
392- QJsonObject serviceAttributes {{ ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)}};
389+ QJsonObject serviceAttributes{ { ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)} };
393390 httpResponse response = _restApi->post (serviceAttributes);
394391 if (response.error ())
395392 {
@@ -405,40 +402,41 @@ int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
405402{
406403 int retVal = 0 ;
407404
408- QJsonObject serviceAttributes {{ ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)}};
405+ QJsonObject serviceAttributes{ { ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)} };
409406 ColorRgb ledValue = ledValues.at (0 );
410407
411- if (_switchOffOnBlack && ledValue == ColorRgb::BLACK)
408+ // http://hostname:port/api/services/light/turn_on
409+ // {
410+ // "entity_id": [ entity-IDs ],
411+ // "rgb_color": [R,G,B]
412+ // }
413+
414+ _restApi->setPath (API_LIGHT_TURN_ON);
415+ serviceAttributes.insert (RGB_COLOR, QJsonArray{ ledValue.red , ledValue.green , ledValue.blue });
416+
417+ int brightness = _brightness;
418+
419+ // Some devices cannot deal with a black color and brightness > 0
420+ if (ledValue == ColorRgb::BLACK)
412421 {
413- _restApi-> setPath (API_LIGHT_TURN_OFF) ;
422+ brightness = 0 ;
414423 }
415- else
416- {
417- // http://hostname:port/api/services/light/turn_on
418- // {
419- // "entity_id": [ entity-IDs ],
420- // "rgb_color": [R,G,B]
421- // }
422424
423- _restApi->setPath (API_LIGHT_TURN_ON);
424- QJsonArray rgbColor {ledValue.red , ledValue.green , ledValue.blue };
425- serviceAttributes.insert (RGB_COLOR, rgbColor);
425+ // Add brightness attribute if applicable
426+ if (brightness == 0 || _isBrightnessOverwrite)
427+ {
428+ serviceAttributes.insert (BRIGHTNESS, brightness);
429+ }
426430
427- if (_isBrightnessOverwrite)
428- {
429- serviceAttributes.insert (BRIGHTNESS, _brightness);
430- }
431- if (_transitionTime > 0 )
432- {
433- // Transition time in seconds
434- serviceAttributes.insert (TRANSITION, _transitionTime);
435- }
431+ if (_transitionTime > 0 )
432+ {
433+ serviceAttributes.insert (TRANSITION, _transitionTime);
436434 }
437435
438436 httpResponse response = _restApi->post (serviceAttributes);
439437 if (response.error ())
440438 {
441- Warning (_log," Updating lights failed with error: '%s'" , QSTRING_CSTR (response.getErrorReason ()) );
439+ Warning (_log, " Updating lights failed with error: '%s'" , QSTRING_CSTR (response.getErrorReason ()));
442440 retVal = -1 ;
443441 }
444442
0 commit comments