Explorar o código

Merge pull request #161 from sidoh/computed_color

Send rgb=255,255,255 color state when bulbs in white mode
Chris Mullins %!s(int64=8) %!d(string=hai) anos
pai
achega
6929d34790

+ 10 - 0
README.md

@@ -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.).

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 2 - 2
dist/index.html.gz.h


+ 35 - 13
lib/MiLightState/GroupState.cpp

@@ -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();
@@ -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;
 }
 
@@ -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) {
@@ -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;
 

+ 3 - 0
lib/MiLightState/GroupState.h

@@ -126,6 +126,9 @@ private:
   };
 
   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;

+ 1 - 1
lib/Settings/Settings.h

@@ -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

+ 4 - 2
lib/Types/GroupStateField.h

@@ -13,7 +13,8 @@ static const char* STATE_NAMES[] = {
   "mode",
   "kelvin",
   "color_temp",
-  "bulb_mode"
+  "bulb_mode",
+  "computed_color"
 };
 
 enum class GroupStateField {
@@ -28,7 +29,8 @@ enum class GroupStateField {
   MODE,
   KELVIN,
   COLOR_TEMP,
-  BULB_MODE
+  BULB_MODE,
+  COMPUTED_COLOR
 };
 
 class GroupStateFieldHelpers {

+ 2 - 1
web/src/js/script.js

@@ -25,7 +25,8 @@ var GROUP_STATE_KEYS = [
   "mode",
   "kelvin",
   "color_temp",
-  "bulb_mode"
+  "bulb_mode",
+  "computed_color"
 ];
 
 var FORM_SETTINGS_HELP = {