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

Fix unique_id generation for AtwZoneSensors #51227

Merged

Conversation

vilppuvuorinen
Copy link
Contributor

@vilppuvuorinen vilppuvuorinen commented May 29, 2021

Breaking change

This change changes unique_id generation for all ATW zone sensors and therefore it will require manual intervention to replace sensor references in existing 1 zone setups.

The existing ID scheme ({measurement}) is replaced by {measurement}-zone-{zone index}. It would be possible to keep the existing scheme for the first, currently functional zone, but the mixed scheme is just terrible. 2 zone setups would have sensor IDs like these:

  • zone 1 -> {measurement}
  • zone 2 -> {measurement}-zone-2

Proposed change

Add zone index to ATW zone sensor unique_ids. The current implementation does not generate unique unique_ids with 2 zone setups.

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:

@emontnemery
Copy link
Contributor

Please fix the broken import in the test.

The existing unique_id generation generated the same ids for both zones
in 2 zone setups. This change adds a zone-<index> suffix to the id breaking
existing 1 zone setups.
@vilppuvuorinen vilppuvuorinen force-pushed the bugfix/fix-atw-zone-sensor-aliasing branch from 1ce1408 to c6528c9 Compare June 1, 2021 17:17
@vilppuvuorinen
Copy link
Contributor Author

vilppuvuorinen commented Jun 1, 2021

I'm having issues running the tests locally in my WSL2 based dev environment. This could take a while.

Edit: Nvm, I got it working.

@vilppuvuorinen vilppuvuorinen force-pushed the bugfix/fix-atw-zone-sensor-aliasing branch from c6528c9 to 9e91b6f Compare June 1, 2021 17:26
@@ -165,7 +165,8 @@ class AtwZoneSensor(MelDeviceSensor):

def __init__(self, api: MelCloudDevice, zone: Zone, measurement, definition):
"""Initialize the sensor."""
super().__init__(api, measurement, definition)
full_measurement = f"{measurement}-zone-{zone.zone_index}"
Copy link
Member

Choose a reason for hiding this comment

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

Can we avoid adding the zone_index if the index is 0? That way it will minimize the impact of the breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd rather check whether the device has more than 1 zones and add the suffix only in those cases. That way the 1 zone variant would not be impacted and the previously broken 2 zone setups would have consistent IDs. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

The goal is to reduce the breaking changes. So if Home Assistant used to always receive index 0 first, that data is correct and it just ignored other zones. So by keeping index 0 in the old format, less will break.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I implemented the zone_index == 1 exception.

The solution I proposed has similar goals and effects.

  • If a device has a single zone, the unique_id would remain the same.
  • If a device has 2 zones, the sensors were unusable and there is nothing to break with the different unique_id generation.

Dev automation moved this from Needs review to Reviewer approved Jun 28, 2021
Copy link
Member

@frenck frenck left a comment

Choose a reason for hiding this comment

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

Thanks, @vilppuvuorinen 👍

@frenck frenck merged commit 4ba5a4f into home-assistant:dev Jun 28, 2021
Dev automation moved this from Reviewer approved to Done Jun 28, 2021
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.

Please address the comment in a new PR. Thanks!


def test_zone_unique_ids(mock_device, mock_zone_1, mock_zone_2):
"""Test unique id generation correctness."""
sensor_1 = AtwZoneSensor(mock_device, mock_zone_1, "room_temperature", {})
Copy link
Member

Choose a reason for hiding this comment

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

We should set up the integration and assert the entity unique_id via the entity registry. We shouldn't interact with the entity instance directly.

https://developers.home-assistant.io/docs/development_testing#writing-tests-for-integrations

@github-actions github-actions bot locked and limited conversation to collaborators Jun 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Dev
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

6 participants