Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions homeassistant/components/hue/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,28 @@ async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowRes
"""
# Filter out non-Hue bridges #1
if (
discovery_info.get(ssdp.ATTR_UPNP_MANUFACTURER_URL)
discovery_info.upnp.get(ssdp.ATTR_UPNP_MANUFACTURER_URL)
not in HUE_MANUFACTURERURL
):
return self.async_abort(reason="not_hue_bridge")

# Filter out non-Hue bridges #2
if any(
name in discovery_info.get(ssdp.ATTR_UPNP_FRIENDLY_NAME, "")
name in discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME, "")
for name in HUE_IGNORED_BRIDGE_NAMES
):
return self.async_abort(reason="not_hue_bridge")

if (
not discovery_info.get(ssdp.ATTR_SSDP_LOCATION)
or ssdp.ATTR_UPNP_SERIAL not in discovery_info
):
if ssdp.ATTR_UPNP_SERIAL not in discovery_info.upnp:
return self.async_abort(reason="not_hue_bridge")

host = urlparse(discovery_info.ssdp_location).hostname
if not host:
Comment thread
marcelveldt marked this conversation as resolved.
Outdated
return self.async_abort(reason="not_hue_bridge")

host = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION]).hostname
bridge = await self._get_bridge(host, discovery_info[ssdp.ATTR_UPNP_SERIAL])
bridge = await self._get_bridge(
str(host), discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
str(host), discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL]
host, discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my initial thought also, but hostname return str | bytes | None

@marcelveldt marcelveldt Nov 30, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh ? why is that ?
On my setup it returns either str or None

Schermafbeelding 2021-11-30 om 11 08 47

@marcelveldt marcelveldt Nov 30, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, my dev machine is on python 3.10... might be that urlparse changed between py versions...
I think this is why I added the type: ignore[arg-type] here...

@marcelveldt marcelveldt Nov 30, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the docs the namedtuple ParseResult either have a str or None value so not bytes.
https://docs.python.org/3/library/urllib.parse.html

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha! I think I found the root cause... I have a similar issue in samsungtv PR #60595

  • previously discovery_info[ATTR_SSDP_LOCATION] return type was Any
  • now discovery_info.ssdp_location is defined as str | None

If the first argument of urlparse is Any, then urlparse.hostname returns str | None
If the first argument of urlparse is str | None, then urlparse.hostname returns str | bytes | None

I'll see if I can clean this up better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are 100% right. The documentation doesn't match wil reality and the type is indeed AnyStr, yikes.
Maybe add a very minor extra line that converts to string if the output is bytes ?
Or indeed leave your forced to string conversion in place.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reworked this, and added an extra test for an invalid hostname.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Mypy happy now ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the tests pass. I think this is good to merge.

)

await self.async_set_unique_id(bridge.id)
self._abort_if_unique_id_configured(
Expand All @@ -232,8 +234,8 @@ async def async_step_zeroconf(
host is already configured and delegate to the import step if not.
"""
bridge = await self._get_bridge(
discovery_info[zeroconf.ATTR_HOST],
discovery_info[zeroconf.ATTR_PROPERTIES]["bridgeid"],
discovery_info.host,
discovery_info.properties["bridgeid"],
)

await self.async_set_unique_id(bridge.id)
Expand All @@ -253,7 +255,7 @@ async def async_step_homekit(
as the unique identifier. Therefore, this method uses discovery without
a unique ID.
"""
self.bridge = await self._get_bridge(discovery_info[zeroconf.ATTR_HOST])
self.bridge = await self._get_bridge(discovery_info.host)
await self._async_handle_discovery_without_unique_id()
return await self.async_step_link()

Expand Down