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

Also migrate dsmr entries for devices with correct serial #123407

Merged
merged 1 commit into from
Aug 10, 2024

Conversation

dupondje
Copy link
Contributor

@dupondje dupondje commented Aug 8, 2024

When the dsmr code could not find the serial_nr for the gas meter, it creates the gas meter device with the entry_id as identifier.

But when there is a correct serial_nr, it will use that as identifier for the dsmr gas device.

Now the migration code did not take this into account, so migration to the new name failed since it didn't look for the device with correct serial_nr.

This commit fixes this and adds a test for this.

Breaking change

Proposed change

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

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 Ruff (ruff format 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.

To help with the load of incoming pull requests:

When the dsmr code could not find the serial_nr for the gas meter,
it creates the gas meter device with the entry_id as identifier.

But when there is a correct serial_nr, it will use that as identifier
for the dsmr gas device.

Now the migration code did not take this into account, so migration to
the new name failed since it didn't look for the device with correct
serial_nr.

This commit fixes this and adds a test for this.
@home-assistant
Copy link

home-assistant bot commented Aug 8, 2024

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

Code owner commands

Code owners of dsmr can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign dsmr Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@dupondje
Copy link
Contributor Author

dupondje commented Aug 8, 2024

@elupus @joostlek @jbouwh : Can somebody review and confirm my conclusion? :)
And even better if somebody could test it.

)
if not dev_entities:
dev_reg.async_remove_device(device_id)
for dev_id in (mbus_device_id, entry.entry_id):
Copy link
Contributor

Choose a reason for hiding this comment

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

We could look for a device with entry.data[CONF_SERIAL_ID_GAS]?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact we should stop using CONF_SERIAL_ID_GAS for devices with mbus devices.
Cause CONF_SERIAL_ID_GAS is a single entity ... while with mbus we have 4 devices that can be linked to the main device, so you can have 4 gas meters in theory.
Better check for the mbus_device_id.

Copy link
Contributor

@elupus elupus Aug 9, 2024

Choose a reason for hiding this comment

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

What about non mbus devices?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How you mean? There should be only 1 main SERIAL for the device and then a serial per mbus device.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure what il saying. I think i would have been more happy with doing a full unique_id check and just iterate over all entities in the config entry instead of per device.

Unique_id, should be just that, unique for the integration. The associated device have nothing to do with it really.

Copy link
Contributor

Choose a reason for hiding this comment

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

I still don't like doing a RANDOM_PREFIX_THAT_CAN_BE_ANYTHINGhourly_gas_meter_reading -> mbus_device_id. It's very error prone.

ALSO, which missed before: async_entries_for_device can include entities from other platform, like helpers. They are unlikely to have that unique_id, but nothing really stopping them.

Copy link
Contributor

@elupus elupus Aug 9, 2024

Choose a reason for hiding this comment

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

But migrating the sensor to the correct/new device is better/cleaner, as before 9118260 for example the device did not have the correct serial like it should.

This will be handled automatically as soon as the device is added to the system so migration code does not need to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But migrating the sensor to the correct/new device is better/cleaner, as before 9118260 for example the device did not have the correct serial like it should.

This will be handled automatically as soon as the device is added to the system so migration code does not need to do that.

As long as we cleanup the old/empty device then :)

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 still don't like doing a RANDOM_PREFIX_THAT_CAN_BE_ANYTHINGhourly_gas_meter_reading -> mbus_device_id. It's very error prone.

ALSO, which missed before: async_entries_for_device can include entities from other platform, like helpers. They are unlikely to have that unique_id, but nothing really stopping them.

Just need to make sure there are not older versions that created the sensors with even another prefix.
I think not, but might be good to double check.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. Let's do it this way for now. The prefix was weird historically too.

I would like to remove this migration code as soon as we can. But ends up a year in the future i think.

@dupondje Think you would be up for taking on maintainership on this component?

@MartinHjelmare MartinHjelmare changed the title dsmr: also migrate entries for devices with correct serial Also migrate dsmr entries for devices with correct serial Aug 8, 2024
@elupus elupus added this to the 2024.8.1 milestone Aug 10, 2024
@elupus elupus merged commit 750bce2 into home-assistant:dev Aug 10, 2024
26 checks passed
frenck pushed a commit that referenced this pull request Aug 10, 2024
dsmr: also migrate entries for devices with correct serial

When the dsmr code could not find the serial_nr for the gas meter,
it creates the gas meter device with the entry_id as identifier.

But when there is a correct serial_nr, it will use that as identifier
for the dsmr gas device.

Now the migration code did not take this into account, so migration to
the new name failed since it didn't look for the device with correct
serial_nr.

This commit fixes this and adds a test for this.
@frenck frenck mentioned this pull request Aug 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 11, 2024
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!

ent_reg, device_id, include_disabled_entities=True
)
if not dev_entities:
dev_reg.async_remove_device(device_id)
Copy link
Member

Choose a reason for hiding this comment

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

It's safer to update the device entry and remove the config entry id. If it was the only config entry id, the device entry will be removed automatically.

@home-assistant home-assistant unlocked this conversation Aug 28, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 29, 2024
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.

DSMR Smart Meter gas meter breaking change in 2024.8 beta
4 participants