psu-ng: Do not create duplicate power supplies

When the entity-manager is restarted, the interfaces watched for the
power supply data will get re-added. If the data on the added interfaces
matches an already existing power supply, skip creating the power supply
object.

Change-Id: Iccd56d842e808dcd5c192d5cc2dc1947fbdb03f6
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/phosphor-power-supply/psu_manager.cpp b/phosphor-power-supply/psu_manager.cpp
index 704020e..93d1415 100644
--- a/phosphor-power-supply/psu_manager.cpp
+++ b/phosphor-power-supply/psu_manager.cpp
@@ -6,6 +6,8 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#include <regex>
+
 using namespace phosphor::logging;
 
 namespace phosphor::power::manager
@@ -141,6 +143,21 @@
             presline = *preslineptr;
         }
 
+        auto invMatch =
+            std::find_if(psus.begin(), psus.end(), [&invpath](auto& psu) {
+                return psu->getInventoryPath() == invpath;
+            });
+        if (invMatch != psus.end())
+        {
+            // This power supply has the same inventory path as the one with
+            // information just added to D-Bus.
+            // Changes to GPIO line name unlikely, so skip checking.
+            // Changes to the I2C bus and address unlikely, as that would
+            // require corresponding device tree updates.
+            // Return out to avoid duplicate object creation.
+            return;
+        }
+
         log<level::DEBUG>(
             fmt::format("make PowerSupply bus: {} addr: {} presline: {}",
                         *i2cbus, *i2caddr, presline)