Use dataclass properties in hue discovery#60598
Conversation
|
Hey there @balloob, @marcelveldt, mind taking a look at this pull request as it has been labeled with an integration ( |
marcelveldt
left a comment
There was a problem hiding this comment.
Looks good!
What about adjusting the tests ?
marcelveldt
left a comment
There was a problem hiding this comment.
needs a test for the config flow condition where a discovered Host is None
|
The test was already there: |
| 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] |
There was a problem hiding this comment.
| str(host), discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL] | |
| host, discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL] |
There was a problem hiding this comment.
That was my initial thought also, but hostname return str | bytes | None
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 wasAny - now
discovery_info.ssdp_locationis defined asstr | 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I have reworked this, and added an extra test for an invalid hostname.
There was a problem hiding this comment.
Looks good. Mypy happy now ?
There was a problem hiding this comment.
Yes the tests pass. I think this is good to merge.
|
Note: this PR is required for #60561 |

Proposed change
Use dataclass properties in
huediscovery.Linked to #60540
Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.The integration reached or maintains the following Integration Quality Scale:
To help with the load of incoming pull requests: