Skip to content

Use dataclass for DhcpServiceInfo#60136

Merged
MartinHjelmare merged 27 commits intohome-assistant:devfrom
epenet:service-info-dataclass
Nov 23, 2021
Merged

Use dataclass for DhcpServiceInfo#60136
MartinHjelmare merged 27 commits intohome-assistant:devfrom
epenet:service-info-dataclass

Conversation

@epenet
Copy link
Copy Markdown
Contributor

@epenet epenet commented Nov 22, 2021

Breaking change

Dhcp discovery now uses a dataclass instead of a dictionary object.
Access using __getitem__ will fail in version 2022.6.
Please use <cls>.<name> instead.

Proposed change

Dhcp discovery now uses a dataclass instead of a dictionary object.
Access using __getitem__ will fail in version 2022.6.
Please use <cls>.<name> instead.

Linked to this comment in the architecture discussion home-assistant/architecture#662 (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
Copy Markdown

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

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 22, 2021

cc @Jc2k for the original idea

@epenet epenet marked this pull request as ready for review November 22, 2021 14:45
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 22, 2021

Side note: is there any way for a developper to stop actions and release the CI agents on their own PR?

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 22, 2021

And I think this needs the breaking-change label added.

@epenet epenet force-pushed the service-info-dataclass branch from 7bbe5f1 to 7bfceb0 Compare November 22, 2021 15:41
@epenet epenet mentioned this pull request Nov 22, 2021
22 tasks
@epenet epenet requested a review from a team as a code owner November 22, 2021 20:20
Copy link
Copy Markdown
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

LGTM 👍

I like this solution quite a bit more.

Copy link
Copy Markdown
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.

The data_entry_flow, config_entries and helpers will need to be updated to support this. They expect and have types for user_input as dict.

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 23, 2021

@MartinHjelmare am I missing something?

data_entry_flow.py should not require amends (the discovery steps are not called by the UI)

config_entries.py is ready for this:

async def async_step_dhcp(
self, discovery_info: DhcpServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle a flow initialized by DHCP discovery."""
return await self.async_step_discovery(cast(dict, discovery_info))

helpers/config_entry_flow.py is ready for this also:

async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle a flow initialized by dhcp discovery."""
if self._async_in_progress() or self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")
await self.async_set_unique_id(self._domain)
return await self.async_step_confirm()

Components can continue using discovery_info[ATTR]

@MartinHjelmare
Copy link
Copy Markdown
Member

When the flow is started the data entry flow manager eventually calls _async_handle_step before we reach the config flow module step of the integration. _async_handle_step takes user_input which has type dict | None.

user_input: dict | None,

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 23, 2021

Is this the only place where something is missing?

I can adjust the type hint:
user_input: dict | DhcpServiceInfo | MqttServiceInfo | UsbServiceInfo | None,

Or create a base class BaseServiceInfo that the other inherit from:
user_input: dict | BaseServiceInfo | None,

@MartinHjelmare
Copy link
Copy Markdown
Member

I looked over it briefly now and I think that's the only place. The other places already had the data input typed as Any.

Any of the above suggestions are fine. The latter is shorter and more easily extendable so I'd recommend that.

@epenet epenet force-pushed the service-info-dataclass branch from 1eea353 to 1f5f35b Compare November 23, 2021 09:57
@epenet epenet mentioned this pull request Nov 23, 2021
22 tasks
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 23, 2021

All checks have passed, the comments from @MartinHjelmare have been addressed.
Is there anything else missing in this PR?

Copy link
Copy Markdown
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.

Thanks!

@MartinHjelmare MartinHjelmare merged commit 560546f into home-assistant:dev Nov 23, 2021
@epenet epenet deleted the service-info-dataclass branch November 23, 2021 12:46
@github-actions github-actions bot locked and limited conversation to collaborators Nov 24, 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.

6 participants