Esempio n. 1
0
async def connect(
    config: conf.AppleTV,
    loop: asyncio.AbstractEventLoop,
    protocol: Protocol = None,
    session: aiohttp.ClientSession = None,
) -> interface.AppleTV:
    """Connect to a device based on a configuration."""
    if config.identifier is None:
        raise exceptions.DeviceIdMissingError("no device identifier")

    service = config.main_service(protocol=protocol)

    implementation = {Protocol.DMAP: DmapAppleTV, Protocol.MRP: MrpAppleTV}.get(
        service.protocol
    )

    if not implementation:
        raise exceptions.UnsupportedProtocolError(str(service.protocol))

    # AirPlay stream API is the same for both DMAP and MRP
    airplay = AirPlayStreamAPI(config, loop)

    atv = implementation(loop, await net.create_session(session), config, airplay)
    await atv.connect()
    return atv
Esempio n. 2
0
async def connect(config, loop, protocol=None, session=None):
    """Connect and logins to an Apple TV."""
    if config.identifier is None:
        raise exceptions.DeviceIdMissingError("no device identifier")

    service = config.main_service(protocol=protocol)

    supported_implementations = {
        Protocol.DMAP: DmapAppleTV,
        Protocol.MRP: MrpAppleTV,
    }

    implementation = supported_implementations.get(service.protocol, None)
    if not implementation:
        raise exceptions.UnsupportedProtocolError(str(service.protocol))

    # If no session is given, create a default one
    if session is None:
        session = await net.create_session(loop=loop)

    # AirPlay stream API is the same for both DMAP and MRP
    airplay = AirPlayStreamAPI(config, loop)

    atv = implementation(loop, session, config, airplay)
    await atv.connect()
    return atv
Esempio n. 3
0
async def pair(config, protocol, loop, session=None, **kwargs):
    """Pair with an Apple TV."""
    service = config.get_service(protocol)
    if not service:
        raise exceptions.NoServiceError('no service available for protocol ' +
                                        str(protocol))

    protocol_handlers = {
        Protocol.DMAP: DmapPairingHandler,
        Protocol.MRP: MrpPairingHandler,
        Protocol.AirPlay: AirPlayPairingHandler,
    }

    handler = protocol_handlers.get(protocol, None)
    if handler is None:
        raise exceptions.UnsupportedProtocolError(str(protocol))

    if session is None:
        session = await net.create_session(loop)

    return handler(config, session, loop, **kwargs)
Esempio n. 4
0
async def pair(config: conf.AppleTV,
               protocol: Protocol,
               loop: asyncio.AbstractEventLoop,
               session: aiohttp.ClientSession = None,
               **kwargs):
    """Pair a protocol for an Apple TV."""
    service = config.get_service(protocol)
    if not service:
        raise exceptions.NoServiceError("no service available for protocol " +
                                        str(protocol))

    handler = {
        Protocol.DMAP: DmapPairingHandler,
        Protocol.MRP: MrpPairingHandler,
        Protocol.AirPlay: AirPlayPairingHandler,
    }.get(protocol)

    if handler is None:
        raise exceptions.UnsupportedProtocolError(str(protocol))

    return handler(config, await net.create_session(session), loop, **kwargs)