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

Keep the provided name when creating a tag #128240

Merged
merged 3 commits into from
Oct 14, 2024

Conversation

silamon
Copy link
Contributor

@silamon silamon commented Oct 12, 2024

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:

@home-assistant
Copy link

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

Code owner commands

Code owners of tag 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 tag 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.

@gjohansson-ST
Copy link
Member

@silamon
Copy link
Contributor Author

silamon commented Oct 13, 2024

Is there a bug in this test then? https://github.com/home-assistant/core/blob/dev/tests/components/tag/test_init.py#L269

No, the (websocket) handler works correctly, what is being tested in this test, what is also being mentioned in both issues. There's a bug in the attached listener.

@gjohansson-ST
Copy link
Member

Isn't that test case what's happening when creating a tag (as mentioned in the issue)?
A message that comes in creates the Kitchen tag which do have a proper name. I would expect this test to fail then given it would have the tag id in the name and not the provided name.

@silamon
Copy link
Contributor Author

silamon commented Oct 13, 2024

Isn't that test case what's happening when creating a tag (as mentioned in the issue)? A message that comes in creates the Kitchen tag which do have a proper name. I would expect this test to fail then given it would have the tag id in the name and not the provided name.

It doesn't because the provided name is taken in consideration. Shortly after, it is "removed" in the attached listeners by not passing in the name.

Steps:

  1. User creates or scan a tag
  2. Tag is being passed to backend and an entity is created
  3. Tag is being passed again to frontend (here is where the test stops)
  4. A listener modifies the entity and removes the provided name again
  5. User refreshs, and sees the original name

@gjohansson-ST
Copy link
Member

I think we should update the _create_entry method instead.
Also I added to test the name in the test.

diff --git a/homeassistant/components/tag/__init__.py b/homeassistant/components/tag/__init__.py
index 0462c5bec3..95efae3d38 100644
--- a/homeassistant/components/tag/__init__.py
+++ b/homeassistant/components/tag/__init__.py
@@ -84,7 +84,9 @@ def _create_entry(
         original_name=f"{DEFAULT_NAME} {tag_id}",
         suggested_object_id=slugify(name) if name else tag_id,
     )
-    return entity_registry.async_update_entity(entry.entity_id, name=name)
+    if name:
+        return entity_registry.async_update_entity(entry.entity_id, name=name)
+    return entry
 
 
 class TagStore(Store[collection.SerializedStorageCollection]):
diff --git a/tests/components/tag/test_init.py b/tests/components/tag/test_init.py
index 6f309391d2..5c1e80c2d8 100644
--- a/tests/components/tag/test_init.py
+++ b/tests/components/tag/test_init.py
@@ -294,6 +294,10 @@ async def test_entity_created_and_removed(
     assert item["id"] == "1234567890"
     assert item["name"] == "Kitchen tag"
 
+    await hass.async_block_till_done()
+    er_entity = entity_registry.async_get("tag.kitchen_tag")
+    assert er_entity.name == "Kitchen tag"
+
     entity = hass.states.get("tag.kitchen_tag")
     assert entity
     assert entity.state == STATE_UNKNOWN

@silamon
Copy link
Contributor Author

silamon commented Oct 14, 2024

I've applied your changes since they make sense.

I think we should update the _create_entry method instead.

I'm not sure if I still have to do something for this.
The _create_entry is called twice:

  1. Upon creation and it mentions specifically above: " Create entity in entity_registry when creating the tag. This is done early to store name only once in entity registry"
  2. When the tag change listener is fired.

It is also run upon restarting, which may update the entity_registry unneccessary.

@gjohansson-ST
Copy link
Member

I'm not sure if I still have to do something for this. The _create_entry is called twice:

I noticed that also when I looked at it so I think it should be looked at but it's slightly out of scope for this PR which is for solving the name bug.
I have added it to my list but if you want to take a look feel free 👍

@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@home-assistant home-assistant bot marked this pull request as draft October 14, 2024 16:35
@silamon silamon marked this pull request as ready for review October 14, 2024 18:54
Copy link
Member

@gjohansson-ST gjohansson-ST left a comment

Choose a reason for hiding this comment

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

Thanks @silamon 👍

All test failures are not related to this PR.

@gjohansson-ST gjohansson-ST added this to the 2024.10.3 milestone Oct 14, 2024
@gjohansson-ST gjohansson-ST merged commit 866912d into home-assistant:dev Oct 14, 2024
37 of 41 checks passed
tetele pushed a commit to tetele/ha-core that referenced this pull request Oct 14, 2024
* Keep the name

* Add patch

* Update homeassistant/components/tag/__init__.py

Co-authored-by: G Johansson <[email protected]>

---------

Co-authored-by: G Johansson <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Oct 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
5 participants