Bladeren bron

Bugfix: boolean values for state/status field are not properly recognized (#477)

* Add failing test

* Fix bug where boolean value for state/status field was not properly parsed
Chris Mullins 6 jaren geleden
bovenliggende
commit
21ea62165c
2 gewijzigde bestanden met toevoegingen van 27 en 4 verwijderingen
  1. 9 4
      lib/MiLight/MiLightClient.cpp
  2. 18 0
      test/remote/spec/state_spec.rb

+ 9 - 4
lib/MiLight/MiLightClient.cpp

@@ -496,17 +496,22 @@ void MiLightClient::handleEffect(const String& effect) {
 }
 
 uint8_t MiLightClient::parseStatus(JsonObject object) {
-  String strStatus;
+  JsonVariant status;
 
   if (object.containsKey("status")) {
-    strStatus = object["status"].as<char*>();
+    status = object["status"];
   } else if (object.containsKey("state")) {
-    strStatus = object["state"].as<char*>();
+    status = object["state"];
   } else {
     return 255;
   }
 
-  return (strStatus.equalsIgnoreCase("on") || strStatus.equalsIgnoreCase("true")) ? ON : OFF;
+  if (status.is<bool>()) {
+    return status.as<bool>() ? ON : OFF;
+  } else {
+    String strStatus(status.as<const char*>());
+    return (strStatus.equalsIgnoreCase("on") || strStatus.equalsIgnoreCase("true")) ? ON : OFF;
+  }
 }
 
 void MiLightClient::updateResendCount() {

+ 18 - 0
test/remote/spec/state_spec.rb

@@ -264,6 +264,24 @@ RSpec.describe 'State' do
   end
 
   context 'fields' do
+    it 'should support on/off' do
+      @client.patch_state({status: 'on'}, @id_params)
+      expect(@client.get_state(@id_params)['status']).to eq('ON')
+
+      # test "state", which is an alias for "status"
+      @client.patch_state({state: 'off'}, @id_params)
+      expect(@client.get_state(@id_params)['status']).to eq('OFF')
+    end
+
+    it 'should support boolean values for status' do
+      # test boolean value "true", which should be the same as "ON".
+      @client.patch_state({status: true}, @id_params)
+      expect(@client.get_state(@id_params)['status']).to eq('ON')
+
+      @client.patch_state({state: false}, @id_params)
+      expect(@client.get_state(@id_params)['status']).to eq('OFF')
+    end
+
     it 'should support the color field' do
       desired_state = {
         'hue' => 0,