Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(await flow().step_confirm()).gives_form_pair_with_pin(
description_placeholders={"protocol": "MRP"}
)
(await flow().step_pair_with_pin(pin=1111)).gives_form_pair_no_pin(
description_placeholders={"protocol": "DMAP", "pin": 1111}
)
(await flow().step_pair_no_pin()).gives_form_pair_with_pin(
description_placeholders={"protocol": "AirPlay"}
)
(await flow().step_pair_with_pin(pin=1234)).gives_create_entry(
{
"address": "127.0.0.1",
"protocol": Protocol.MRP.value,
"name": "MRP Device",
"credentials": {
Protocol.MRP.value: "mrp_creds",
Protocol.DMAP.value: "dmap_creds",
Protocol.AirPlay.value: "airplay_creds",
},
},
unique_id="mrp_id",
)
async def _autodiscover_device(args, loop):
apple_tv = await _scan_for_device(
args, args.scan_timeout, loop, protocol=args.protocol)
if not apple_tv:
return None
def _set_credentials(protocol, field):
service = apple_tv.get_service(protocol)
if service:
value = service.credentials or getattr(args, field)
setattr(args, field, value)
_set_credentials(Protocol.DMAP, 'dmap_credentials')
_set_credentials(Protocol.MRP, 'mrp_credentials')
_set_credentials(Protocol.AirPlay, 'airplay_credentials')
logging.info('Auto-discovered %s at %s', args.name, args.address)
return apple_tv
async def _scan(self):
identifier = self.config_entry.unique_id
address = self.config_entry.data[CONF_ADDRESS]
protocol = Protocol(self.config_entry.data[CONF_PROTOCOL])
self._update_state(message="Discovering device...")
try:
atvs = await scan(
self.hass.loop,
identifier=identifier,
protocol=protocol,
hosts=[address],
)
if atvs:
return atvs[0]
except exceptions.NonLocalSubnetError:
_LOGGER.debug(
"Address %s is on non-local subnet, relying on regular scan", address
)
def _manual_device(args):
config = AppleTV(args.address, args.name)
if args.dmap_credentials or args.protocol == const.Protocol.DMAP:
config.add_service(DmapService(
args.id, args.dmap_credentials, port=args.port))
if args.mrp_credentials or args.protocol == const.Protocol.MRP:
config.add_service(MrpService(
args.id, args.port, credentials=args.mrp_credentials))
if args.airplay_credentials:
config.add_service(AirPlayService(
args.id, credentials=args.airplay_credentials))
return config
def get_service(self, protocol):
"""Look up a service based on protocol.
If a service with the specified protocol is not available, None is
returned.
"""
# Special case for AirPlay for now
if protocol == Protocol.AirPlay:
if self._services.get(protocol, None) is None:
self._services[protocol] = AirPlayService(None, 7000)
return self._services.get(protocol, None)
async def _connect(self, conf):
credentials = self.config_entry.data[CONF_CREDENTIALS]
session = async_get_clientsession(self.hass)
for protocol, creds in credentials.items():
conf.set_credentials(Protocol(int(protocol)), creds)
self._update_state("Connecting to device...")
self.atv = await connect(conf, self.hass.loop, session=session)
self.atv.listener = self
self._update_state("Connected, waiting for update...", connected=True)
self.atv.push_updater.start()
self.address_updated(str(conf.address))
await self._setup_device_registry()
self._connection_attempts = 0
if self._connection_was_lost:
_LOGGER.info(
'Connection was re-established to Apple TV "%s"',