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

Make device info a TypedDict #49670

Merged
merged 1 commit into from
Apr 30, 2021
Merged

Make device info a TypedDict #49670

merged 1 commit into from
Apr 30, 2021

Conversation

scop
Copy link
Member

@scop scop commented Apr 25, 2021

Proposed change

This makes entity device info a TypedDict, providing for easier coding and better typecheck time validation.

Type defs follow the docs at https://developers.home-assistant.io/docs/device_registry_index/#device-properties as well as in the device registry. In particular, entry_type is the only thing marked as optional here per the docs above.

This contains the bare minimum to implement the change and make tests pass, next step is to fix device_info signatures in the rest of the tree. I have it prepared already, but will submit separately to keep PR size at bay, unless a reviewer here requests it to be included already here.

FYI @prystupa: This exposes a "bug" in the bond component; it uses a three-tuple as device identifier, with one component of it being possibly None, whereas the expected identifiers are declared to be two-tuples of str (no optionals). I've marked it to be ignored here, that'll preserve current status. I guess the bug is currently a typecheck level only, as nothing seems to peek into the contents of those identifiers at runtime, they're just used as-is for lookup. But it would be good to get this properly fixed, preferably IMO after this PR has been merged.

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.
  • 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 @fphammerle, mind taking a look at this pull request as its been labeled with an integration (huawei_lte) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

@probot-home-assistant
Copy link

Hey there @home-assistant/z-wave, mind taking a look at this pull request as its been labeled with an integration (zwave_js) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)

"identifiers": {(DOMAIN, self._hub.bond_id, self._device.device_id)},
"suggested_area": self._device.location,
"via_device": (DOMAIN, self._hub.bond_id),
"identifiers": {
Copy link
Member

Choose a reason for hiding this comment

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

The dev docs aren't really clear that this isn't allowed for identifiers, but it does seem explicit for connections

https://developers.home-assistant.io/docs/device_registry_index/#device-properties

AFAICT, the only requirement for identifiers is they have to be unique and hashable

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 it would be ok to limit it to a two string tuple. There should be no problem to concatenate the strings into a single string as needed for the second string item.

Copy link
Member

@bdraco bdraco Apr 25, 2021

Choose a reason for hiding this comment

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

We should probably update the docs as well to make it explict.

Copy link
Member Author

@scop scop Apr 25, 2021

Choose a reason for hiding this comment

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

Device registry has identifiers hinted as str two-tuples everywhere though. One approach to fixing this would be to relax that somehow, but I don't know if that's seen appropriate or what's the intent.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it would be ok to limit it to a two string tuple. There should be no problem to concatenate the strings into a single string as needed for the second string item.

Agreed, but I suppose just doing that would cause the newly structured identifiers not match their old saved device registry entries for the bond integration, some migration code would be needed.

Copy link
Member

Choose a reason for hiding this comment

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

We haven't enforced two string tuple and I have seen tuples of length 1 and 3.

I think that for typing, we should type what we do right now. And in another PR we can decide to restrict it to 2.

Note, it was officially meant to be 2.

Copy link
Member Author

Choose a reason for hiding this comment

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

#49864 implements that, will adjust here once that's in. That won't be enough to make bond pass cleanly here as it has Optionals in the identifier str tuple, but decreases the need for the workaround.

@KapJI KapJI mentioned this pull request Apr 29, 2021
21 tasks
MartinHjelmare pushed a commit that referenced this pull request Apr 30, 2021
This reflects current practice, but the intent has been to have them as
2-tuples, and a future change is likely to start enforcing that (again).

Refs #49670 (comment)
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 59f32f7 into home-assistant:dev Apr 30, 2021
@mib1185 mib1185 mentioned this pull request May 1, 2021
21 tasks
@github-actions github-actions bot locked and limited conversation to collaborators May 1, 2021
@scop scop deleted the scop/device-info-typeddict branch May 2, 2021 07:40
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