home-assistant-libs / pychromecast

Library for Python 3 to communicate with the Google Chromecast.
MIT License
2.54k stars 381 forks source link

UnicodeError: label too long #170

Open JesseWebDotCom opened 7 years ago

JesseWebDotCom commented 7 years ago

Reproduced the error with brand new, minimal HA OS X install (version 0.43.0). I have been experiencing this issue for many months on my pi3 but am using OS X to help give you a clean environment where I can implement any debugging you require.

I factory reset all my chrome cast devices and configured only 1 (named "LivingRoom"). I used a minimal HA config with discovery off and only:

media_player:
  - platform: cast
    name: Living Room
    host: 192.168.1.72

(I've also tested with discovery on)

HA developer tools | about displays:

The following errors have been logged this session:

17-04-22 16:57:52 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform cast
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/idna.py", line 167, in encode
    raise UnicodeError("label too long")
UnicodeError: label too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/homeassistant/helpers/entity_component.py", line 155, in _async_setup_platform
    entity_platform.schedule_add_entities, discovery_info
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/jessewebdotcom/.homeassistant/custom_components/media_player/cast.py", line 73, in setup_platform
    all_chromecasts = pychromecast.get_chromecasts()
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/__init__.py", line 81, in get_chromecasts
    blocking=blocking))
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/__init__.py", line 48, in _get_chromecast_from_host
    blocking=blocking)
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/__init__.py", line 142, in __init__
    dev_status = get_device_status(self.host)
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/dial.py", line 49, in get_device_status
    timeout=10)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 479, in request
    prep.url, proxies, stream, verify, cert
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 654, in merge_environment_settings
    env_proxies = get_environ_proxies(url) or {}
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/utils.py", line 617, in get_environ_proxies
    if should_bypass_proxies(url):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/utils.py", line 601, in should_bypass_proxies
    bypass = proxy_bypass(netloc)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 2616, in proxy_bypass
    return proxy_bypass_macosx_sysconf(host)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 2593, in proxy_bypass_macosx_sysconf
    return _proxy_bypass_macosx_sysconf(host, proxy_settings)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 2566, in _proxy_bypass_macosx_sysconf
    hostIP = socket.gethostbyname(hostonly)
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label too long)

Console output while HA starts is:

ERROR:homeassistant.components.media_player:Error while setting up platform cast
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/idna.py", line 167, in encode
    raise UnicodeError("label too long")
UnicodeError: label too long

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/homeassistant/helpers/entity_component.py", line 155, in _async_setup_platform
    entity_platform.schedule_add_entities, discovery_info
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/jessewebdotcom/.homeassistant/custom_components/media_player/cast.py", line 73, in setup_platform
    all_chromecasts = pychromecast.get_chromecasts()
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/__init__.py", line 81, in get_chromecasts
    blocking=blocking))
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/__init__.py", line 48, in _get_chromecast_from_host
    blocking=blocking)
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/__init__.py", line 142, in __init__
    dev_status = get_device_status(self.host)
  File "/Users/jessewebdotcom/.homeassistant/deps/pychromecast/dial.py", line 49, in get_device_status
    timeout=10)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 501, in get
    return self.request('GET', url, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 479, in request
    prep.url, proxies, stream, verify, cert
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/sessions.py", line 654, in merge_environment_settings
    env_proxies = get_environ_proxies(url) or {}
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/utils.py", line 617, in get_environ_proxies
    if should_bypass_proxies(url):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/requests/utils.py", line 601, in should_bypass_proxies
    bypass = proxy_bypass(netloc)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 2616, in proxy_bypass
    return proxy_bypass_macosx_sysconf(host)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 2593, in proxy_bypass_macosx_sysconf
    return _proxy_bypass_macosx_sysconf(host, proxy_settings)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/urllib/request.py", line 2566, in _proxy_bypass_macosx_sysconf
    hostIP = socket.gethostbyname(hostonly)
UnicodeError: encoding with 'idna' codec failed (UnicodeError: label too long)
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=turn_on>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=turn_off>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=toggle>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=volume_up>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=volume_down>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=media_play_pause>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=media_play>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=media_pause>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=media_stop>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=media_next_track>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=media_previous_track>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=clear_playlist>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=volume_set>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=volume_mute>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=media_seek>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=select_source>
INFO:homeassistant.core:Bus:Handling <Event service_registered[L]: domain=media_player, service=play_media>
INFO:homeassistant.core:Bus:Handling <Event component_loaded[L]: component=media_player>
balloob commented 7 years ago

I wonder what we pass in. Could you add a print(host) before line 47 in dial.py

JesseWebDotCom commented 7 years ago
INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.70
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.252
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.3
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.26
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.212
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.2
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.25
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.7
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.9
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
192.168.1.10
-----STOP PRINT HOST-----

INFO:pychromecast:Querying device status

-----START PRINT HOST-----
b'\xfe\x80\x00\x00\x00\x00\x00\x00\xca*\x14\xff\xfe)\x1c\xfb'
-----STOP PRINT HOST-----

ERROR:homeassistant.components.media_player:Error while setting up platform cast
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/idna.py", line 167, in encode
    raise UnicodeError("label too long")
UnicodeError: label too long
JesseWebDotCom commented 7 years ago

I tried returning "None" if evaluated as False by this function:

def valid_ip_or_name(s):
    import re

    if re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", s):
        return True

    if  re.match("^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$", s):
        return True

    return False

which avoided the UnicodeError and got me a little further but ultimately stopped here:

ERROR:pychromecast.socket_client:Failed to connect, retrying in 5.0s
WARNING:homeassistant.setup:Setup of media_player is taking over 10 seconds.
WARNING:homeassistant.components.media_player:Setup of platform cast is taking over 10 seconds.
balloob commented 7 years ago

So looks like the problem is with b'\xfe\x80\x00\x00\x00\x00\x00\x00\xca*\x14\xff\xfe)\x1c\xfb', can you try to find out where this host is being generated? It seems pretty random since all others are IPs.

balloob commented 7 years ago

image

JesseWebDotCom commented 7 years ago

I have 10 cast devices, which correlates to the 10 ip addresses listed above. however, I also have an Nvidia shield that shows up in the Google Home app as a cast device (along my 10 real cast devices). I'm going to check if the address you listed above is my shield. With that said, I guess you'll have to add logic to (detect and) reject or accept IPV6 addresses.

JesseWebDotCom commented 7 years ago

No luck, I turned off IPV6 on the shield, rebooted, and still the same error occurs. Any ideas on how I can determine which host has that address?

JesseWebDotCom commented 7 years ago

found it, it's my iMac. Going to test with ipv6 turned off on my iMac. (update: didn't help as I couldn't find a way to do it - even setting ipv6 to local link)

JesseWebDotCom commented 7 years ago

OK, turns out it was caused by this product: https://www.airserver.com

which allows your iMac to act as an airplay, miracast, and chrome cast client. Once I turned it off, HA started and recognized all cast audio devices as expected.

If you want to add support for ipv6 and or this app, let me know and I can test any code updates you want.

OttoWinter commented 6 years ago

Even though not many people have devices with IPv6 link-local addresses in their homes, it might be good for pychromecast to either just ignore them or support them and not just throw an exception. Ignoring them would be very easy - just check whether encoding works or something like that. Supporting them in dial.py would probably not be that much more difficult, because the requests library can handle urls like http://[fe80::ca2a:14ff:fe29:1cfb]/setup/eureka_info (notice the square brackets).