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 connect(
    config: interface.BaseConfig,
    loop: asyncio.AbstractEventLoop,
    protocol: Protocol = None,
    session: aiohttp.ClientSession = None,
) -> interface.AppleTV:
    """Connect to a device based on a configuration."""
    if not config.services:
        raise exceptions.NoServiceError("no service to connect to")

    if config.identifier is None:
        raise exceptions.DeviceIdMissingError("no device identifier")

    config_copy = deepcopy(config)
    session_manager = await http.create_session(session)
    core_dispatcher = CoreStateDispatcher()
    atv = FacadeAppleTV(config_copy, session_manager, core_dispatcher)

    try:
        for proto, proto_methods in PROTOCOLS.items():
            service = config_copy.get_service(proto)
            if service is None or not service.enabled:
                continue
            if not service.enabled:
                _LOGGER.debug("Ignore %s as it is disabled", proto.name)
                continue

            # Lock protocol argument so protocol does not have to deal
            # with that
            takeover_method = partial(atv.takeover, proto)

            # Core provides core access with a protocol specific twist
            core = Core(
                loop,
                config_copy,
                service,
                atv,
                session_manager,
                takeover_method,
                ProtocolStateDispatcher(proto, core_dispatcher),
            )

            for setup_data in proto_methods.setup(core):
                atv.add_protocol(setup_data)

        await atv.connect()
    except Exception:
        await session_manager.close()
        raise
    else:
        return atv
Esempio n. 4
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 not config.services:
        raise exceptions.NoServiceError("no service to connect to")

    if config.identifier is None:
        raise exceptions.DeviceIdMissingError("no device identifier")

    session_manager = await http.create_session(session)
    atv = FacadeAppleTV(config, session_manager)

    for service in config.services:
        setup_method = _PROTOCOL_IMPLEMENTATIONS.get(service.protocol)
        if not setup_method:
            raise RuntimeError(
                "missing implementation for protocol {service.protocol}")

        setup_data = setup_method(loop, config, atv.interfaces, atv,
                                  session_manager)
        if setup_data:
            _LOGGER.debug("Adding protocol %s", service.protocol)
            atv.add_protocol(service.protocol, setup_data)
        else:
            _LOGGER.debug("Not adding protocol: %s", service.protocol)
    try:
        await atv.connect()
    except Exception:
        await session_manager.close()
        raise
    else:
        return atv