Repository: JeffLIrion/python-androidtv (language: python)

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 / hints:
Same issue here.
Same issue here
Also affected. Any pointers to debug ? some log or other that ould help finding the source of the issue ? Thx

Fix patch:
diff --git a/androidtv/constants.py b/androidtv/constants.py
index 50d0c88..af1138b 100644
--- a/androidtv/constants.py
+++ b/androidtv/constants.py
@@ -94,7 +94,7 @@ class DeviceEnum(IntEnum):
 )
 #: 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


Test patch:
diff --git a/tests/test_constants.py b/tests/test_constants.py
index ec9a5d1..cc49d16 100644
--- a/tests/test_constants.py
+++ b/tests/test_constants.py
@@ -86,7 +86,7 @@ def test_constants(self):
         # 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 @@ def test_constants(self):
         # 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 @@ def test_constants(self):
         # 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