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

Use EntityDescription - synology_dsm #55407

Merged
merged 6 commits into from
Sep 3, 2021

Conversation

cdce8p
Copy link
Member

@cdce8p cdce8p commented Aug 29, 2021

Proposed change

Update synology_dsm to use EntityDescription for sensor, binary_sensor, and switch metadata.
-> #53201

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

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.

@cdce8p cdce8p requested a review from Quentame as a code owner August 29, 2021 14:02
@cdce8p cdce8p added the project-code_style PRs related to #53201 - pylint CodeStyle update label Aug 29, 2021
@probot-home-assistant
Copy link

Hey there @hacf-fr, @Quentame, @mib1185, mind taking a look at this pull request as it has been labeled with an integration (synology_dsm) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

Copy link
Contributor

@mib1185 mib1185 left a comment

Choose a reason for hiding this comment

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

I have done some tests, with suggested change about unique id everything works well.
All suggestions about moving specific stuff into corresponding platforms are based on the comments I get from martin in #55388

homeassistant/components/synology_dsm/__init__.py Outdated Show resolved Hide resolved
@cdce8p
Copy link
Member Author

cdce8p commented Aug 29, 2021

All suggestions about moving specific stuff into corresponding platforms are based on the comments I get from martin in #55388

I don't think it makes much sense here to move them. Aside from the switch description, most are imported in __init__.py as well. Moving them to their respective platforms would create a circular dependency.

Sometimes a const.py file is the better option after all.

Copy link
Contributor

@mib1185 mib1185 left a comment

Choose a reason for hiding this comment

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

Local tests passed ... LGTM 👍

@cdce8p cdce8p added the smash Indicator this PR is close to finish for merging or closing label Aug 31, 2021
@ludeeus ludeeus self-assigned this Sep 3, 2021
Copy link
Member

@ludeeus ludeeus left a comment

Choose a reason for hiding this comment

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

LGTM 👍

self._attr_unique_id: str = (
f"{api.information.serial}_{description.api_key}:{description.key}"
)
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
Copy link
Member

Choose a reason for hiding this comment

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

This is static, move it to a class attribute

@cdce8p cdce8p merged commit 4eba2cc into home-assistant:dev Sep 3, 2021
@cdce8p cdce8p deleted the cs_09_synology_dsm branch September 3, 2021 21:35
@@ -604,51 +598,25 @@ async def async_update(self, now: timedelta | None = None) -> None:
class SynologyDSMBaseEntity(CoordinatorEntity):
"""Representation of a Synology NAS entry."""

entity_description: SynologyDSMEntityDescription
unique_id: str
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
Copy link
Member

Choose a reason for hiding this comment

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

This is a bit dangerous. If one entity instance would update this dict all instances of the class would be updated. It's safer to define mutable objects as instance attributes.

Copy link
Member Author

Choose a reason for hiding this comment

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

True, it might be dangerous if unaccounted for. Although, we define other attributes also as statics. They can just as easily be overwriting. Mutable or not shouldn't make a difference here.

At the moment, defining the attribution as static is quite common (14 places). So too is _attr_should_poll, _attr_entity_registry_enabled_default, and to a lesser extend _attr_icon or even _attr_native_unit_of_measurement.

--
I'm not fully against changing them, only I think we should decide it for every attribute then and not just of extra_state_attributes.

@ludeeus What do you think?

Copy link
Member

@MartinHjelmare MartinHjelmare Sep 4, 2021

Choose a reason for hiding this comment

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

The important point here is that we set a class attribute with a mutable object. The other examples you give are not mutable. Those are not a problem.

It's not a problem if an instance overwrites the attribute. That won't affect the other instances. The problem comes when one instance mutates a mutable object stored in a class attribute. Then all instances of that class will see the change. That is probably a bug.

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it. I'll open a PR shortly to change all mutable class attributes to instance once. (At least all I can find.)

Copy link
Member

Choose a reason for hiding this comment

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

Thanks!

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 currently not used/set in any of the subclasses for this integration, and as far as I know, every other integration that actively uses attributes resets (replaces) it in their update method or implement their own property.

But I do see your point, it's easy to make a mistake here in the future.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla-signed code-quality integration: synology_dsm project-code_style PRs related to #53201 - pylint CodeStyle update smash Indicator this PR is close to finish for merging or closing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants