Skip to content

Commit

Permalink
helpers: Add is_device_supported
Browse files Browse the repository at this point in the history
Add helper method is_device_supported, returning a boolean indicating if
the device is supported by pyatv.
  • Loading branch information
postlund committed Jun 26, 2023
1 parent 9cbeeae commit 8ee9b61
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
16 changes: 15 additions & 1 deletion docs/api/pyatv/helpers.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ <h1>Index</h1>
<ul class="">
<li><code><a title="pyatv.helpers.auto_connect" href="#pyatv.helpers.auto_connect">auto_connect</a></code></li>
<li><code><a title="pyatv.helpers.get_unique_id" href="#pyatv.helpers.get_unique_id">get_unique_id</a></code></li>
<li><code><a title="pyatv.helpers.is_device_supported" href="#pyatv.helpers.is_device_supported">is_device_supported</a></code></li>
<li><code><a title="pyatv.helpers.is_streamable" href="#pyatv.helpers.is_streamable">is_streamable</a></code></li>
</ul>
</li>
Expand All @@ -30,7 +31,7 @@ <h1 class="title">Module <code>pyatv.helpers</code></h1>
</header>
<section id="section-intro">
<p>Various helper methods.</p>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/helpers.py#L1-L102" class="git-link">Browse git</a></div>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/helpers.py#L1-L122" class="git-link">Browse git</a></div>
</section>
<section>
</section>
Expand Down Expand Up @@ -66,6 +67,19 @@ <h2 class="section-title" id="header-functions">Functions</h2>
<p>The unique identifier is returned if available, otherwise <code>None</code> is returned.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/helpers.py#L54-L87" class="git-link">Browse git</a></div>
</dd>
<dt id="pyatv.helpers.is_device_supported">
<code class="name flex">
<span>def <span class="ident">is_device_supported</span></span>(<span>conf: <a title="pyatv.interface.BaseConfig" href="interface#pyatv.interface.BaseConfig">BaseConfig</a>) -> bool</span>
</code>
</dt>
<dd>
<section class="desc"><p>Return if pyatv supports this device.</p>
<p>This method will return False if all of its services are either
PairingRequirement.Unsupported or PairingRequirement.Disabled. In all other cases
it will return True. Do note that even if this method returns True, pairing (or
that existing credentials are provided) might still be needed.</p></section>
<div class="git-link-div"><a href="https://github.com/postlund/pyatv/blob/master/pyatv/helpers.py#L105-L122" class="git-link">Browse git</a></div>
</dd>
<dt id="pyatv.helpers.is_streamable">
<code class="name flex">
<span>async def <span class="ident">is_streamable</span></span>(<span>filename: str) -> bool</span>
Expand Down
20 changes: 20 additions & 0 deletions pyatv/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,23 @@ async def is_streamable(filename: str) -> bool:
except Exception:
return False
return True


def is_device_supported(conf: pyatv.interface.BaseConfig) -> bool:
"""Return if pyatv supports this device.
This method will return False if all of its services are either
PairingRequirement.Unsupported or PairingRequirement.Disabled. In all other cases
it will return True. Do note that even if this method returns True, pairing (or
that existing credentials are provided) might still be needed.
"""
# Gather a set of present pairing requirements, subtract unsupported requirements
# and check that we have something left.
dev_requirements = set(service.pairing for service in conf.services)
unsupported_requirements = set(
[
pyatv.const.PairingRequirement.Unsupported,
pyatv.const.PairingRequirement.Disabled,
]
)
return len(dev_requirements.difference(unsupported_requirements)) > 0
36 changes: 35 additions & 1 deletion tests/test_helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Functional tests for helper methods. Agnostic to protocol implementation."""

from ipaddress import IPv4Address
from unittest.mock import MagicMock, patch

import pytest

from pyatv import conf, helpers
from pyatv import conf, const, helpers

from tests.utils import data_path

Expand Down Expand Up @@ -101,3 +102,36 @@ def test_get_unique_id_(service_type, service_name, properties, expected_id):
)
async def test_is_streamable_supported_file(test_file, streamable):
assert await helpers.is_streamable(data_path(test_file)) == streamable


@pytest.mark.parametrize(
"pairing_requirement, is_supported",
[
(const.PairingRequirement.Unsupported, False),
(const.PairingRequirement.Disabled, False),
(const.PairingRequirement.NotNeeded, True),
(const.PairingRequirement.Optional, True),
(const.PairingRequirement.Mandatory, True),
],
)
def test_is_device_supported(pairing_requirement, is_supported):
dev = conf.AppleTV(IPv4Address("127.0.0.1"), "test")
dev.add_service(
conf.ManualService(
"unsupported",
const.Protocol.DMAP,
0,
{},
pairing_requirement=const.PairingRequirement.Unsupported,
)
)
dev.add_service(
conf.ManualService(
"test",
const.Protocol.AirPlay,
0,
{},
pairing_requirement=pairing_requirement,
)
)
assert helpers.is_device_supported(dev) == is_supported

0 comments on commit 8ee9b61

Please sign in to comment.