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

Reboot buttons for Shelly devices is trying to use a unique_id that's already in use. #95560

Closed
Petro31 opened this issue Jun 29, 2023 · 16 comments · Fixed by #95707
Closed

Reboot buttons for Shelly devices is trying to use a unique_id that's already in use. #95560

Petro31 opened this issue Jun 29, 2023 · 16 comments · Fixed by #95707

Comments

@Petro31
Copy link
Contributor

Petro31 commented Jun 29, 2023

The problem

Title explains it all. The integration is attempting to reuse a unique_id that's already taken by another entity it has created. The entity with the issue appears to be the reboot button for each device. I have 2 shelly devices, this error appears twice in my logs for each device.

2023-06-29 10:40:22.126 ERROR (MainThread) [homeassistant.components.button] Error while setting up shelly platform for button
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 353, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/shelly/button.py", line 136, in async_setup_entry
    await er.async_migrate_entries(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_registry.py", line 1328, in async_migrate_entries
    ent_reg.async_update_entity(entry.entity_id, **updates)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_registry.py", line 936, in async_update_entity
    return self._async_update_entity(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_registry.py", line 882, in _async_update_entity
    raise ValueError(
ValueError: Unique id 'F4CFA2E45D78_reboot' is already in use by 'button.washer_and_dryer_reboot'
2023-06-29 10:40:22.239 WARNING (MainThread) [homeassistant.helpers.entity] Entity None (<class 'homeassistant.components.wled.light.WLEDSegmentLight'>) is implicitly using device name by not setting its name. Instead, the name should be set to None, please create a bug report at https://github.com/home-assistant/core/issues?q=is%3Aopen+is%3Aissue+label%3A%22integration%3A+wled%22
2023-06-29 10:40:22.251 ERROR (MainThread) [homeassistant.components.button] Error while setting up shelly platform for button
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 353, in _async_setup_platform
    await asyncio.shield(task)
  File "/usr/src/homeassistant/homeassistant/components/shelly/button.py", line 136, in async_setup_entry
    await er.async_migrate_entries(
  File "/usr/src/homeassistant/homeassistant/helpers/entity_registry.py", line 1328, in async_migrate_entries
    ent_reg.async_update_entity(entry.entity_id, **updates)
  File "/usr/src/homeassistant/homeassistant/helpers/entity_registry.py", line 936, in async_update_entity
    return self._async_update_entity(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_registry.py", line 882, in _async_update_entity
    raise ValueError(
ValueError: Unique id 'C45BBE603D98_reboot' is already in use by 'button.shelly_em3_reboot'

What version of Home Assistant Core has the issue?

core-2023.7.0b0

What was the last working version of Home Assistant Core?

core-2023.6.3

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

shelly

Link to integration documentation on our website

https://www.home-assistant.io/integrations/shelly/

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

No response

Additional information

No response

@home-assistant
Copy link

Hey there @balloob, @bieniu, @thecode, @chemelli74, @bdraco, mind taking a look at this issue as it has been labeled with an integration (shelly) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of shelly can trigger bot actions by commenting:

  • @home-assistant close Closes the issue.
  • @home-assistant rename Awesome new title Renames the issue.
  • @home-assistant reopen Reopen the issue.
  • @home-assistant unassign shelly Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


shelly documentation
shelly source
(message by IssueLinks)

@frenck frenck added this to the 2023.7.0 milestone Jun 29, 2023
@bieniu
Copy link
Member

bieniu commented Jun 29, 2023

I'm not able to reproduce the problem. Could you show us the reboot button entries from /config/.storage/core.entity_registry file and device entries from /config/.storage/core.config_entries.

@Petro31
Copy link
Contributor Author

Petro31 commented Jun 29, 2023

wahser_dryer entity registry:

{
        "aliases": [],
        "area_id": null,
        "capabilities": null,
        "config_entry_id": "faf5cce0f97195c483248b4a8c2a92e1",
        "device_class": null,
        "device_id": "6b4d9d9e74f3c6e38704696d104ec525",
        "disabled_by": null,
        "entity_category": "config",
        "entity_id": "button.washer_and_dryer_reboot",
        "hidden_by": null,
        "icon": null,
        "id": "606ce5974bf6fdfbb60ffb5fb5e7dca9",
        "has_entity_name": false,
        "name": null,
        "options": {
          "conversation": {
            "should_expose": false
          }
        },
        "original_device_class": "restart",
        "original_icon": null,
        "original_name": "Washer and Dryer Reboot",
        "platform": "shelly",
        "supported_features": 0,
        "translation_key": null,
        "unique_id": "F4CFA2E45D78_reboot",
        "unit_of_measurement": null
      },

paired config entry

      {
        "entry_id": "faf5cce0f97195c483248b4a8c2a92e1",
        "version": 1,
        "domain": "shelly",
        "title": "Washer and Dryer",
        "data": {
          "host": "192.168.1.9",
          "sleep_period": 0,
          "model": "SHEM",
          "gen": 1
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "user",
        "unique_id": "F4CFA2E45D78",
        "disabled_by": null
      },

C45BBE603D98_reboot has 2 matches

entity_registry

      {
        "aliases": [],
        "area_id": null,
        "capabilities": null,
        "config_entry_id": "6fa03fca5aa4079c0d22bf14b76d414a",
        "device_class": null,
        "device_id": "d0779a233ede3216f455e2a770446dd1",
        "disabled_by": null,
        "entity_category": "config",
        "entity_id": "button.shelly_em3_reboot",
        "hidden_by": null,
        "icon": null,
        "id": "f5a52702562b90344bebf7865aa4a6d4",
        "has_entity_name": false,
        "name": "Shelly EM3 Reboot",
        "options": {
          "conversation": {
            "should_expose": false
          }
        },
        "original_device_class": "restart",
        "original_icon": null,
        "original_name": "shellyem3-C45BBE603D98 Reboot",
        "platform": "shelly",
        "supported_features": 0,
        "translation_key": null,
        "unique_id": "C45BBE603D98_reboot",
        "unit_of_measurement": null
      },

paired config entry

      {
        "entry_id": "6fa03fca5aa4079c0d22bf14b76d414a",
        "version": 1,
        "domain": "shelly",
        "title": "shellyem3-C45BBE603D98",
        "data": {
          "host": "192.168.1.200",
          "sleep_period": 0,
          "model": "SHEM-3",
          "gen": 1
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "zeroconf",
        "unique_id": "C45BBE603D98",
        "disabled_by": null
      },

2nd

      {
        "aliases": [],
        "area_id": null,
        "capabilities": null,
        "config_entry_id": "6fa03fca5aa4079c0d22bf14b76d414a",
        "device_class": null,
        "device_id": "d0779a233ede3216f455e2a770446dd1",
        "disabled_by": null,
        "entity_category": "config",
        "entity_id": "button.shellyem3_c45bbe603d98_reboot",
        "hidden_by": null,
        "icon": null,
        "id": "927c3483bc1dc7c7fa70b54343c92587",
        "has_entity_name": false,
        "name": null,
        "options": {
          "conversation": {
            "should_expose": false
          }
        },
        "original_device_class": "restart",
        "original_icon": null,
        "original_name": "shellyem3-C45BBE603D98 Reboot",
        "platform": "shelly",
        "supported_features": 0,
        "translation_key": null,
        "unique_id": "shellyem3_c45bbe603d98_reboot",
        "unit_of_measurement": null
      }

matched entry

      {
        "entry_id": "6fa03fca5aa4079c0d22bf14b76d414a",
        "version": 1,
        "domain": "shelly",
        "title": "shellyem3-C45BBE603D98",
        "data": {
          "host": "192.168.1.200",
          "sleep_period": 0,
          "model": "SHEM-3",
          "gen": 1
        },
        "options": {},
        "pref_disable_new_entities": false,
        "pref_disable_polling": false,
        "source": "zeroconf",
        "unique_id": "C45BBE603D98",
        "disabled_by": null
      },

@bieniu
Copy link
Member

bieniu commented Jun 30, 2023

There are two entries in your registry for the reboot entity. You probably renamed the device (from ShellyEM3-C45BBE603D98 to ShellyEM3) and a second reboot entity was created by a bug in the code. Could you check if there is a second reboot entity (unavailable) for this device?
Now the new code tries to migrate both entities to the new unique_id scheme and the result is duplicate unique_id. I think I'll be able to reproduce it. The question is how to fix it 🤔

@Petro31
Copy link
Contributor Author

Petro31 commented Jun 30, 2023

I'll just edit my registry. No idea how this even happened. Haven't touched those entities in a year or so.

@bieniu
Copy link
Member

bieniu commented Jun 30, 2023

There was a bug in the integration code, when the user renamed the device, new button entities were created and the "old" ones became unavailable.

@Petro31
Copy link
Contributor Author

Petro31 commented Jun 30, 2023

Oh, if that bug is fixed, I'll just delete the wrong config entry wheenver I get a chance

@Petro31
Copy link
Contributor Author

Petro31 commented Jun 30, 2023

Still doesn't make sense why I'm getting 2 errors when only 1 of them has a double entry, but maybe I did a bad job looking for it.

@thecode
Copy link
Member

thecode commented Jun 30, 2023

There are two entries in your registry for the reboot entity. You probably renamed the device (from ShellyEM3-C45BBE603D98 to ShellyEM3) and a second reboot entity was created by a bug in the code. Could you check if there is a second reboot entity (unavailable) for this device? Now the new code tries to migrate both entities to the new unique_id scheme and the result is duplicate unique_id. I think I'll be able to reproduce it. The question is how to fix it 🤔

I was thinking if we find a button entity that is already migrated we delete all entities that has _reboot in their name and stop the migration. This means that if there are multiple we may migrate only one and maybe not the correct one but it would happen now anyhow. There is no risk from deleting entities since it runs only on the button platform.

@bieniu
Copy link
Member

bieniu commented Jul 1, 2023

I did this test today:

  • on HA 2023.6.3 I configured the device
  • I renamed the device and restarted the HA after which a new Reboot entity appeared
  • updated HA to 2023.7.0 beta

and the unique_id migration went fine and no error occurred.
However, the old Reboot entity is still visible in HA as unavailable.

obraz

What am I missing?

@thecode
Copy link
Member

thecode commented Jul 1, 2023

I did this test today:

  • on HA 2023.6.3 I configured the device
  • I renamed the device and restarted the HA after which a new Reboot entity appeared
  • updated HA to 2023.7.0 beta

and the unique_id migration went fine and no error occurred. However, the old Reboot entity is still visible in HA as unavailable.

obraz

What am I missing?

I don't think you are missing something. I think we can wait to see if other have this issue. Anyhow I was thinking maybe we can check if the device MAC (which we use as part of the unique ID) is not part of the entity and delete entities without MAC. This will ensure we clean-up old entities. WDYT?

@bieniu
Copy link
Member

bieniu commented Jul 1, 2023

I will repeat my test with two devices Plus Plug S and Gas so there will be more buttons to migrate. Maybe with more entities there is some edge case.
Yes, we should definitely clean up the “old” entities.

@bieniu
Copy link
Member

bieniu commented Jul 2, 2023

I was able to reproduce the error with Shelly Gas (4 button entities) and I will prepare the fix.

@tomlut
Copy link

tomlut commented Jul 6, 2023

I was just about to open an issue for this after updating to core 2023.7.0

I have indeed renamed my devices.

Do I need to edit the .storage registry or will your PR fix this when it is released?

@bieniu
Copy link
Member

bieniu commented Jul 6, 2023

Fix is a part of the HA 2023.7.0 release. Probably you need to remove unavailable button entities in the UI. Please do not edit the registry.

You can share your registry here so that we can analyze it.

@tomlut
Copy link

tomlut commented Jul 6, 2023

Thank you. Deleting the unavailable buttons and restarting worked perfectly.

@github-actions github-actions bot locked and limited conversation to collaborators Aug 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants