Skip to content

Commit 2c2ad22

Browse files
committed
Add color_mode field which should be supplied for HomeAssistant
1 parent 6543f60 commit 2c2ad22

8 files changed

Lines changed: 69 additions & 27 deletions

File tree

dist/index.html.gz.h

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

lib/MQTT/HomeAssistantDiscoveryClient.cpp

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,16 @@ void HomeAssistantDiscoveryClient::addConfig(const char* alias, const BulbId& bu
104104
break;
105105
}
106106

107-
// These bulbs support RGB color
108-
switch (bulbId.deviceType) {
109-
case REMOTE_TYPE_FUT089:
110-
case REMOTE_TYPE_RGB:
111-
case REMOTE_TYPE_RGB_CCT:
112-
case REMOTE_TYPE_RGBW:
113-
config[F("rgb")] = true;
114-
break;
115-
default:
116-
break; //nothing
107+
// Flag RGB support
108+
if (MiLightRemoteTypeHelpers::supportsRgb(bulbId.deviceType)) {
109+
config[F("rgb")] = true;
117110
}
118111

119-
// These bulbs support adjustable white values
120-
switch (bulbId.deviceType) {
121-
case REMOTE_TYPE_CCT:
122-
case REMOTE_TYPE_FUT089:
123-
case REMOTE_TYPE_FUT091:
124-
case REMOTE_TYPE_RGB_CCT:
125-
config[GroupStateFieldNames::COLOR_TEMP] = true;
126-
config[F("max_mirs")] = COLOR_TEMP_MAX_MIREDS;
127-
config[F("min_mirs")] = COLOR_TEMP_MIN_MIREDS;
128-
break;
129-
default:
130-
break; //nothing
112+
// Flag adjustable color temp support
113+
if (MiLightRemoteTypeHelpers::supportsColorTemp(bulbId.deviceType)) {
114+
config[GroupStateFieldNames::COLOR_TEMP] = true;
115+
config[F("max_mirs")] = COLOR_TEMP_MAX_MIREDS;
116+
config[F("min_mirs")] = COLOR_TEMP_MIN_MIREDS;
131117
}
132118

133119
String message;

lib/MiLightState/GroupState.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ bool GroupState::isSetField(GroupStateField field) const {
241241
case GroupStateField::COLOR_TEMP:
242242
return isSetKelvin();
243243
case GroupStateField::BULB_MODE:
244+
case GroupStateField::COLOR_MODE:
244245
return isSetBulbMode();
245246
default:
246247
Serial.print(F("WARNING: tried to check if unknown field was set: "));
@@ -855,6 +856,29 @@ void GroupState::applyField(JsonObject partialState, const BulbId& bulbId, Group
855856
partialState[GroupStateFieldNames::BULB_MODE] = BULB_MODE_NAMES[getBulbMode()];
856857
break;
857858

859+
// For HomeAssistant. Should report:
860+
// 1. "brightness" if no color temp/rgb support
861+
// 2. "rgb" if RGB or RGBW bulb and in color mode
862+
// 3. "color_temp" if WW or RGBW and in color temp mode
863+
// 4. "onoff" if in night mode
864+
case GroupStateField::COLOR_MODE:
865+
if (
866+
MiLightRemoteTypeHelpers::supportsRgb(bulbId.deviceType)
867+
&& getBulbMode() == BULB_MODE_COLOR
868+
) {
869+
partialState[GroupStateFieldNames::COLOR_MODE] = F("rgb");
870+
} else if (
871+
MiLightRemoteTypeHelpers::supportsColorTemp(bulbId.deviceType)
872+
&& getBulbMode() == BULB_MODE_WHITE
873+
) {
874+
partialState[GroupStateFieldNames::COLOR_MODE] = F("color_temp");
875+
} else if (getBulbMode() == BULB_MODE_NIGHT) {
876+
partialState[GroupStateFieldNames::COLOR_MODE] = F("onoff");
877+
} else {
878+
partialState[GroupStateFieldNames::COLOR_MODE] = F("brightness");
879+
}
880+
break;
881+
858882
case GroupStateField::COLOR:
859883
if (getBulbMode() == BULB_MODE_COLOR) {
860884
applyColor(partialState);

lib/Types/GroupStateField.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ static const char* STATE_NAMES[] = {
2020
GroupStateFieldNames::GROUP_ID,
2121
GroupStateFieldNames::DEVICE_TYPE,
2222
GroupStateFieldNames::OH_COLOR,
23-
GroupStateFieldNames::HEX_COLOR
23+
GroupStateFieldNames::HEX_COLOR,
24+
GroupStateFieldNames::COLOR_MODE,
2425
};
2526

2627
GroupStateField GroupStateFieldHelpers::getFieldByName(const char* name) {

lib/Types/GroupStateField.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ namespace GroupStateFieldNames {
2424
static const char HEX_COLOR[] = "hex_color";
2525
static const char COMMAND[] = "command";
2626
static const char COMMANDS[] = "commands";
27+
28+
// For use with HomeAssistant
29+
static const char COLOR_MODE[] = "color_mode";
2730
};
2831

2932
enum class GroupStateField {
@@ -45,7 +48,8 @@ enum class GroupStateField {
4548
GROUP_ID,
4649
DEVICE_TYPE,
4750
OH_COLOR,
48-
HEX_COLOR
51+
HEX_COLOR,
52+
COLOR_MODE,
4953
};
5054

5155
class GroupStateFieldHelpers {

lib/Types/MiLightRemoteType.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,28 @@ const String MiLightRemoteTypeHelpers::remoteTypeToString(const MiLightRemoteTyp
6565
Serial.println(type);
6666
return "unknown";
6767
}
68+
}
69+
70+
const bool MiLightRemoteTypeHelpers::supportsRgb(const MiLightRemoteType type) {
71+
switch (type) {
72+
case REMOTE_TYPE_FUT089:
73+
case REMOTE_TYPE_RGB:
74+
case REMOTE_TYPE_RGB_CCT:
75+
case REMOTE_TYPE_RGBW:
76+
return true;
77+
default:
78+
return false;
79+
}
80+
}
81+
82+
const bool MiLightRemoteTypeHelpers::supportsColorTemp(const MiLightRemoteType type) {
83+
switch (type) {
84+
case REMOTE_TYPE_CCT:
85+
case REMOTE_TYPE_FUT089:
86+
case REMOTE_TYPE_FUT091:
87+
case REMOTE_TYPE_RGB_CCT:
88+
return true;
89+
default:
90+
return false;
91+
}
6892
}

lib/Types/MiLightRemoteType.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@ class MiLightRemoteTypeHelpers {
1717
public:
1818
static const MiLightRemoteType remoteTypeFromString(const String& type);
1919
static const String remoteTypeToString(const MiLightRemoteType type);
20+
static const bool supportsRgb(const MiLightRemoteType type);
21+
static const bool supportsColorTemp(const MiLightRemoteType type);
2022
};

web/src/js/script.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ var GROUP_STATE_KEYS = [
390390
"group_id",
391391
"device_type",
392392
"oh_color",
393-
"hex_color"
393+
"hex_color",
394+
"color_mode"
394395
];
395396

396397
var LED_MODES = [

0 commit comments

Comments
 (0)