예제 #1
0
async def appstart(loop):  # pylint: disable=too-many-branches,too-many-statements
    """Script starts here."""
    parser = argparse.ArgumentParser()
    parser.add_argument("--local-ip", default="127.0.0.1", help="local IP address")
    parser.add_argument(
        "--demo", default=False, action="store_true", help="enable demo mode"
    )
    parser.add_argument(
        "-d", "--debug", default=False, action="store_true", help="enable debug logs"
    )

    protocols = parser.add_argument_group("protocols")
    protocols.add_argument(
        "--mrp", default=False, action="store_true", help="enable MRP protocol"
    )
    protocols.add_argument(
        "--dmap", default=False, action="store_true", help="enable DMAP protocol"
    )
    protocols.add_argument(
        "--airplay", default=False, action="store_true", help="enable AirPlay protocol"
    )
    protocols.add_argument(
        "--companion",
        default=False,
        action="store_true",
        help="enable Companion protocol",
    )
    protocols.add_argument(
        "--raop",
        default=False,
        action="store_true",
        help="enable RAOP protocol",
    )
    args = parser.parse_args()

    if not (args.mrp or args.dmap or args.airplay or args.companion or args.raop):
        parser.error("no protocol enabled (see --help)")

    level = logging.DEBUG if args.debug else logging.WARNING
    logging.basicConfig(
        level=level,
        stream=sys.stdout,
        datefmt="%Y-%m-%d %H:%M:%S",
        format="%(asctime)s %(levelname)s: %(message)s",
    )

    tasks = []
    unpublishers = []
    zconf = Zeroconf()
    fake_atv = FakeAppleTV(loop, test_mode=False)
    if args.mrp:
        _, usecase = fake_atv.add_service(Protocol.MRP)
        if args.demo:
            tasks.append(asyncio.ensure_future(_alter_playing(usecase)))

    if args.dmap:
        _, usecase = fake_atv.add_service(
            Protocol.DMAP, hsgid=HSGID, pairing_guid=PAIRING_GUID, session_id=SESSION_ID
        )
        if args.demo:
            tasks.append(asyncio.ensure_future(_alter_playing(usecase)))

    if args.airplay:
        fake_atv.add_service(Protocol.AirPlay)

    if args.companion:
        fake_atv.add_service(Protocol.Companion)

    if args.raop:
        fake_atv.add_service(Protocol.RAOP)

    await fake_atv.start()

    if args.mrp:
        unpublishers.append(
            await publish_mrp_zeroconf(
                loop, zconf, args.local_ip, fake_atv.get_port(Protocol.MRP)
            )
        )

    if args.dmap:
        unpublishers.append(
            await publish_dmap_zeroconf(
                loop, zconf, args.local_ip, fake_atv.get_port(Protocol.DMAP)
            )
        )

    if args.airplay:
        unpublishers.append(
            await publish_airplay_zeroconf(
                loop, zconf, args.local_ip, fake_atv.get_port(Protocol.AirPlay)
            )
        )

    if args.companion:
        unpublishers.append(
            await publish_companion_zeroconf(
                loop, zconf, args.local_ip, fake_atv.get_port(Protocol.Companion)
            )
        )

    if args.raop:
        unpublishers.append(
            await publish_raop_zeroconf(
                loop, zconf, args.local_ip, fake_atv.get_port(Protocol.RAOP)
            )
        )

    print("Press ENTER to quit")
    await loop.run_in_executor(None, sys.stdin.readline)

    await fake_atv.stop()

    for task in tasks:
        task.cancel()

    for unpublisher in unpublishers:
        await unpublisher()

    print("Exiting")

    return 0
예제 #2
0
class CompanionFunctionalTest(AioHTTPTestCase):
    async def setUpAsync(self):
        await super().setUpAsync()
        self.conf = AppleTV(IPv4Address("127.0.0.1"), "Test device")
        self.conf.add_service(
            MrpService("mrp_id", self.fake_atv.get_port(Protocol.MRP)))
        self.conf.add_service(
            CompanionService(
                self.fake_atv.get_port(Protocol.Companion),
                credentials=CLIENT_CREDENTIALS,
            ))
        self.atv = await self.get_connected_device()

    def tearDown(self):
        self.atv.close()
        super().tearDown()

    async def get_application(self, loop=None):
        self.fake_atv = FakeAppleTV(self.loop)
        self.fake_atv.add_service(Protocol.MRP)
        self.state, self.usecase = self.fake_atv.add_service(
            Protocol.Companion)
        return self.fake_atv.app

    async def get_connected_device(self):
        return await pyatv.connect(self.conf, loop=self.loop)

    @unittest_run_loop
    async def test_connect_only_companion(self):
        conf = AppleTV(IPv4Address("127.0.0.1"), "Test device")
        conf.add_service(
            CompanionService(self.fake_atv.get_port(Protocol.Companion)))

        with pytest.raises(exceptions.DeviceIdMissingError):
            await pyatv.connect(conf, loop=self.loop)

    @unittest_run_loop
    async def test_launch_app(self):
        await self.atv.apps.launch_app(TEST_APP)
        await until(lambda: self.state.active_app == TEST_APP)

    @unittest_run_loop
    async def test_app_list(self):
        self.usecase.set_installed_apps({
            TEST_APP: TEST_APP_NAME,
            TEST_APP2: TEST_APP_NAME2,
        })

        apps = await self.atv.apps.app_list()

        expected_apps = [
            App(TEST_APP_NAME, TEST_APP),
            App(TEST_APP_NAME2, TEST_APP2)
        ]
        assert not DeepDiff(expected_apps, apps)

    @unittest_run_loop
    async def test_features(self):
        assert (self.atv.features.get_feature(
            FeatureName.LaunchApp).state == FeatureState.Available)
        assert (self.atv.features.get_feature(
            FeatureName.AppList).state == FeatureState.Available)

    @unittest_run_loop
    async def test_power_functions(self):
        assert self.state.powered_on

        await self.atv.power.turn_off()
        assert not self.state.powered_on

        await self.atv.power.turn_on()
        assert self.state.powered_on

    @unittest_run_loop
    async def test_session_start(self):
        # All commands should trigger a session start, so just use one and verify
        assert self.state.sid == 0
        await self.atv.power.turn_off()
        assert self.state.sid != 0
        assert self.state.service_type == "com.apple.tvremoteservices"