Repository: postlund/pyatv
Pull request #2099 (2023-07-13 07:22:38)

mrp: Fix pairing requirement when disabled
Relates to #1567

<a href="https://gitpod.io/#https://github.com/postlund/pyatv/pull/2099"><img src="https://gitpod.io/button/open-in-gitpod.svg"/></a>



codecov[bot]: ## [Codecov](https://app.codecov.io/gh/postlund/pyatv/pull/2099?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl) Report
Patch coverage: **`100.00`**% and project coverage change: **`-0.21`** :warning:
> Comparison is base [(`fef1667`)](https://app.codecov.io/gh/postlund/pyatv/commit/fef16675aa495772de5f50959ee1fc15f42c35af?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl) 88.85% compared to head [(`c34a0cc`)](https://app.codecov.io/gh/postlund/pyatv/pull/2099?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl) 88.64%.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #2099      +/-   ##
==========================================
- Coverage   88.85%   88.64%   -0.21%     
==========================================
  Files         162      162              
  Lines       10858    10862       +4     
==========================================
- Hits         9648     9629      -19     
- Misses       1210     1233      +23     
```


| [Impacted Files](https://app.codecov.io/gh/postlund/pyatv/pull/2099?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl) | Coverage Δ | |
|---|---|---|
| [pyatv/protocols/mrp/\_\_init\_\_.py](https://app.codecov.io/gh/postlund/pyatv/pull/2099?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl#diff-cHlhdHYvcHJvdG9jb2xzL21ycC9fX2luaXRfXy5weQ==) | `95.99% <100.00%> (+0.02%)` | :arrow_up: |

... and [4 files with indirect coverage changes](https://app.codecov.io/gh/postlund/pyatv/pull/2099/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/postlund/pyatv/pull/2099?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Pierre+St%C3%A5hl).


Patch:
diff --git a/pyatv/protocols/mrp/__init__.py b/pyatv/protocols/mrp/__init__.py
index 17e8c284..bb578cd0 100644
--- a/pyatv/protocols/mrp/__init__.py
+++ b/pyatv/protocols/mrp/__init__.py
@@ -1053,11 +1053,12 @@ async def service_info(
     Pairing has never been enforced by MRP (maybe by design), but it is
     possible to pair if AllowPairing is YES.
     """
-    service.pairing = (
-        PairingRequirement.Optional
-        if service.properties.get("allowpairing", "no").lower() == "yes"
-        else PairingRequirement.Disabled
-    )
+    if not service.enabled:
+        service.pairing = PairingRequirement.NotNeeded
+    elif service.properties.get("allowpairing", "no").lower() == "yes":
+        service.pairing = PairingRequirement.Optional
+    else:
+        service.pairing = PairingRequirement.Disabled
 
 
 def create_with_connection(  # pylint: disable=too-many-locals


Test patch:
diff --git a/tests/protocols/mrp/test_mrp.py b/tests/protocols/mrp/test_mrp.py
index 43e59e69..3032437c 100644
--- a/tests/protocols/mrp/test_mrp.py
+++ b/tests/protocols/mrp/test_mrp.py
@@ -99,3 +99,16 @@ async def test_service_info_pairing(airplay_props, mrp_props, pairing_req):
 
     assert mrp_service.pairing == pairing_req
     assert airplay_service.pairing == PairingRequirement.Unsupported
+
+
+@pytest.mark.asyncio
+async def test_disabled_service_no_pairing():
+    mrp_service = MutableService("mrp", Protocol.MRP, 0, {}, enabled=False)
+    airplay_service = MutableService("id", Protocol.AirPlay, 0, {"allowpairing": "no"})
+
+    await service_info(
+        mrp_service,
+        DeviceInfo({}),
+        {Protocol.MRP: mrp_service, Protocol.AirPlay: airplay_service},
+    )
+    assert mrp_service.pairing == PairingRequirement.NotNeeded