Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send rgb=255,255,255 color state when bulbs in white mode #161

Merged
merged 3 commits into from Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -223,6 +223,16 @@ irb(main):007:0> puts client.get.inspect

**Make sure that `mqtt_topic_pattern`, `mqtt_state_topic_pattern`, and `matt_update_topic_pattern` are all different!** If they are they same you can put your ESP in a loop where its own updates trigger an infinite command loop.

##### Customize fields

You can select which fields should be included in state updates by configuring the `group_state_fields` parameter. Available fields should be mostly self explanatory, with the possible exceptions of:

1. `state` / `status` - same value with different keys (useful if your platform expects one or the other).
1. `brightness` / `level` - [0, 255] and [0, 100] scales of the same value.
1. `kelvin / color_temp` - [0, 100] and [153, 370] scales for the same value. The later's unit is mireds.
1. `bulb_mode` - what mode the bulb is in: white, rgb, etc.
1. `color` / `computed_color` - behaves the same when bulb is in rgb mode. `computed_color` will send RGB = 255,255,255 when in white mode. This is useful for HomeAssistant where it always expects the color to be set.

## UDP Gateways

You can add an arbitrary number of UDP gateways through the REST API or through the web UI. Each gateway server listens on a port and responds to the standard set of commands supported by the Milight protocol. This should allow you to use one of these with standard Milight integrations (SmartThings, Home Assistant, OpenHAB, etc.).
Expand Down
4 changes: 2 additions & 2 deletions dist/index.html.gz.h

Large diffs are not rendered by default.

48 changes: 35 additions & 13 deletions lib/MiLightState/GroupState.cpp
Expand Up @@ -78,6 +78,9 @@ GroupState::GroupState() {

bool GroupState::isSetField(GroupStateField field) const {
switch (field) {
case GroupStateField::COMPUTED_COLOR:
// Always set -- either send RGB color or white
return true;
case GroupStateField::STATE:
case GroupStateField::STATUS:
return isSetState();
Expand All @@ -98,6 +101,9 @@ bool GroupState::isSetField(GroupStateField field) const {
return isSetBulbMode();
}

Serial.print(F("WARNING: tried to check if unknown field was set: "));
Serial.println(static_cast<unsigned int>(field));

return false;
}

Expand Down Expand Up @@ -312,6 +318,26 @@ bool GroupState::patch(const JsonObject& state) {
return changes;
}

void GroupState::applyColor(ArduinoJson::JsonObject& state) {
uint8_t rgb[3];
RGBConverter converter;
converter.hsvToRgb(
getHue()/360.0,
// Default to fully saturated
(isSetSaturation() ? getSaturation() : 100)/100.0,
1,
rgb
);
applyColor(state, rgb[0], rgb[1], rgb[2]);
}

void GroupState::applyColor(ArduinoJson::JsonObject& state, uint8_t r, uint8_t g, uint8_t b) {
JsonObject& color = state.createNestedObject("color");
color["r"] = r;
color["g"] = g;
color["b"] = b;
}

void GroupState::applyField(JsonObject& partialState, GroupStateField field) {
if (isSetField(field)) {
switch (field) {
Expand All @@ -334,19 +360,15 @@ void GroupState::applyField(JsonObject& partialState, GroupStateField field) {

case GroupStateField::COLOR:
if (getBulbMode() == BULB_MODE_COLOR) {
uint8_t rgb[3];
RGBConverter converter;
converter.hsvToRgb(
getHue()/360.0,
// Default to fully saturated
(isSetSaturation() ? getSaturation() : 100)/100.0,
1,
rgb
);
JsonObject& color = partialState.createNestedObject("color");
color["r"] = rgb[0];
color["g"] = rgb[1];
color["b"] = rgb[2];
applyColor(partialState);
}
break;

case GroupStateField::COMPUTED_COLOR:
if (getBulbMode() == BULB_MODE_COLOR) {
applyColor(partialState);
} else {
applyColor(partialState, 255, 255, 255);
}
break;

Expand Down
3 changes: 3 additions & 0 deletions lib/MiLightState/GroupState.h
Expand Up @@ -126,6 +126,9 @@ class GroupState {
};

Data state;

void applyColor(JsonObject& state, uint8_t r, uint8_t g, uint8_t b);
void applyColor(JsonObject& state);
};

extern const BulbId DEFAULT_BULB_ID;
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings/Settings.h
Expand Up @@ -46,7 +46,7 @@ enum RadioInterfaceType {
static const GroupStateField DEFAULT_GROUP_STATE_FIELDS[] = {
GroupStateField::STATE,
GroupStateField::BRIGHTNESS,
GroupStateField::COLOR,
GroupStateField::COMPUTED_COLOR,
GroupStateField::MODE,
GroupStateField::COLOR_TEMP,
GroupStateField::BULB_MODE
Expand Down
6 changes: 4 additions & 2 deletions lib/Types/GroupStateField.h
Expand Up @@ -13,7 +13,8 @@ static const char* STATE_NAMES[] = {
"mode",
"kelvin",
"color_temp",
"bulb_mode"
"bulb_mode",
"computed_color"
};

enum class GroupStateField {
Expand All @@ -28,7 +29,8 @@ enum class GroupStateField {
MODE,
KELVIN,
COLOR_TEMP,
BULB_MODE
BULB_MODE,
COMPUTED_COLOR
};

class GroupStateFieldHelpers {
Expand Down
3 changes: 2 additions & 1 deletion web/src/js/script.js
Expand Up @@ -25,7 +25,8 @@ var GROUP_STATE_KEYS = [
"mode",
"kelvin",
"color_temp",
"bulb_mode"
"bulb_mode",
"computed_color"
];

var FORM_SETTINGS_HELP = {
Expand Down