Repository: JeffLIrion/python-androidtv

Issue:
Android Debug Bridge (AndroidTV) app_id frequently shows "Failed to write while dumping service window: Broken pipe
### The problem

I have an automation (below) that triggers based on the app_Id from the device via Android Debug Bridge. Since Home Assistant 
/2023.10.4, the platform state attributes often responds with:

```
app_id: Failed to write while dumping service window: Broken pipe
app_name: Failed to write while dumping service window: Broken pipe
source: Failed to write while dumping service window: Broken pipe
adb_response: null
```

This causes automations to trigger based on the state change away from the expected values (testing for `com.valvesoftware.steamlink`).


### Additional information

A few other users have reported this via the Home Assistant integrations github - https://github.com/home-assistant/core/issues/104652



Discussion:

Patch:
diff --git a/androidtv/basetv/basetv.py b/androidtv/basetv/basetv.py
index 1d702ec..47d82eb 100644
--- a/androidtv/basetv/basetv.py
+++ b/androidtv/basetv/basetv.py
@@ -323,10 +323,7 @@ class BaseTV(object):  # pylint: disable=too-few-public-methods
         if constants.CUSTOM_RUNNING_APPS in self._custom_commands:
             return self._custom_commands[constants.CUSTOM_RUNNING_APPS]
 
-        if self.DEVICE_ENUM == constants.DeviceEnum.FIRETV:
-            return constants.CMD_RUNNING_APPS_FIRETV
-
-        return constants.CMD_RUNNING_APPS_ANDROIDTV
+        return constants.CMD_RUNNING_APPS
 
     def _cmd_turn_off(self):
         """Get the command used to turn off this device.
diff --git a/androidtv/constants.py b/androidtv/constants.py
index 50d0c88..655f068 100644
--- a/androidtv/constants.py
+++ b/androidtv/constants.py
@@ -94,7 +94,7 @@ CMD_DEFINE_CURRENT_APP_VARIABLE = (
 )
 #: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 11 device
 CMD_DEFINE_CURRENT_APP_VARIABLE11 = (
-    "CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && " + CMD_PARSE_CURRENT_APP11
+    "CURRENT_APP=$(dumpsys window windows | grep -E 'mInputMethod(Input)?Target') && " + CMD_PARSE_CURRENT_APP11
 )
 
 #: Assign focused application identifier to ``CURRENT_APP`` variable for an Android 12 device
@@ -204,11 +204,8 @@ CMD_CURRENT_APP_MEDIA_SESSION_STATE13 = CMD_CURRENT_APP13 + " && " + CMD_MEDIA_S
 #: Determine the current app and get the state from ``dumpsys media_session`` for a Google TV device
 CMD_CURRENT_APP_MEDIA_SESSION_STATE_GOOGLE_TV = CMD_CURRENT_APP_GOOGLE_TV + " && " + CMD_MEDIA_SESSION_STATE
 
-#: Get the running apps for an Android TV device
-CMD_RUNNING_APPS_ANDROIDTV = "ps -A | grep u0_a"
-
-#: Get the running apps for a Fire TV device
-CMD_RUNNING_APPS_FIRETV = "ps | grep u0_a"
+#: Get the running apps for an Android/Fire TV device
+CMD_RUNNING_APPS = "ps -A | grep u0_a"
 
 #: Get installed apps
 CMD_INSTALLED_APPS = "pm list packages"


Test patch:
diff --git a/tests/test_constants.py b/tests/test_constants.py
index ec9a5d1..b2bf790 100644
--- a/tests/test_constants.py
+++ b/tests/test_constants.py
@@ -86,7 +86,7 @@ class TestConstants(unittest.TestCase):
         # CMD_CURRENT_APP11
         self.assertCommand(
             constants.CMD_CURRENT_APP11,
-            r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP",
+            r"CURRENT_APP=$(dumpsys window windows | grep -E 'mInputMethod(Input)?Target') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP",
         )
 
         # CMD_CURRENT_APP12
@@ -116,7 +116,7 @@ class TestConstants(unittest.TestCase):
         # CMD_CURRENT_APP_MEDIA_SESSION_STATE11
         self.assertCommand(
             constants.CMD_CURRENT_APP_MEDIA_SESSION_STATE11,
-            r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP && dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'",
+            r"CURRENT_APP=$(dumpsys window windows | grep -E 'mInputMethod(Input)?Target') && CURRENT_APP=${CURRENT_APP%%/*} && CURRENT_APP=${CURRENT_APP##* } && echo $CURRENT_APP && dumpsys media_session | grep -A 100 'Sessions Stack' | grep -A 100 $CURRENT_APP | grep -m 1 'state=PlaybackState {'",
         )
 
         # CMD_CURRENT_APP_MEDIA_SESSION_STATE12
@@ -167,7 +167,7 @@ class TestConstants(unittest.TestCase):
         # CMD_LAUNCH_APP11
         self.assertCommand(
             constants.CMD_LAUNCH_APP11,
-            r"CURRENT_APP=$(dumpsys window windows | grep -E -m 1 'mInputMethod(Input)?Target') && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP##* }} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c android.intent.category.LEANBACK_LAUNCHER --pct-syskeys 0 1; fi",
+            r"CURRENT_APP=$(dumpsys window windows | grep -E 'mInputMethod(Input)?Target') && CURRENT_APP=${{CURRENT_APP%%/*}} && CURRENT_APP=${{CURRENT_APP##* }} && if [ $CURRENT_APP != '{0}' ]; then monkey -p {0} -c android.intent.category.LEANBACK_LAUNCHER --pct-syskeys 0 1; fi",
         )
 
         # CMD_LAUNCH_APP12
@@ -212,11 +212,8 @@ class TestConstants(unittest.TestCase):
         # CMD_MODEL
         self.assertCommand(constants.CMD_MODEL, r"getprop ro.product.model")
 
-        # CMD_RUNNING_APPS_ANDROIDTV
-        self.assertCommand(constants.CMD_RUNNING_APPS_ANDROIDTV, r"ps -A | grep u0_a")
-
-        # CMD_RUNNING_APPS_FIRETV
-        self.assertCommand(constants.CMD_RUNNING_APPS_FIRETV, r"ps | grep u0_a")
+        # CMD_RUNNING_APPS
+        self.assertCommand(constants.CMD_RUNNING_APPS, r"ps -A | grep u0_a")
 
         # CMD_SCREEN_ON
         self.assertCommand(