Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicitly allow Platform enum in discovery helper #63571

Merged
merged 12 commits into from
Jan 9, 2022

Conversation

epenet
Copy link
Contributor

@epenet epenet commented Jan 6, 2022

Proposed change

Add Platform to type hint in homeassistant/helpers/discovery.py
Adjust a few components accordingly (will need a follow-up PR to adjust all components but they are here as a sample)
cc @frenck / @cdce8p / @MartinHjelmare

This is a follow-up to the discussion in #63495 (comment)

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

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

@probot-home-assistant
Copy link

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

@probot-home-assistant
Copy link

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

@probot-home-assistant
Copy link

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

@epenet epenet mentioned this pull request Jan 6, 2022
22 tasks
@epenet epenet marked this pull request as draft January 6, 2022 21:01
@cdce8p cdce8p mentioned this pull request Jan 7, 2022
22 tasks
@epenet
Copy link
Contributor Author

epenet commented Jan 7, 2022

I have renamed the arguments.
I have explicitely converted the Platform enum to its value to ensure async_setup_component uses a string.

And I have kept just a single component (amcrest) to show how it would be used.

if component not in hass.config.components:
setup_success = await setup.async_setup_component(hass, component, hass_config)
# Ensure that the underlying platform domain is setup
platform_domain = platform.value if isinstance(platform, Platform) else platform
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could add a frame.report here if we want to encourage people to use the Platform enum.
For example "if platform is a string and platform could be in Platform then report the frame"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but since Platform is a helper and not a definitive list maybe it is not necessary to report it

Copy link
Member

Choose a reason for hiding this comment

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

I think the better long term option would be to at some point update the type again and only allow Platform not str anymore. For now it's perfectly fine to pass a str. As I mentioned, they behave the same.

@epenet epenet changed the title Use Platform enum in discovery Explicitely allow Platform enum in discovery helper Jan 7, 2022
@epenet epenet marked this pull request as ready for review January 7, 2022 06:58
@epenet
Copy link
Contributor Author

epenet commented Jan 7, 2022

cc @scop as you were heavily involved in the StrEnum

Copy link
Member

@cdce8p cdce8p left a comment

Choose a reason for hiding this comment

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

Similar comments to #63581 (review)
As for requiring Platform enum (with typing), at some point this might be an option. Don't know what others think about it though. I seem to remember that Paulus once said he is fine with passing a string. Not sure that's still current. home-assistant/architecture#663 (comment)

homeassistant/helpers/discovery.py Outdated Show resolved Hide resolved
homeassistant/helpers/discovery.py Outdated Show resolved Hide resolved
homeassistant/helpers/discovery.py Outdated Show resolved Hide resolved
if component not in hass.config.components:
setup_success = await setup.async_setup_component(hass, component, hass_config)
# Ensure that the underlying platform domain is setup
platform_domain = platform.value if isinstance(platform, Platform) else platform
Copy link
Member

Choose a reason for hiding this comment

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

I think the better long term option would be to at some point update the type again and only allow Platform not str anymore. For now it's perfectly fine to pass a str. As I mentioned, they behave the same.

@epenet epenet marked this pull request as draft January 8, 2022 15:02
@epenet epenet marked this pull request as ready for review January 8, 2022 20:05
Copy link
Member

@MartinHjelmare MartinHjelmare 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!

@MartinHjelmare MartinHjelmare merged commit 665eeb4 into home-assistant:dev Jan 9, 2022
@epenet epenet deleted the discovery-loadplatform branch January 9, 2022 06:59
@github-actions github-actions bot locked and limited conversation to collaborators Jan 10, 2022
@emontnemery emontnemery changed the title Explicitely allow Platform enum in discovery helper Explicitly allow Platform enum in discovery helper Jan 10, 2022
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.

6 participants