async def mrp_protocol(event_loop, mrp_atv): port = mrp_atv.get_port(Protocol.MRP) service = ManualService("mrp_id", Protocol.MRP, port, {}) connection = MrpConnection("127.0.0.1", port, event_loop) protocol = MrpProtocol(connection, SRPAuthHandler(), service) yield protocol protocol.stop()
def __init__(self, config: BaseConfig, service: BaseService, session_manager: ClientSessionManager, loop: asyncio.AbstractEventLoop, **kwargs): """Initialize a new MrpPairingHandler.""" super().__init__(session_manager, service) self.connection = MrpConnection(config.address, self.service.port, loop) self.srp = SRPAuthHandler() self.protocol = MrpProtocol(self.connection, self.srp, self.service) self.pairing_procedure = MrpPairSetupProcedure(self.protocol, self.srp) self.pin_code = None self._has_paired = False
def __init__(self, config: BaseConfig, service: BaseService, session: ClientSessionManager, loop: asyncio.AbstractEventLoop, **kwargs): """Initialize a new CompanionPairingHandler.""" super().__init__(session, service) self.connection = CompanionConnection(loop, str(config.address), self.service.port, None) self.srp = SRPAuthHandler() self.protocol = CompanionProtocol(self.connection, self.srp, self.service) self.pairing_procedure = CompanionPairSetupProcedure( self.protocol, self.srp) self.pin_code: Optional[str] = None self._has_paired: bool = False
def __init__(self, loop, address, port, credentials): """Initialize a new instance of ProxyMrpAppleTV.""" super().__init__(DEVICE_NAME) self.loop = loop self.buffer = b"" self.transport = None self.chacha = None self.connection = MrpConnection(address, port, self.loop) self.protocol = MrpProtocol( self.connection, SRPAuthHandler(), MutableService(None, Protocol.MRP, port, {}, credentials=credentials), )
def pair_setup( auth_type: AuthenticationType, connection: HttpConnection ) -> PairSetupProcedure: """Return procedure object used for Pair-Setup.""" _LOGGER.debug("Setting up new AirPlay Pair-Setup procedure with type %s", auth_type) if auth_type == AuthenticationType.Legacy: srp = LegacySRPAuthHandler(new_credentials()) srp.initialize() return AirPlayLegacyPairSetupProcedure(connection, srp) if auth_type == AuthenticationType.HAP: srp = SRPAuthHandler() srp.initialize() return AirPlayHapPairSetupProcedure(connection, srp) raise exceptions.NotSupportedError( f"authentication type {auth_type} does not support Pair-Setup" )
async def connect(self): """Connect to remote host.""" if self._protocol: return _LOGGER.debug("Connect to Companion from API") self._connection = CompanionConnection( self.core.loop, str(self.core.config.address), self.core.service.port, self.core.device_listener, ) self._protocol = CompanionProtocol(self._connection, SRPAuthHandler(), self.core.service) self._protocol.listener = self await self._protocol.start() await self.system_info() await self._session_start() await self.subscribe_event("_iMC")
def pair_verify( credentials: HapCredentials, connection: HttpConnection ) -> PairVerifyProcedure: """Return procedure object used for Pair-Verify.""" _LOGGER.debug( "Setting up new AirPlay Pair-Verify procedure with type %s", credentials.type ) if credentials.type == AuthenticationType.Null: return NullPairVerifyProcedure() if credentials.type == AuthenticationType.Legacy: srp = LegacySRPAuthHandler(credentials) srp.initialize() return AirPlayLegacyPairVerifyProcedure(connection, srp) srp = SRPAuthHandler() srp.initialize() if credentials.type == AuthenticationType.HAP: return AirPlayHapPairVerifyProcedure(connection, srp, credentials) return AirPlayHapTransientPairVerifyProcedure(connection, srp)
def __init__(self, loop: asyncio.AbstractEventLoop, address: str, port: int, credentials: str) -> None: """Initialize a new instance of CompanionAppleTVProxy.""" super().__init__(DEVICE_NAME) self.loop = loop self.buffer: bytes = b"" self.transport = None self.chacha: Optional[chacha20.Chacha20Cipher] = None self.connection: Optional[CompanionConnection] = CompanionConnection( self.loop, address, port) self.protocol: CompanionProtocol = CompanionProtocol( self.connection, SRPAuthHandler(), MutableService(None, Protocol.Companion, port, {}, credentials=credentials), ) self._receive_event: asyncio.Event = asyncio.Event() self._receive_task: Optional[asyncio.Future] = None
def create_with_connection( # pylint: disable=too-many-locals core: Core, connection: AbstractMrpConnection, requires_heatbeat: bool = True, ) -> SetupData: """Set up a new MRP service from a connection.""" protocol = MrpProtocol(connection, SRPAuthHandler(), core.service) psm = PlayerStateManager(protocol) remote_control = MrpRemoteControl(core.loop, psm, protocol) metadata = MrpMetadata(protocol, psm, core.config.identifier) power = MrpPower(core.loop, protocol, remote_control) push_updater = MrpPushUpdater(metadata, psm, core.state_dispatcher) audio = MrpAudio(protocol, core.state_dispatcher) interfaces = { RemoteControl: remote_control, Metadata: metadata, Power: power, PushUpdater: push_updater, Features: MrpFeatures(core.config, psm, audio), Audio: audio, } async def _connect() -> bool: await protocol.start() if requires_heatbeat: protocol.enable_heartbeat() return True def _close() -> Set[asyncio.Task]: push_updater.stop() protocol.stop() return set() def _device_info() -> Dict[str, Any]: devinfo = device_info(list(scan().keys())[0], core.service.properties) # Extract build number from DEVICE_INFO_MESSAGE from device if protocol.device_info: info = protocol.device_info.inner() # type: ignore devinfo[DeviceInfo.BUILD_NUMBER] = info.systemBuildVersion if info.modelID: devinfo[DeviceInfo.RAW_MODEL] = info.modelID devinfo[DeviceInfo.MODEL] = lookup_model(info.modelID) return devinfo # Features managed by this protocol features = set([ FeatureName.Artwork, FeatureName.VolumeDown, FeatureName.VolumeUp, FeatureName.SetVolume, FeatureName.Volume, FeatureName.App, ]) features.update(_FEATURES_SUPPORTED) features.update(_FEATURE_COMMAND_MAP.keys()) features.update(_FIELD_FEATURES.keys()) return SetupData(Protocol.MRP, _connect, _close, _device_info, interfaces, features)