Skip to content

Use dataclass properties in hue discovery#60598

Merged
frenck merged 6 commits intohome-assistant:devfrom
epenet:hue-discovery
Nov 30, 2021
Merged

Use dataclass properties in hue discovery#60598
frenck merged 6 commits intohome-assistant:devfrom
epenet:hue-discovery

Conversation

@epenet
Copy link
Copy Markdown
Contributor

@epenet epenet commented Nov 30, 2021

Proposed change

Use dataclass properties in hue discovery.

Linked to #60540

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

The integration reached or maintains the following Integration Quality Scale:

  • No score or internal
  • 🥈 Silver
  • 🥇 Gold
  • 🏆 Platinum

To help with the load of incoming pull requests:

@probot-home-assistant
Copy link
Copy Markdown

Hey there @balloob, @marcelveldt, mind taking a look at this pull request as it has been labeled with an integration (hue) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

Copy link
Copy Markdown
Member

@marcelveldt marcelveldt left a comment

Choose a reason for hiding this comment

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

Looks good!
What about adjusting the tests ?

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 30, 2021

What about adjusting the tests ?

They should have been adjusted already in previous PRs: #59980, #60217, #60339
Did I miss something?

@marcelveldt
Copy link
Copy Markdown
Member

What about adjusting the tests ?

They should have been adjusted already in previous PRs: #59980, #60217, #60339 Did I miss something?

Ah, sorry missed that. Was looking for the adjusted tests in this PR ;-)

Copy link
Copy Markdown
Member

@marcelveldt marcelveldt left a comment

Choose a reason for hiding this comment

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

needs a test for the config flow condition where a discovered Host is None

@epenet epenet mentioned this pull request Nov 30, 2021
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 30, 2021

The test was already there: tests/components/hue/test_config_flow.py::test_bridge_ssdp_missing_location
It was failing because str(None) generates "None" string when I expected ""empty string.

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

Copy link
Copy Markdown
Member

@marcelveldt marcelveldt Nov 30, 2021

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

Copy link
Copy Markdown
Member

@marcelveldt marcelveldt Nov 30, 2021

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...

Copy link
Copy Markdown
Member

@marcelveldt marcelveldt Nov 30, 2021

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.

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 30, 2021

Note: this PR is required for #60561

Copy link
Copy Markdown
Member

@marcelveldt marcelveldt left a comment

Choose a reason for hiding this comment

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

LGTM

good to merge

Copy link
Copy Markdown
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Nice! 👍

@frenck frenck merged commit d75785d into home-assistant:dev Nov 30, 2021
@epenet epenet deleted the hue-discovery branch November 30, 2021 15:45
@github-actions github-actions bot locked and limited conversation to collaborators Dec 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants