Переглянути джерело

add oh_color field to support colorRGB channel type for OpenHAB

Christopher Mullins 6 роки тому
батько
коміт
0a17b586a5

+ 1 - 0
README.md

@@ -259,6 +259,7 @@ You can select which fields should be included in state updates by configuring t
 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.
+1. `oh_color` - same as `color` with a format compatible with [OpenHAB's colorRGB channel type](https://www.openhab.org/addons/bindings/mqtt.generic/#channel-type-colorrgb-colorhsb).
 1. `device_id` / `device_type` / `group_id` - this information is in the MQTT topic or REST route, but can be included in the payload in the case that processing the topic or route is more difficult.
 
 ## UDP Gateways

Різницю між файлами не показано, бо вона завелика
+ 2 - 2
dist/index.html.gz.h


+ 22 - 0
lib/MiLightState/GroupState.cpp

@@ -146,6 +146,7 @@ bool GroupState::isSetField(GroupStateField field) const {
       return isSetBrightness();
     case GroupStateField::COLOR:
     case GroupStateField::HUE:
+    case GroupStateField::OH_COLOR:
       return isSetHue();
     case GroupStateField::SATURATION:
       return isSetSaturation();
@@ -634,6 +635,21 @@ void GroupState::applyColor(ArduinoJson::JsonObject& state, uint8_t r, uint8_t g
   color["b"] = b;
 }
 
+void GroupState::applyOhColor(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
+  );
+  char ohColorStr[13];
+  sprintf(ohColorStr, "%d,%d,%d", rgb[0], rgb[1], rgb[2]);
+  state["color"] = ohColorStr;
+}
+
 // gather partial state for a single field; see GroupState::applyState to gather many fields
 void GroupState::applyField(JsonObject& partialState, const BulbId& bulbId, GroupStateField field) {
   if (isSetField(field)) {
@@ -661,6 +677,12 @@ void GroupState::applyField(JsonObject& partialState, const BulbId& bulbId, Grou
         }
         break;
 
+      case GroupStateField::OH_COLOR:
+        if (getBulbMode() == BULB_MODE_COLOR) {
+          applyOhColor(partialState);
+        }
+        break;
+
       case GroupStateField::COMPUTED_COLOR:
         if (getBulbMode() == BULB_MODE_COLOR) {
           applyColor(partialState);

+ 2 - 0
lib/MiLightState/GroupState.h

@@ -203,6 +203,8 @@ private:
 
   void applyColor(JsonObject& state, uint8_t r, uint8_t g, uint8_t b);
   void applyColor(JsonObject& state);
+  // Apply OpenHAB-style color, e.g., {"color":"0,0,0"}
+  void applyOhColor(JsonObject& state);
 };
 
 extern const BulbId DEFAULT_BULB_ID;

+ 4 - 2
lib/Types/GroupStateField.h

@@ -18,7 +18,8 @@ static const char* STATE_NAMES[] = {
   "effect",
   "device_id",
   "group_id",
-  "device_type"
+  "device_type",
+  "oh_color"
 };
 
 enum class GroupStateField {
@@ -38,7 +39,8 @@ enum class GroupStateField {
   EFFECT,
   DEVICE_ID,
   GROUP_ID,
-  DEVICE_TYPE
+  DEVICE_TYPE,
+  OH_COLOR
 };
 
 class GroupStateFieldHelpers {

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

@@ -233,7 +233,8 @@ var GROUP_STATE_KEYS = [
   "effect",
   "device_id",
   "group_id",
-  "device_type"
+  "device_type",
+  "oh_color"
 ];
 
 var LED_MODES = [