Skip to content

Use dataclass for SsdpServiceInfo#59931

Merged
bdraco merged 16 commits intohome-assistant:devfrom
epenet:ssdp-type-hint
Nov 29, 2021
Merged

Use dataclass for SsdpServiceInfo#59931
bdraco merged 16 commits intohome-assistant:devfrom
epenet:ssdp-type-hint

Conversation

@epenet
Copy link
Copy Markdown
Contributor

@epenet epenet commented Nov 18, 2021

Breaking change

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

Proposed change

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

Linked to this comment in the architecture discussion home-assistant/architecture#662 (comment)

This is a central PR to see what needs to be done:

  1. Revert incorrect SsdpServiceInfo(TypedDict):
  1. Rename matching domains as the current value prevents use as property :
  1. Introduce new SsdpServiceInfo(BaseServiceInfo) class:
  1. Update all tests in components to use the new SsdpServiceInfo(BaseServiceInfo) class:
  1. (this PR) Update ssdp to use the new SsdpServiceInfo(BaseServiceInfo) in discovery_flow.async_create_flow
  2. Update all components to use the new SsdpServiceInfo in async_step_ssdp

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:

@epenet epenet changed the title Ssdp type hint Adjust ssdp type hints Nov 18, 2021
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 18, 2021

Should info[ATTR_UPNP_UDN] also be duplicated/moved to info[ATTR_UPNP][ATTR_UPNP_UDN]?

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 18, 2021

Should info[ATTR_UPNP_UDN] also be duplicated/moved to info[ATTR_UPNP][ATTR_UPNP_UDN]?

Only if it can have arbitrary keys. I think the answer is yes, but it would be good to get a comment from StevenLooman or chishm to confirm

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 18, 2021

Should info[ATTR_UPNP_UDN] also be duplicated/moved to info[ATTR_UPNP][ATTR_UPNP_UDN]?

Only if it can have arbitrary keys. I think the answer is yes, but it would be good to get a comment from StevenLooman or chishm to confirm

cc @StevenLooman

@epenet epenet requested a review from StevenLooman as a code owner November 18, 2021 21:50
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 18, 2021

In order to help with the review (I hope) I have added to this PR the corresponding changes to dlna_dmr component as an example.

If you feel that it makes the PR too big and it is better to move it back out into a separate PR I will be happy to oblige.

Edit: I have moved the sample dlna_dmr PR to #59957

@chishm
Copy link
Copy Markdown
Contributor

chishm commented Nov 19, 2021 via email

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 19, 2021

I'll take a closer look when I've got more time, but the biggest concern I have at the moment is that, for discovery, components often match UPnP info (e.g. deviceType) in their manifest.json. That will possibly break if it's not in the top-level discovery info dict.

@chishm this PR is not really a breaking change, as the data is duplicated for now. It is more a warning to other users to show that the real breaking change is coming in a future release.
I will prepare anyway the removal PR so that we can track and test it.

@epenet epenet mentioned this pull request Nov 19, 2021
22 tasks
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 19, 2021

I have created the draft removal PR: #59960

@chishm
Copy link
Copy Markdown
Contributor

chishm commented Nov 20, 2021

@chishm this PR is not really a breaking change, as the data is duplicated for now. It is more a warning to other users to show that the real breaking change is coming in a future release. I will prepare anyway the removal PR so that we can track and test it.

Understood, but I thought it best to flag early that this will break how discovery info is matched to components. For example, arcam_fmj defines in it's manifest.json the following:

  "ssdp": [
    {
      "deviceType": "urn:schemas-upnp-org:device:MediaRenderer:1",
      "manufacturer": "ARCAM"
    }
  ]

In the ssdp component this is matched by IntegrationMatchers.async_matching_domains to the UPnP info in the combined_headers dict. The match is only done at the top level of the dict (no recursion), and is optimised for quickly checking SSDP notifications/searches against all domains.

This will need more consideration before altering the structure of the combined_headers dict.

@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 20, 2021

Understood, but I thought it best to flag early that this will break how discovery info is matched to components.

Yes it is already covered in the tests, and implemented in draft #59960

@chishm
Copy link
Copy Markdown
Contributor

chishm commented Nov 20, 2021

Yes it is already covered in the tests, and implemented in draft #59960

Ah, thank you for pointing that out, I missed it on first read-through. I withdraw my concern and will give this a full review tomorrow 😄

@epenet epenet changed the title Adjust ssdp type hints Use dataclass for SsdpServiceInfo (part 1) Nov 23, 2021
@epenet epenet changed the title Use dataclass for SsdpServiceInfo (part 1) Use dataclass for SsdpServiceInfo Nov 24, 2021
@epenet epenet mentioned this pull request Nov 24, 2021
22 tasks
@epenet epenet marked this pull request as draft November 24, 2021 11:33
@epenet epenet marked this pull request as ready for review November 26, 2021 13:03
@epenet
Copy link
Copy Markdown
Contributor Author

epenet commented Nov 29, 2021

I have dropped the DISCOVERY_MAPPING constant.

I have also dropped the __iter__ method to replace it with __contains__ method as I think it is a better way to solve the in/not in checks that some components still use. See #60339

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 29, 2021

I'll take a look tomorrow after some sleep.

@epenet epenet mentioned this pull request Nov 29, 2021
22 tasks
@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 29, 2021

I've run though this a few times now, and I think its good to go. I'm going to test sonos with it first though

@bdraco
Copy link
Copy Markdown
Member

bdraco commented Nov 29, 2021

Sonos looks good.

Already tested discovery.

@bdraco bdraco merged commit ec1c52d into home-assistant:dev Nov 29, 2021
@epenet epenet deleted the ssdp-type-hint branch November 29, 2021 16:11
@github-actions github-actions bot locked and limited conversation to collaborators Nov 30, 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.

4 participants