How to use the pyatv.exceptions function in pyatv

To help you get started, we’ve selected a few pyatv examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github postlund / pyatv / tests / test_convert.py View on Github external
def test_unknown_playstate_throws(self):
        with self.assertRaises(exceptions.UnknownPlayState):
            convert.playstate(99999)
github postlund / pyatv / tests / test_types.py View on Github external
def test_unknown_playstate_throws(self):
        with self.assertRaises(exceptions.UnknownPlayState):
            convert.playstate(99999)
github postlund / hass-atv-beta / tests / apple_tv / test_config_flow.py View on Github external
async def test_user_pair_invalid_pin(flow, mrp_device, pairing_mock):
    """Test pairing with invalid pin."""
    pairing_mock.begin.return_value = mock_coro()
    pairing_mock.finish.side_effect = exceptions.PairingError()

    (await flow().step_user(identifier="MRP Device")).gives_form_confirm(
        description_placeholders={"name": "MRP Device"}
    )

    (await flow().step_confirm()).gives_form_pair_with_pin()

    (await flow().step_pair_with_pin(pin=1111)).gives_form_pair_with_pin(
        errors={"base": "auth"}
    )
github postlund / pyatv / tests / test_conf.py View on Github external
def test_main_service_no_service(self):
        with self.assertRaises(exceptions.NoServiceError):
            self.config.main_service()
github postlund / pyatv / tests / airplay / test_airplay.py View on Github external
async def test_play_video_no_permission(self):
        self.usecase.airplay_playback_playing_no_permission()

        with self.assertRaises(exceptions.NoCredentialsError):
            await self.player.play_url(STREAM, position=START_POSITION)
github postlund / hass-atv-beta / custom_components / apple_tv / __init__.py View on Github external
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
            )

        _LOGGER.debug(
            "Failed to find device %s with address %s, trying to scan",
            identifier,
            address,
        )

        atvs = await scan(self.hass.loop, identifier=identifier, protocol=protocol)
        if atvs:
            return atvs[0]

        self._update_state("Device not found, trying again later...")
        _LOGGER.debug("Failed to find device %s, trying later", identifier)
github postlund / pyatv / pyatv / conf.py View on Github external
def main_service(self, protocol=None):
        """Return suggested service used to establish connection."""
        protocols = [protocol] if protocol is not None else \
            [Protocol.MRP, Protocol.DMAP]

        for prot in protocols:
            service = self.get_service(prot)
            if service is not None:
                return service

        raise exceptions.NoServiceError(
            'no service to connect to')
github postlund / pyatv / pyatv / convert.py View on Github external
def media_kind(kind):
    """Convert iTunes media kind to API representation."""
    if kind in [1]:
        return MediaType.Unknown
    if kind in [3, 7, 11, 12, 13, 18, 32]:
        return MediaType.Video
    if kind in [2, 4, 10, 14, 17, 21, 36]:
        return MediaType.Music
    if kind in [8, 64]:
        return MediaType.TV

    raise exceptions.UnknownMediaKind('Unknown media kind: ' + str(kind))
github postlund / pyatv / pyatv / airplay / player.py View on Github external
headers=HEADERS,
                data=plistlib.dumps(body, fmt=plistlib.FMT_BINARY),
                timeout=TIMEOUT)

            # Sometimes AirPlay fails with "Internal Server Error", we
            # apply a "lets try again"-approach to that
            if status == 500:
                retry += 1
                _LOGGER.debug('Failed to stream %s, retry %d of %d',
                              url, retry, PLAY_RETRIES)
                await asyncio.sleep(1.0, loop=self.loop)
                continue

            # TODO: Should be more fine-grained
            if 400 <= status < 600:
                raise exceptions.AuthenticationError(
                    'Status code: ' + str(status))

            await self._wait_for_media_to_end()
            return

        raise exceptions.PlaybackError('Max retries exceeded')
github postlund / pyatv / examples / device_auth.py View on Github external
async def authenticate_with_device(atv):
    """Perform device authentication and print credentials."""
    credentials = await atv.airplay.generate_credentials()
    await atv.airplay.load_credentials(credentials)

    try:
        await atv.airplay.start_authentication()
        pin = input('PIN Code: ')
        await atv.airplay.finish_authentication(pin)
        print('Credentials: {0}'.format(credentials))

    except exceptions.DeviceAuthenticationError:
        print('Failed to authenticate', file=sys.stderr)