psu-ng: Run validation when PSU is plugged

Subscribe to the Present inventory property so that when a PSU becomes
present (it's plugged into the system), it triggers the PSU validation
check so that the user can know if the new PSU(s) is supported on the
system instead of needing to issue a power on to run the validation.

Tested: At BMC Ready state, set the Present property on powersupply1 to
false, then true, and check that the validation ran every time the
Present property was set to true. Same when changing the Present
property for powersupply0.

Change-Id: I14dc7d5902871284c9c099e81b45e78e4abf83bc
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/phosphor-power-supply/psu_manager.cpp b/phosphor-power-supply/psu_manager.cpp
index 5a4af4b..6fa824a 100644
--- a/phosphor-power-supply/psu_manager.cpp
+++ b/phosphor-power-supply/psu_manager.cpp
@@ -177,6 +177,14 @@
         auto psu = std::make_unique<PowerSupply>(bus, invpath, *i2cbus,
                                                  *i2caddr, presline);
         psus.emplace_back(std::move(psu));
+
+        // Subscribe to power supply presence changes
+        auto presenceMatch = std::make_unique<sdbusplus::bus::match_t>(
+            bus,
+            sdbusplus::bus::match::rules::propertiesChanged(invpath,
+                                                            INVENTORY_IFACE),
+            [this](auto& msg) { this->presenceChanged(msg); });
+        presenceMatches.emplace_back(std::move(presenceMatch));
     }
 
     if (psus.empty())
@@ -349,6 +357,25 @@
     }
 }
 
+void PSUManager::presenceChanged(sdbusplus::message::message& msg)
+{
+    std::string msgSensor;
+    std::map<std::string, std::variant<uint32_t, bool>> msgData;
+    msg.read(msgSensor, msgData);
+
+    // Check if it was the Present property that changed.
+    auto valPropMap = msgData.find(PRESENT_PROP);
+    if (valPropMap != msgData.end())
+    {
+        if (std::get<bool>(valPropMap->second))
+        {
+            // A PSU became present, force the PSU validation to run.
+            runValidateConfig = true;
+            validationTimer->restartOnce(validationTimeout);
+        }
+    }
+}
+
 void PSUManager::createError(const std::string& faultName,
                              std::map<std::string, std::string>& additionalData)
 {