core: IPv6 in auto-detected Nanoleaf device causes parsing issues

The problem

I got an “unexpected error”, when trying to add my auto-detected Nanoleaf 4D device. The logs indicate an issue with IPv6 handling. When adding the device manually via its’ IPv4 address, i get an already_in_progress error.

What version of Home Assistant Core has the issue?

core-2024.11.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Container

Integration causing the issue

Nanoleaf

Link to integration documentation on our website

https://www.home-assistant.io/integrations/nanoleaf

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

Logger: homeassistant.components.nanoleaf.config_flow
Source: components/nanoleaf/config_flow.py:163
integration: Nanoleaf (documentation, issues)
First occurred: 5:09:38 PM (1 occurrences)
Last logged: 5:09:38 PM

Unknown error authorizing Nanoleaf
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/yarl/_parse.py", line 134, in split_netloc
    port = int(port_str)
           ^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: '3100:44ec:3500:828a:f7ff:fe09:f2ae:16021'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 516, in _request
    url = self._build_url(str_or_url)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 444, in _build_url
    url = URL(str_or_url)
          ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/yarl/_url.py", line 307, in __new__
    return pre_encoded_url(val) if encoded else encode_url(val)
                                                ^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/yarl/_url.py", line 154, in encode_url
    username, password, host, port = split_netloc(netloc)
                                     ^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/yarl/_parse.py", line 136, in split_netloc
    raise ValueError("Invalid URL: port can't be converted to integer")
ValueError: Invalid URL: port can't be converted to integer

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

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/nanoleaf/config_flow.py", line 163, in async_step_link
    await self.nanoleaf.authorize()
  File "/usr/local/lib/python3.12/site-packages/aionanoleaf/nanoleaf.py", line 266, in authorize
    resp = await self._session.post(f"{self._api_url}/new")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/aiohttp/client.py", line 518, in _request
    raise InvalidUrlClientError(str_or_url) from e
aiohttp.client_exceptions.InvalidUrlClientError: http://2a02:3100:44ec:3500:828a:f7ff:fe09:f2ae:16021/api/v1/new

Additional information

Adding my other Nanoleaf Device (Shapes) worked fine.

About this issue

Most upvoted comments

I think the issue is with the underlying library and how the device presents itself in mDNS. https://github.com/milanmeu/aionanoleaf is the underlying library, which does not handle ipv6 host ip addresses correctly when generating URLs. I think also the homeassistant side tries to parse the port number incorrectly as well on return.

In Flame I notice the device reports ipv6 addresses that are part of the thread network as well as the IP network. I’m not sure how the library is picking the address to send back yet I’ll keep digging if I have time but I thought I’d help with my little bit of research.

It helped me to disable IPv6 in network settings of HA.

I have no idea how to respond to that. Because, it’s not always an option, to turn off IPv6. In my case, for example, it isn’t. IPv6 was standardized in 1998 - 26 Years since then and, from my point of view, it should just work flawlessly by today.

But, because we don’t know where the problem comes from exactly, we need to wait until someone has the mercy to help us.

Are your Nanoleafs working via the Nanoleaf mobile app?