PowerStatusMonitor: create DBus match in ctor

On request [1], create this change to create the DBus match for our
power status monitor in its constructor.

We get rid of the invalid state of the match not existing and can drop a
check for it.

We can also drop a call in 'main' function previously used to initialize
the match. The public API of this class is simplified.

Tested: Inspection only.

References:
[1] https://gerrit.openbmc.org/c/openbmc/entity-manager/+/81483/comment/858d95be_e2f9f6b8/

Change-Id: Ib05941a90ba2520b25e2c2b1e9a1e325caa722ae
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/src/entity_manager/entity_manager.cpp b/src/entity_manager/entity_manager.cpp
index 83240be..09cc7ba 100644
--- a/src/entity_manager/entity_manager.cpp
+++ b/src/entity_manager/entity_manager.cpp
@@ -73,7 +73,7 @@
     objServer(sdbusplus::asio::object_server(systemBus, /*skipManager=*/true)),
     lastJson(nlohmann::json::object()),
     systemConfiguration(nlohmann::json::object()), io(io),
-    propertiesChangedTimer(io)
+    powerStatus(*systemBus), propertiesChangedTimer(io)
 {
     // All other objects that EntityManager currently support are under the
     // inventory subtree.
diff --git a/src/entity_manager/main.cpp b/src/entity_manager/main.cpp
index efcb4d0..6a98279 100644
--- a/src/entity_manager/main.cpp
+++ b/src/entity_manager/main.cpp
@@ -34,10 +34,6 @@
 
     em.handleCurrentConfigurationJson();
 
-    // some boards only show up after power is on, we want to not say they are
-    // removed until the same state happens
-    em.powerStatus.setupPowerMatch(em.systemBus);
-
     io.run();
 
     return 0;
diff --git a/src/entity_manager/power_status_monitor.cpp b/src/entity_manager/power_status_monitor.cpp
index 93617d2..b281602 100644
--- a/src/entity_manager/power_status_monitor.cpp
+++ b/src/entity_manager/power_status_monitor.cpp
@@ -15,12 +15,21 @@
 const static constexpr char* path = "/xyz/openbmc_project/state/host0";
 const static constexpr char* property = "CurrentHostState";
 
-bool PowerStatusMonitor::isPowerOn()
+PowerStatusMonitor::PowerStatusMonitor(sdbusplus::asio::connection& conn) :
+
+    powerMatch(static_cast<sdbusplus::bus_t&>(conn),
+               "type='signal',interface='" +
+                   std::string(em_utils::properties::interface) + "',path='" +
+                   std::string(power::path) + "',arg0='" +
+                   std::string(power::interface) + "'",
+               std::bind_front(&PowerStatusMonitor::handlePowerMatch, this))
+
 {
-    if (!powerMatch)
-    {
-        throw std::runtime_error("Power Match Not Created");
-    }
+    getInitialPowerStatus(conn);
+}
+
+bool PowerStatusMonitor::isPowerOn() const
+{
     return powerStatusOn;
 }
 
@@ -39,18 +48,10 @@
     }
 }
 
-void PowerStatusMonitor::setupPowerMatch(
-    const std::shared_ptr<sdbusplus::asio::connection>& conn)
+void PowerStatusMonitor::getInitialPowerStatus(
+    sdbusplus::asio::connection& conn)
 {
-    powerMatch = std::make_unique<sdbusplus::bus::match_t>(
-        static_cast<sdbusplus::bus_t&>(*conn),
-        "type='signal',interface='" +
-            std::string(em_utils::properties::interface) + "',path='" +
-            std::string(power::path) + "',arg0='" +
-            std::string(power::interface) + "'",
-        std::bind_front(&PowerStatusMonitor::handlePowerMatch, this));
-
-    conn->async_method_call(
+    conn.async_method_call(
         [this](boost::system::error_code ec,
                const std::variant<std::string>& state) {
             if (ec)
diff --git a/src/entity_manager/power_status_monitor.hpp b/src/entity_manager/power_status_monitor.hpp
index 0955607..b6fd272 100644
--- a/src/entity_manager/power_status_monitor.hpp
+++ b/src/entity_manager/power_status_monitor.hpp
@@ -9,15 +9,16 @@
 class PowerStatusMonitor
 {
   public:
-    bool isPowerOn();
-    void setupPowerMatch(
-        const std::shared_ptr<sdbusplus::asio::connection>& conn);
+    explicit PowerStatusMonitor(sdbusplus::asio::connection& conn);
+
+    bool isPowerOn() const;
 
   private:
     void handlePowerMatch(sdbusplus::message_t& message);
+    void getInitialPowerStatus(sdbusplus::asio::connection& conn);
 
     bool powerStatusOn = false;
-    std::unique_ptr<sdbusplus::bus::match_t> powerMatch = nullptr;
+    sdbusplus::bus::match_t powerMatch;
 };
 
 } // namespace power