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

Differentiate between device info types #95641

Merged
merged 16 commits into from
Jul 10, 2023
Merged

Differentiate between device info types #95641

merged 16 commits into from
Jul 10, 2023

Conversation

balloob
Copy link
Member

@balloob balloob commented Jun 30, 2023

Breaking change

Home Assistant is now differentiating between 3 different types of device info that can be included with an entity. It will reject device info values that include keys belonging to 2 different types.

Proposed change

This is a first step to make device_info property on an entity more strict. Eventual goal will be to know which config entry is the primary entry making a device.

This first step is going to distinguish between 3 types of device info that we expect:

  • primary device info (name, etc)
  • linking to existing device (only connections/identifiers)
  • providing extra info (default_X values)

Requires:

Architecture discussion home-assistant/architecture#936

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)
  • Deprecation (breaking change to happen in the future)
  • 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
  • I have followed the perfect PR recommendations
  • 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.

To help with the load of incoming pull requests:

@balloob balloob requested a review from a team as a code owner June 30, 2023 18:44
"default_model",
"default_name",
# Used by Fritz
"via_device",
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 didn't expect this to be set to indicate which network device is used to connect a device to the network. I guess it fits, but other integrations don't do it.

@balloob balloob force-pushed the define-device-type-info branch 2 times, most recently from 94aad19 to 03727f9 Compare July 2, 2023 14:31
@balloob balloob force-pushed the define-device-type-info branch 2 times, most recently from d1ee589 to 34bfdb1 Compare July 6, 2023 01:25
Copy link
Contributor

@bachya bachya left a comment

Choose a reason for hiding this comment

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

✅ PurpleAir

homeassistant/helpers/entity_platform.py Outdated Show resolved Hide resolved
Comment on lines +769 to +773
self.logger.error(
"Ignoring device info without identifiers or connections: %s",
device_info,
)
return None
Copy link
Contributor

@emontnemery emontnemery Jul 7, 2023

Choose a reason for hiding this comment

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

It didn't raise in the old code either, but is there a good reason to not raise here? Why do we go ahead and add an entity which has invalid device info?

I'm not suggesting we change the behavior in this PR, but it's something to consider in a follow-up I think.

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 chose not to change that behavior as it would be breaking. It would probably be a small breaking change as I was pleasantly surprised the low amount of bad device info this PR found. If we decide to do this, we should do it in a follow-up PR.

homeassistant/helpers/entity_platform.py Show resolved Hide resolved
Comment on lines +783 to +788
self.logger.error(
"Device info for %s needs to either describe a device, "
"link to existing device or provide extra information.",
device_info,
)
return None
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment here, should this raise instead?

Comment on lines +796 to +800
self.logger.error(
"Ignoring device info with invalid configuration_url '%s'",
config_url,
)
return None
Copy link
Contributor

Choose a reason for hiding this comment

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

Same

@@ -10,4 +10,5 @@ def fritz_fixture() -> Mock:
with patch("homeassistant.components.fritzbox.Fritzhome") as fritz, patch(
"homeassistant.components.fritzbox.config_flow.Fritzhome"
):
fritz.return_value.get_prefixed_host.return_value = "http://1.2.3.4"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this test changed?

Copy link
Member Author

Choose a reason for hiding this comment

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

The test used to return a Mock for this function, which resulted in passing Mock to configuration_url which triggered invalid device info, and device not to be created.

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 have slightly changed the behavior here. In the past, an invalid configuration URL would merely result in removal of the invalid configuration URL. With the updated logic, we will actually mark the whole device info as invalid and not create a device.

@@ -114,6 +114,7 @@ def create_mock_client() -> Mock:
mock_client.instances = [
{"friendly_name": "Test instance 1", "instance": 0, "running": True}
]
mock_client.remote_url = f"http://{TEST_HOST}:{TEST_PORT_UI}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this test changed?

@@ -19,6 +19,7 @@ def api_fixture(get_sensors_response):
"""Define a fixture to return a mocked aiopurple API object."""
return Mock(
async_check_api_key=AsyncMock(),
get_map_url=Mock(return_value="http://example.com"),
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this test changed?

Co-authored-by: Erik Montnemery <erik@montnemery.com>
@balloob balloob merged commit eee8566 into dev Jul 10, 2023
47 checks passed
@balloob balloob deleted the define-device-type-info branch July 10, 2023 13:56
@github-actions github-actions bot locked and limited conversation to collaborators Jul 11, 2023
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.

None yet

4 participants