EntityManager: Introduce pruneDevice()

Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: Iac01b11acd19be915a6a428af31ead8b8c3d394b
diff --git a/src/EntityManager.cpp b/src/EntityManager.cpp
index f8144b3..141b0ba 100644
--- a/src/EntityManager.cpp
+++ b/src/EntityManager.cpp
@@ -852,6 +852,32 @@
     return *ptr == "On" || *ptr == "BiosPost";
 }
 
+static void pruneDevice(const nlohmann::json& systemConfiguration,
+                        const bool powerOff, const bool scannedPowerOff,
+                        const std::string& name, const nlohmann::json& device)
+{
+    if (systemConfiguration.contains(name))
+    {
+        return;
+    }
+
+    bool requirePowerOn = deviceRequiresPowerOn(device);
+
+    if (powerOff && requirePowerOn)
+    {
+        // power not on yet, don't know if it's there or not
+        return;
+    }
+
+    if (!powerOff && scannedPowerOff && requirePowerOn)
+    {
+        // already logged it when power was off
+        return;
+    }
+
+    logDeviceRemoved(device);
+}
+
 void startRemovedTimer(boost::asio::steady_timer& timer,
                        nlohmann::json& systemConfiguration)
 {
@@ -877,32 +903,16 @@
         [&systemConfiguration](const boost::system::error_code& ec) {
             if (ec == boost::asio::error::operation_aborted)
             {
-                // we were cancelled
                 return;
             }
 
             bool powerOff = !isPowerOn();
             for (const auto& [name, device] : lastJson.items())
             {
-                if (systemConfiguration.contains(name))
-                {
-                    continue;
-                }
-
-                bool requirePowerOn = deviceRequiresPowerOn(device);
-                if (powerOff && requirePowerOn)
-                {
-                    // power not on yet, don't know if it's there or not
-                    continue;
-                }
-                if (!powerOff && scannedPowerOff && requirePowerOn)
-                {
-                    // already logged it when power was off
-                    continue;
-                }
-
-                logDeviceRemoved(device);
+                pruneDevice(systemConfiguration, powerOff, scannedPowerOff,
+                            name, device);
             }
+
             scannedPowerOff = true;
             if (!powerOff)
             {