Factor out getPowerState() helper function

This same pattern had been open-coded in many of the sensor daemons;
let's reduce the duplication a bit.

Signed-off-by: Zev Weiss <zev@bewilderbeest.net>
Change-Id: I8556af30fdcd53cdf06e5a4911f9152d3265c7af
diff --git a/include/Utils.hpp b/include/Utils.hpp
index 92a8b6f..b823e5b 100644
--- a/include/Utils.hpp
+++ b/include/Utils.hpp
@@ -170,6 +170,19 @@
     }
 }
 
+inline PowerState getPowerState(const SensorBaseConfigMap& cfg)
+{
+    PowerState state = PowerState::always;
+    auto findPowerState = cfg.find("PowerState");
+    if (findPowerState != cfg.end())
+    {
+        std::string powerState =
+            std::visit(VariantToStringVisitor(), findPowerState->second);
+        setReadState(powerState, state);
+    }
+    return state;
+}
+
 inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn,
                    const std::string& name, bool on)
 {
diff --git a/src/ADCSensorMain.cpp b/src/ADCSensorMain.cpp
index cec0984..fe8334d 100644
--- a/src/ADCSensorMain.cpp
+++ b/src/ADCSensorMain.cpp
@@ -253,14 +253,7 @@
                 }
             }
 
-            auto findPowerOn = baseConfiguration->second.find("PowerState");
-            PowerState readState = PowerState::always;
-            if (findPowerOn != baseConfiguration->second.end())
-            {
-                std::string powerState =
-                    std::visit(VariantToStringVisitor(), findPowerOn->second);
-                setReadState(powerState, readState);
-            }
+            PowerState readState = getPowerState(baseConfiguration->second);
 
             auto& sensor = sensors[sensorName];
             sensor = nullptr;
diff --git a/src/ExternalSensorMain.cpp b/src/ExternalSensorMain.cpp
index cbe2388..0a42274 100644
--- a/src/ExternalSensorMain.cpp
+++ b/src/ExternalSensorMain.cpp
@@ -307,14 +307,7 @@
                           << "\n";
             }
 
-            auto findPowerOn = baseConfiguration.second.find("PowerState");
-            PowerState readState = PowerState::always;
-            if (findPowerOn != baseConfiguration.second.end())
-            {
-                std::string powerState =
-                    std::visit(VariantToStringVisitor(), findPowerOn->second);
-                setReadState(powerState, readState);
-            }
+            PowerState readState = getPowerState(baseConfigMap);
 
             auto& sensorEntry = sensors[sensorName];
             sensorEntry = nullptr;
diff --git a/src/FanMain.cpp b/src/FanMain.cpp
index ff0ffaa..c55757c 100644
--- a/src/FanMain.cpp
+++ b/src/FanMain.cpp
@@ -405,17 +405,7 @@
                 redundancy = &systemRedundancy;
             }
 
-            PowerState powerState = PowerState::on;
-            auto findPower = baseConfiguration->second.find("PowerState");
-            if (findPower != baseConfiguration->second.end())
-            {
-                const auto* ptrPower =
-                    std::get_if<std::string>(&(findPower->second));
-                if (ptrPower != nullptr)
-                {
-                    setReadState(*ptrPower, powerState);
-                }
-            }
+            PowerState powerState = getPowerState(baseConfiguration->second);
 
             constexpr double defaultMaxReading = 25000;
             constexpr double defaultMinReading = 0;
diff --git a/src/HwmonTempMain.cpp b/src/HwmonTempMain.cpp
index 988e8b9..d1d50d6 100644
--- a/src/HwmonTempMain.cpp
+++ b/src/HwmonTempMain.cpp
@@ -400,14 +400,7 @@
                 }
             }
 
-            auto findPowerOn = baseConfigMap.find("PowerState");
-            PowerState readState = PowerState::always;
-            if (findPowerOn != baseConfigMap.end())
-            {
-                std::string powerState =
-                    std::visit(VariantToStringVisitor(), findPowerOn->second);
-                setReadState(powerState, readState);
-            }
+            PowerState readState = getPowerState(baseConfigMap);
 
             auto permitSet = getPermitSet(baseConfigMap);
             auto& sensor = sensors[sensorName];
diff --git a/src/IpmbSensor.cpp b/src/IpmbSensor.cpp
index 8d4dfaa..69c1e46 100644
--- a/src/IpmbSensor.cpp
+++ b/src/IpmbSensor.cpp
@@ -475,15 +475,7 @@
         offsetVal = std::visit(VariantToDoubleVisitor(), findOffsetVal->second);
     }
 
-    auto findPowerState = entry.find("PowerState");
-
-    if (findPowerState != entry.end())
-    {
-        std::string powerState =
-            std::visit(VariantToStringVisitor(), findPowerState->second);
-
-        setReadState(powerState, readState);
-    }
+    readState = getPowerState(entry);
 }
 
 void createSensors(
diff --git a/src/PSUSensorMain.cpp b/src/PSUSensorMain.cpp
index 1683e39..0e4deea 100644
--- a/src/PSUSensorMain.cpp
+++ b/src/PSUSensorMain.cpp
@@ -484,14 +484,7 @@
         checkGroupEvent(directory.string(), groupEventMatch,
                         groupEventPathList);
 
-        PowerState readState = PowerState::always;
-        auto findPowerOn = baseConfig->second.find("PowerState");
-        if (findPowerOn != baseConfig->second.end())
-        {
-            std::string powerState =
-                std::visit(VariantToStringVisitor(), findPowerOn->second);
-            setReadState(powerState, readState);
-        }
+        PowerState readState = getPowerState(baseConfig->second);
 
         /* Check if there are more sensors in the same interface */
         int i = 1;