postlund / pyatv

A client library for Apple TV and AirPlay devices
https://pyatv.dev
MIT License
875 stars 95 forks source link

Rogue Airplay service causes AppleTV integration to fail #1747

Open sj7trunks opened 2 years ago

sj7trunks commented 2 years ago

Describe the bug

Home Assistant Apple TV integrations would stop or act as if devices didn't exist. Deleting devices caused devices not to come back. After checking logs and tracing to pyatv and then 'Record It' app running on a macbook Air, disabling application caused all services to come back fine.

Error log

Error in Home Assistant

2022-06-06 19:19:05 ERROR (MainThread) [homeassistant.components.apple_tv.config_flow] Unexpected exception
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/config_flow.py", line 273, in async_find_device_wrapper
    await self.async_find_device(allow_exist)
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/config_flow.py", line 286, in async_find_device
    self.atv, self.atv_identifiers = await device_scan(
  File "/usr/src/homeassistant/homeassistant/components/apple_tv/config_flow.py", line 58, in device_scan
    scan_result = await scan(loop, timeout=3, hosts=_host_filter(), aiozc=aiozc)
  File "/usr/local/lib/python3.9/site-packages/pyatv/__init__.py", line 86, in scan
    devices = (await scanner.discover(timeout)).values()
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/scan.py", line 142, in discover
    await self.process(timeout)
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/scan.py", line 434, in process
    services_by_address, name_to_model = await self._lookup_services_and_models(
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/scan.py", line 394, in _lookup_services_and_models
    infos = self._build_service_info_queries()
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/scan.py", line 381, in _build_service_info_queries
    device_name = self._device_info_name[type_](name)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/raop/__init__.py", line 445, in raop_name_from_service_name
    return service_name.split("@", maxsplit=1)[1]
IndexError: list index out of range

Error that shows up on the top of pyatv

bash-5.1# atvremote scan
2022-06-06 19:21:31 ERROR [pyatv.core.scan]: Failed to parse service: Service(type='_raop._tcp.local', name='Record It (somehosthere)', address=IPv4Address('192.168.1.134'), port=5001, properties={'et': '0,3,5', 'da': 'true', 'ek': '1', 'sf': '0x4', 'tp': 'UDP', 'pw': 'false', 'am': 'AppleTV3,2', 'cn': '0,1,3', 'txtvers': '1', 'vn': '3', 'md': '0,1,2', 'vs': '220.68', 'sv': 'false', 'ch': '2', 'sm': 'false', 'sr': '44100', 'pk': '450b7ae3d6d1bce477676da61428c3c20bb9ef7e9f4b4cb8834075083cdbd2ea', 'ss': '16'})
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/scan.py", line 188, in handle_response
    self._service_discovered(service, response)
  File "/usr/local/lib/python3.9/site-packages/pyatv/core/scan.py", line 198, in _service_discovered
    result = self._services[service.type][0](service, response)
  File "/usr/local/lib/python3.9/site-packages/pyatv/protocols/raop/__init__.py", line 433, in raop_service_handler
    _, name = mdns_service.name.split("@", maxsplit=1)
ValueError: not enough values to unpack (expected 2, got 1)

How to reproduce the bug?

Install Record It, run pyatv scan

What is expected behavior?

Scan works without issue

Operating System

MacOS

Python

3.9

pyatv

0.8.2

Device

Apple TV

Additional context

N/A

postlund commented 2 years ago

To me that looks like an invalid name, but I'm not entirely sure what the specification actually mandate. I can most likely use the public key (pk attribute) as unique identifier in this case so I can work around this issue. Will try to sort it out later today or tomorrow.

postlund commented 1 year ago

If you still see this problem, can you please attach the output of atvremote --debug scan?