Skip to content

Commit

Permalink
Merge branch 'color_mode_field' into v1.11.0
Browse files Browse the repository at this point in the history
# Conflicts:
#	dist/index.html.gz.h
  • Loading branch information
sidoh committed Sep 27, 2023
2 parents bd1246c + 076c292 commit a8815b5
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 25 deletions.
30 changes: 8 additions & 22 deletions lib/MQTT/HomeAssistantDiscoveryClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,16 @@ void HomeAssistantDiscoveryClient::addConfig(const char* alias, const BulbId& bu
break;
}

// These bulbs support RGB color
switch (bulbId.deviceType) {
case REMOTE_TYPE_FUT089:
case REMOTE_TYPE_RGB:
case REMOTE_TYPE_RGB_CCT:
case REMOTE_TYPE_RGBW:
config[F("rgb")] = true;
break;
default:
break; //nothing
// Flag RGB support
if (MiLightRemoteTypeHelpers::supportsRgb(bulbId.deviceType)) {
config[F("rgb")] = true;
}

// These bulbs support adjustable white values
switch (bulbId.deviceType) {
case REMOTE_TYPE_CCT:
case REMOTE_TYPE_FUT089:
case REMOTE_TYPE_FUT091:
case REMOTE_TYPE_RGB_CCT:
config[GroupStateFieldNames::COLOR_TEMP] = true;
config[F("max_mirs")] = COLOR_TEMP_MAX_MIREDS;
config[F("min_mirs")] = COLOR_TEMP_MIN_MIREDS;
break;
default:
break; //nothing
// Flag adjustable color temp support
if (MiLightRemoteTypeHelpers::supportsColorTemp(bulbId.deviceType)) {
config[GroupStateFieldNames::COLOR_TEMP] = true;
config[F("max_mirs")] = COLOR_TEMP_MAX_MIREDS;
config[F("min_mirs")] = COLOR_TEMP_MIN_MIREDS;
}

String message;
Expand Down
24 changes: 24 additions & 0 deletions lib/MiLightState/GroupState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ bool GroupState::isSetField(GroupStateField field) const {
case GroupStateField::COLOR_TEMP:
return isSetKelvin();
case GroupStateField::BULB_MODE:
case GroupStateField::COLOR_MODE:
return isSetBulbMode();
default:
Serial.print(F("WARNING: tried to check if unknown field was set: "));
Expand Down Expand Up @@ -855,6 +856,29 @@ void GroupState::applyField(JsonObject partialState, const BulbId& bulbId, Group
partialState[GroupStateFieldNames::BULB_MODE] = BULB_MODE_NAMES[getBulbMode()];
break;

// For HomeAssistant. Should report:
// 1. "brightness" if no color temp/rgb support
// 2. "rgb" if RGB or RGBW bulb and in color mode
// 3. "color_temp" if WW or RGBW and in color temp mode
// 4. "onoff" if in night mode
case GroupStateField::COLOR_MODE:
if (
MiLightRemoteTypeHelpers::supportsRgb(bulbId.deviceType)
&& getBulbMode() == BULB_MODE_COLOR
) {
partialState[GroupStateFieldNames::COLOR_MODE] = F("rgb");
} else if (
MiLightRemoteTypeHelpers::supportsColorTemp(bulbId.deviceType)
&& getBulbMode() == BULB_MODE_WHITE
) {
partialState[GroupStateFieldNames::COLOR_MODE] = F("color_temp");
} else if (getBulbMode() == BULB_MODE_NIGHT) {
partialState[GroupStateFieldNames::COLOR_MODE] = F("onoff");
} else {
partialState[GroupStateFieldNames::COLOR_MODE] = F("brightness");
}
break;

case GroupStateField::COLOR:
if (getBulbMode() == BULB_MODE_COLOR) {
applyColor(partialState);
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static const std::vector<GroupStateField> DEFAULT_GROUP_STATE_FIELDS({
GroupStateField::COMPUTED_COLOR,
GroupStateField::MODE,
GroupStateField::COLOR_TEMP,
GroupStateField::BULB_MODE
GroupStateField::COLOR_MODE
});

struct GatewayConfig {
Expand Down
3 changes: 2 additions & 1 deletion lib/Types/GroupStateField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ static const char* STATE_NAMES[] = {
GroupStateFieldNames::GROUP_ID,
GroupStateFieldNames::DEVICE_TYPE,
GroupStateFieldNames::OH_COLOR,
GroupStateFieldNames::HEX_COLOR
GroupStateFieldNames::HEX_COLOR,
GroupStateFieldNames::COLOR_MODE,
};

GroupStateField GroupStateFieldHelpers::getFieldByName(const char* name) {
Expand Down
6 changes: 5 additions & 1 deletion lib/Types/GroupStateField.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace GroupStateFieldNames {
static const char HEX_COLOR[] = "hex_color";
static const char COMMAND[] = "command";
static const char COMMANDS[] = "commands";

// For use with HomeAssistant
static const char COLOR_MODE[] = "color_mode";
};

enum class GroupStateField {
Expand All @@ -45,7 +48,8 @@ enum class GroupStateField {
GROUP_ID,
DEVICE_TYPE,
OH_COLOR,
HEX_COLOR
HEX_COLOR,
COLOR_MODE,
};

class GroupStateFieldHelpers {
Expand Down
24 changes: 24 additions & 0 deletions lib/Types/MiLightRemoteType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,28 @@ const String MiLightRemoteTypeHelpers::remoteTypeToString(const MiLightRemoteTyp
Serial.println(type);
return "unknown";
}
}

const bool MiLightRemoteTypeHelpers::supportsRgb(const MiLightRemoteType type) {
switch (type) {
case REMOTE_TYPE_FUT089:
case REMOTE_TYPE_RGB:
case REMOTE_TYPE_RGB_CCT:
case REMOTE_TYPE_RGBW:
return true;
default:
return false;
}
}

const bool MiLightRemoteTypeHelpers::supportsColorTemp(const MiLightRemoteType type) {
switch (type) {
case REMOTE_TYPE_CCT:
case REMOTE_TYPE_FUT089:
case REMOTE_TYPE_FUT091:
case REMOTE_TYPE_RGB_CCT:
return true;
default:
return false;
}
}
2 changes: 2 additions & 0 deletions lib/Types/MiLightRemoteType.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ class MiLightRemoteTypeHelpers {
public:
static const MiLightRemoteType remoteTypeFromString(const String& type);
static const String remoteTypeToString(const MiLightRemoteType type);
static const bool supportsRgb(const MiLightRemoteType type);
static const bool supportsColorTemp(const MiLightRemoteType type);
};
34 changes: 34 additions & 0 deletions test/remote/spec/state_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -563,4 +563,38 @@
expect(state['status']).to eq('ON')
end
end

context 'color_mode field' do
before(:all) do
@client.patch_settings({
group_state_fields: [
"status",
"level",
"kelvin",
"hue",
"saturation",
"color_mode",
"hex_color"
]
})
end

context 'for rgb+ww lights' do
it 'when in color mode, field should be "rgb" and color fields should be present' do
state = @client.patch_state({status: 'ON', color: '#ff0000'}, @id_params)

expect(state['color_mode']).to eq('rgb')
expect(state['hue']).to eq(0)
expect(state['saturation']).to eq(100)
expect(state['color']).to eq('#FF0000')
end

it 'when in white mode, field should be "color_temp" and color fields should not be present' do
state = @client.patch_state({status: 'ON', kelvin: 100, level: 100}, @id_params)

expect(state['color_mode']).to eq('color_temp')
expect(state['kelvin']).to eq(100)
end
end
end
end

0 comments on commit a8815b5

Please sign in to comment.