14
14
15
15
// Constants
16
16
namespace {
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(.*)" ;
52
50
53
51
} // End of constants
54
52
@@ -58,11 +56,12 @@ LedDeviceHomeAssistant::LedDeviceHomeAssistant(const QJsonObject& deviceConfig)
58
56
, _apiPort(API_DEFAULT_PORT)
59
57
, _isBrightnessOverwrite(DEFAULT_IS_BRIGHTNESS_OVERWRITE)
60
58
, _isFullBrightnessAtStart(DEFAULT_IS_FULL_BRIGHTNESS_AT_START)
61
- , _brightness (BRI_MAX)
59
+ , _brightness(BRI_MAX)
60
+ , _transitionTime(0 )
62
61
{
63
62
#ifdef ENABLE_MDNS
64
63
QMetaObject::invokeMethod (MdnsBrowser::getInstance ().data (), " browseForServiceType" ,
65
- Qt::QueuedConnection, Q_ARG (QByteArray, MdnsServiceRegister::getServiceType (_activeDeviceType)));
64
+ Qt::QueuedConnection, Q_ARG (QByteArray, MdnsServiceRegister::getServiceType (_activeDeviceType)));
66
65
#endif
67
66
}
68
67
@@ -81,7 +80,7 @@ bool LedDeviceHomeAssistant::init(const QJsonObject& deviceConfig)
81
80
{
82
81
bool isInitOK{ false };
83
82
84
- if ( LedDevice::init (deviceConfig) )
83
+ if (LedDevice::init (deviceConfig))
85
84
{
86
85
// Overwrite non supported/required features
87
86
if (deviceConfig[" rewriteTime" ].toInt (0 ) > 0 )
@@ -99,30 +98,28 @@ bool LedDeviceHomeAssistant::init(const QJsonObject& deviceConfig)
99
98
_isBrightnessOverwrite = _devConfig[CONFIG_BRIGHTNESS_OVERWRITE].toBool (DEFAULT_IS_BRIGHTNESS_OVERWRITE);
100
99
_isFullBrightnessAtStart = _devConfig[CONFIG_FULL_BRIGHTNESS_AT_START].toBool (DEFAULT_IS_FULL_BRIGHTNESS_AT_START);
101
100
_brightness = _devConfig[CONFIG_BRIGHTNESS].toInt (BRI_MAX);
102
- _switchOffOnBlack = _devConfig[CONFIG_ON_OFF_BLACK].toBool (DEFAULT_IS_SWITCH_OFF_ON_BLACK);
103
101
int transitionTimeMs = _devConfig[CONFIG_TRANSITIONTIME].toInt (0 );
104
102
_transitionTime = transitionTimeMs / 1000.0 ;
105
103
106
104
Debug (_log, " Hostname/IP : %s" , QSTRING_CSTR (_hostName));
107
- Debug (_log, " Port : %d" , _apiPort );
105
+ Debug (_log, " Port : %d" , _apiPort);
108
106
109
- Debug (_log, " Overwrite Brightn.: %s" , _isBrightnessOverwrite ? " Yes" : " No" );
107
+ Debug (_log, " Overwrite Brightn.: %s" , _isBrightnessOverwrite ? " Yes" : " No" );
110
108
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);
114
111
115
- _lightEntityIds = _devConfig[ CONFIG_ENITYIDS ].toVariant ().toStringList ();
112
+ _lightEntityIds = _devConfig[CONFIG_ENITYIDS].toVariant ().toStringList ();
116
113
int configuredLightsCount = _lightEntityIds.size ();
117
114
118
- if ( configuredLightsCount == 0 )
115
+ if (configuredLightsCount == 0 )
119
116
{
120
- this ->setInError ( " No light entity-ids configured" );
117
+ this ->setInError (" No light entity-ids configured" );
121
118
isInitOK = false ;
122
119
}
123
120
else
124
121
{
125
- Debug (_log, " Lights configured : %d" , configuredLightsCount );
122
+ Debug (_log, " Lights configured : %d" , configuredLightsCount);
126
123
isInitOK = true ;
127
124
}
128
125
}
@@ -138,11 +135,11 @@ bool LedDeviceHomeAssistant::initLedsConfiguration()
138
135
QString lightEntityId = _lightEntityIds[0 ];
139
136
140
137
// Get properties for configured light entitiy to check availability
141
- _restApi->setPath ({ API_STATES, lightEntityId});
138
+ _restApi->setPath ({ API_STATES, lightEntityId });
142
139
httpResponse response = _restApi->get ();
143
140
if (response.error ())
144
141
{
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 ());
146
143
this ->setInError (errorReason);
147
144
}
148
145
else
@@ -239,10 +236,10 @@ QJsonObject LedDeviceHomeAssistant::discover(const QJsonObject& /*params*/)
239
236
#ifdef ENABLE_MDNS
240
237
QString discoveryMethod (" mDNS" );
241
238
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
+ );
246
243
#else
247
244
QString discoveryMethod (" ssdp" );
248
245
deviceList = discoverSsdp ();
@@ -288,7 +285,7 @@ QJsonObject LedDeviceHomeAssistant::getProperties(const QJsonObject& params)
288
285
QVector<QJsonValue> filteredVector;
289
286
290
287
// 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)
292
289
{
293
290
QJsonObject obj = value.toObject ();
294
291
QString entityId = obj[ENTITY_ID].toString ();
@@ -300,14 +297,14 @@ QJsonObject LedDeviceHomeAssistant::getProperties(const QJsonObject& params)
300
297
}
301
298
302
299
// 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) {
304
301
QString nameA = a.toObject ()[" attributes" ].toObject ()[" friendly_name" ].toString ();
305
302
QString nameB = b.toObject ()[" attributes" ].toObject ()[" friendly_name" ].toString ();
306
303
return nameA < nameB; // Ascending order
307
- });
304
+ });
308
305
// Convert the sorted vector back to a QJsonArray
309
306
QJsonArray sortedArray;
310
- for (const QJsonValue & value : filteredVector) {
307
+ for (const QJsonValue& value : filteredVector) {
311
308
sortedArray.append (value);
312
309
}
313
310
@@ -341,10 +338,10 @@ void LedDeviceHomeAssistant::identify(const QJsonObject& params)
341
338
{
342
339
if (openRestAPI ())
343
340
{
344
- QJsonArray lightEntityIds = params[ ENTITY_ID ].toArray ();
341
+ QJsonArray lightEntityIds = params[ENTITY_ID].toArray ();
345
342
346
343
_restApi->setPath (API_LIGHT_TURN_ON);
347
- QJsonObject serviceAttributes{{ENTITY_ID, lightEntityIds}};
344
+ QJsonObject serviceAttributes{ {ENTITY_ID, lightEntityIds} };
348
345
serviceAttributes.insert (FLASH, " short" );
349
346
350
347
httpResponse response = _restApi->post (serviceAttributes);
@@ -362,7 +359,7 @@ bool LedDeviceHomeAssistant::powerOn()
362
359
if (_isDeviceReady)
363
360
{
364
361
_restApi->setPath (API_LIGHT_TURN_ON);
365
- QJsonObject serviceAttributes {{ ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)}};
362
+ QJsonObject serviceAttributes{ { ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)} };
366
363
367
364
if (_isFullBrightnessAtStart)
368
365
{
@@ -389,7 +386,7 @@ bool LedDeviceHomeAssistant::powerOff()
389
386
if (_isDeviceReady)
390
387
{
391
388
_restApi->setPath (API_LIGHT_TURN_OFF);
392
- QJsonObject serviceAttributes {{ ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)}};
389
+ QJsonObject serviceAttributes{ { ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)} };
393
390
httpResponse response = _restApi->post (serviceAttributes);
394
391
if (response.error ())
395
392
{
@@ -405,40 +402,41 @@ int LedDeviceHomeAssistant::write(const std::vector<ColorRgb>& ledValues)
405
402
{
406
403
int retVal = 0 ;
407
404
408
- QJsonObject serviceAttributes {{ ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)}};
405
+ QJsonObject serviceAttributes{ { ENTITY_ID, QJsonArray::fromStringList (_lightEntityIds)} };
409
406
ColorRgb ledValue = ledValues.at (0 );
410
407
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)
412
421
{
413
- _restApi-> setPath (API_LIGHT_TURN_OFF) ;
422
+ brightness = 0 ;
414
423
}
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
- // }
422
424
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
+ }
426
430
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);
436
434
}
437
435
438
436
httpResponse response = _restApi->post (serviceAttributes);
439
437
if (response.error ())
440
438
{
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 ()));
442
440
retVal = -1 ;
443
441
}
444
442
0 commit comments