def test_parse_strings(): in_data = tags.string_tag("stra", "") + tags.string_tag( "strb", "test string") parsed = parser.parse(in_data, lookup_tag) assert 2 == len(parsed) assert "" == parser.first(parsed, "stra") assert "test string" == parser.first(parsed, "strb")
def test_parse_uint_of_various_lengths(): in_data = (tags.uint8_tag("uuu8", 12) + tags.uint16_tag("uu16", 37888) + tags.uint32_tag("uu32", 305419896) + tags.uint64_tag("uu64", 8982983289232)) parsed = parser.parse(in_data, lookup_tag) assert 4 == len(parsed) assert 12 == parser.first(parsed, "uuu8") assert 37888 == parser.first(parsed, "uu16") assert 305419896 == parser.first(parsed, "uu32") assert 8982983289232 == parser.first(parsed, "uu64")
def test_parse_value_in_container(): in_data = tags.container_tag( "cona", tags.uint8_tag("uuu8", 36) + tags.uint16_tag("uu16", 13000)) parsed = parser.parse(in_data, lookup_tag) assert 1 == len(parsed) inner = parser.first(parsed, "cona") assert 2 == len(inner) assert 36 == parser.first(inner, "uuu8") assert 13000 == parser.first(inner, "uu16")
async def test_succesful_pairing(mock_pairing): pairing, zeroconf, service = await mock_pairing() url = pairing_url(zeroconf, PAIRING_CODE) data, _ = await utils.simple_get(url) await pairing.finish() # Verify content returned in pairingresponse parsed = parser.parse(data, tag_definitions.lookup_tag) assert parser.first(parsed, "cmpa", "cmpg") == 1 assert parser.first(parsed, "cmpa", "cmnm") == REMOTE_NAME assert parser.first(parsed, "cmpa", "cmty") == "iPhone" assert service.credentials == PAIRING_GUID
def media_type() -> MediaType: """Type of media is currently playing, e.g. video, music.""" state = parser.first(playstatus, "cmst", "caps") if not state: return MediaType.Unknown mediakind = parser.first(playstatus, "cmst", "cmmk") if mediakind is not None: return daap.media_kind(mediakind) # Fallback: if artist or album exists we assume music (not present # for video) if artist() or album(): return MediaType.Music return MediaType.Video
async def perform_pairing(self, remote_name, expected_code, port): """Pair with a remote client. This will perform a GET-request to the specified port and hand over information to the client (pyatv) so that the pairing process can be completed. """ server = f"http://127.0.0.1:{port}" url = f"{server}/pair?pairingcode={expected_code}&servicename=test" data, _ = await utils.simple_get(url) # Verify content returned in pairingresponse parsed = parser.parse(data, tag_definitions.lookup_tag) assert parser.first(parsed, "cmpa", "cmpg") == 1 assert parser.first(parsed, "cmpa", "cmnm") == remote_name assert parser.first(parsed, "cmpa", "cmty") == "iPhone"
def _is_available(self, field: tuple, expected_value=None) -> FeatureState: if self.apple_tv.latest_playstatus: value = parser.first(self.apple_tv.latest_playstatus, *field) if value is not None: if not expected_value or expected_value == value: return FeatureState.Available return FeatureState.Unavailable
def test_parse_binary_plist(): data = {"key": "value"} in_data = tags.raw_tag("plst", plistlib.dumps(data, fmt=plistlib.FMT_BINARY)) parsed = parser.parse(in_data, lookup_tag) assert 1 == len(parsed) assert data, parser.first(parsed, "plst")
def shuffle() -> Optional[ShuffleState]: """If shuffle is enabled or not.""" state = parser.first(playstatus, "cmst", "cash") if state is None or state == 0: return ShuffleState.Off # DMAP does not support the "albums" state and will always report # "songs" if shuffle is active return ShuffleState.Songs
async def test_pair_custom_pairing_guid(mock_pairing): pairing, zeroconf, service = await mock_pairing(pin_code=PIN_CODE2, pairing_guid=PAIRING_GUID2) url = pairing_url(zeroconf, PAIRING_CODE2) data, _ = await utils.simple_get(url) await pairing.finish() # Verify content returned in pairingresponse parsed = parser.parse(data, tag_definitions.lookup_tag) assert parser.first(parsed, "cmpa", "cmpg") == int(PAIRING_GUID2, 16) assert service.credentials == PAIRING_GUID2
def handle_set_parameter(self, request: HttpRequest) -> Optional[HttpResponse]: """Handle incoming SET_PARAMETER request.""" _LOGGER.debug("Received SET_PARAMETER: %s", request) if request.headers["Content-Type"] == "application/x-dmap-tagged": tags = parser.parse(request.body, lookup_tag) self.state.metadata.title = parser.first(tags, "mlit", "minm") self.state.metadata.artist = parser.first(tags, "mlit", "asar") self.state.metadata.album = parser.first(tags, "mlit", "asal") elif request.body.startswith("volume:"): self.state.volume = float(request.body.split(" ", maxsplit=1)[1]) _LOGGER.debug("Changing volume to %f", self.state.volume) else: return HttpResponse( "RTSP", "1.0", 501, "Not implemented", {"CSeq": request.headers["CSeq"]}, b"", ) return HttpResponse("RTSP", "1.0", 200, "OK", {"CSeq": request.headers["CSeq"]}, b"")
async def playstatus(self, use_revision=False, timeout=None): """Request raw data about what is currently playing. If use_revision=True, this command will "block" until playstatus changes on the device. Must be logged in. """ cmd_url = _PSU_CMD.format(self.playstatus_revision if use_revision else 0) resp = await self.daap.get(cmd_url, timeout=timeout) self.playstatus_revision = parser.first(resp, "cmst", "cmsr") self.latest_playstatus = resp self.latest_playing = build_playing_instance(resp) self.latest_hash = self.latest_playing.hash return self.latest_playing
def _convert_button(self, data): value = parser.first(data, "cmbe") # Consider navigation buttons if six commands have been received if self.state.buttons_press_count == 6: if value == "touchUp&time=6&point=20,250": return "up" elif value == "touchUp&time=6&point=20,275": return "down" elif value == "touchUp&time=7&point=50,100": return "left" elif value == "touchUp&time=7&point=75,100": return "right" return value
async def login(self): """Login to Apple TV using specified login id.""" # Do not use session.get_data(...) in login as that would end up in # an infinite loop. def _login_request(): url = self._mkurl("login?[AUTH]&hasFP=1", session=False, login_id=True) _login_request.log_text = "Login request: " + url return self.http.get_data( url, headers=_DMAP_HEADERS, ) resp = await self._do(_login_request, is_login=True) self._session_id = parser.first(resp, "mlog", "mlid") _LOGGER.info("Logged in and got session id %s", self._session_id) return self._session_id
def test_ignore_value(): elem = tags.uint8_tag("igno", 44) parsed = parser.parse(elem, lookup_tag) assert parser.first(parsed, "igno") is None
def test_extract_simplified_container(): elem = tags.uint8_tag("uuu8", 12) inner = tags.container_tag("conb", elem) in_data = tags.container_tag("cona", inner) parsed = parser.parse(in_data, lookup_tag) assert 12 == parser.first(parsed, "cona", "conb", "uuu8")
def test_parse_bytes(): in_data = tags.raw_tag("byte", b"\x01\xAA\xFF\x45") parsed = parser.parse(in_data, lookup_tag) assert 1 == len(parsed) assert "0x01aaff45" == parser.first(parsed, "byte")
def album() -> Optional[str]: """Album of the currently playing song.""" return parser.first(playstatus, "cmst", "canl")
def _get_time_in_seconds(tag) -> int: time = parser.first(playstatus, "cmst", tag) return daap.ms_to_s(time)
def test_parse_bool(): in_data = tags.bool_tag("bola", True) + tags.bool_tag("bolb", False) parsed = parser.parse(in_data, lookup_tag) assert 2 == len(parsed) assert parser.first(parsed, "bola") assert not parser.first(parsed, "bolb")
def artist() -> Optional[str]: """Arist of the currently playing song.""" return parser.first(playstatus, "cmst", "cana")
def genre() -> Optional[str]: """Genre of the currently playing song.""" return parser.first(playstatus, "cmst", "cang")
def repeat() -> Optional[RepeatState]: """Repeat mode.""" state = parser.first(playstatus, "cmst", "carp") if state is None: return RepeatState.Off return RepeatState(state)
def device_state() -> DeviceState: """Device state, e.g. playing or paused.""" state = parser.first(playstatus, "cmst", "caps") return daap.playstate(state)
def title() -> Optional[str]: """Title of the current media, e.g. movie or song name.""" return parser.first(playstatus, "cmst", "cann")