Small change to power state initial sequence

When HostPowerState is constructed with a callback, if it reads true
from HostState (different from the init value of _powerState), the
callback will be called. This is not a power state change at all, so we
shouldn't call the callback.

Tested with monitor-use-host-state enabled for phosphor-fan-monitor
service.

When HostState is On, restart phosphor-fan-monitor service.
Result: the service won't throw or turn host off.

Signed-off-by: Chau Ly <chaul@amperecomputing.com>
Change-Id: Ie616a9631a129401a3cdc000612a8e694d02ef7a
diff --git a/power_state.hpp b/power_state.hpp
index 478b596..c428a39 100644
--- a/power_state.hpp
+++ b/power_state.hpp
@@ -293,12 +293,13 @@
             HostState hostState = *currentHostState;
 
             hostPowerStates.emplace_back(hostState);
-            setHostPowerState(hostPowerStates);
+            setHostPowerState(hostPowerStates, true);
         }
     }
 
   private:
-    void setHostPowerState(std::vector<HostState>& hostPowerStates)
+    void setHostPowerState(std::vector<HostState>& hostPowerStates,
+                           bool callFromStateChange)
     {
         bool powerStateflag = false;
         for (const auto& powerState : hostPowerStates)
@@ -313,7 +314,19 @@
                 break;
             }
         }
-        setPowerState(powerStateflag);
+        if (callFromStateChange)
+        {
+            setPowerState(powerStateflag);
+        }
+        else
+        {
+            // This won't call callbacks (if exists) during the constructor of
+            // the class when the first read for this flag is true which is
+            // different from the init value of _powerState.
+            // Daemon that wants to do anything when the initial power state
+            // is true can check isPowerOn() and do it.
+            _powerState = powerStateflag;
+        }
     }
 
     /**
@@ -351,7 +364,7 @@
                 }
             }
         }
-        setHostPowerState(hostPowerStates);
+        setHostPowerState(hostPowerStates, false);
     }
 
     const std::string _hostStatePath{"/xyz/openbmc_project/state"};