Просмотр исходного кода

Merge pull request #106 from r4nd0mbr1ck/master

Switch to white if white_value in payload
Chris Mullins лет назад: 8
Родитель
Сommit
b48e469090
2 измененных файлов с 31 добавлено и 12 удалено
  1. 27 10
      lib/MiLight/MiLightClient.cpp
  2. 4 2
      lib/MiLight/MiLightClient.h

+ 27 - 10
lib/MiLight/MiLightClient.cpp

@@ -253,6 +253,11 @@ void MiLightClient::update(const JsonObject& request) {
     }
   }
 
+  //Homeassistant - Handle effect
+  if (request.containsKey("effect")) {
+    this->handleEffect(request["effect"]);
+  }
+
   if (request.containsKey("hue")) {
     this->updateHue(request["hue"]);
   }
@@ -267,16 +272,20 @@ void MiLightClient::update(const JsonObject& request) {
     uint8_t r = color["r"];
     uint8_t g = color["g"];
     uint8_t b = color["b"];
-
-    double hsv[3];
-    RGBConverter converter;
-    converter.rgbToHsv(r, g, b, hsv);
-
-    uint16_t hue = round(hsv[0]*360);
-    uint8_t saturation = round(hsv[1]*100);
-
-    this->updateHue(hue);
-    this->updateSaturation(saturation);
+    //If close to white
+    if( r > 256 - RGB_WHITE_BOUNDARY && g > 256 - RGB_WHITE_BOUNDARY && b > 256 - RGB_WHITE_BOUNDARY) {
+        this->updateColorWhite();
+    } else {
+      double hsv[3];
+      RGBConverter converter;
+      converter.rgbToHsv(r, g, b, hsv);
+
+      uint16_t hue = round(hsv[0]*360);
+      uint8_t saturation = round(hsv[1]*100);
+
+      this->updateHue(hue);
+      this->updateSaturation(saturation);
+    }
   }
 
   if (request.containsKey("level")) {
@@ -336,6 +345,14 @@ void MiLightClient::handleCommand(const String& command) {
   }
 }
 
+void MiLightClient::handleEffect(const String& effect) {
+  if (effect == "night_mode") {
+    this->enableNightMode();
+  } else if (effect == "white") {
+    this->updateColorWhite();
+  }
+}
+
 uint8_t MiLightClient::parseStatus(const JsonObject& object) {
   String strStatus;
 

+ 4 - 2
lib/MiLight/MiLightClient.h

@@ -11,7 +11,8 @@
 //#define DEBUG_PRINTF
 
 #define MILIGHT_DEFAULT_RESEND_COUNT 10
-
+//Used to determine close to white
+#define RGB_WHITE_BOUNDARY 40
 
 class MiLightClient {
 public:
@@ -66,7 +67,8 @@ public:
 
   void update(const JsonObject& object);
   void handleCommand(const String& command);
-
+  void handleEffect(const String& effect);
+  
   void onPacketSent(PacketSentHandler handler);
 
 protected: