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

Xiaomi_aqara cover generated same entity with tail "_2" #13522

Closed
zaywalker opened this issue Mar 28, 2018 · 25 comments · Fixed by #19766 or #19770
Closed

Xiaomi_aqara cover generated same entity with tail "_2" #13522

zaywalker opened this issue Mar 28, 2018 · 25 comments · Fixed by #19766 or #19770

Comments

@zaywalker
Copy link

zaywalker commented Mar 28, 2018

Home Assistant release with the issue:

0.65.6

Last working Home Assistant release (if known):
0.65.6

Operating environment (Hass.io/Docker/Windows/etc.):

Raspberry pi with virtual env. followed install instruction.

Component/platform:

Xiaomi Gateway (Aqara) / Xiaomi Cover

Description of problem:
sometimes entity_registry.yaml makes same entity with "_2" tail and unique ids.

cover.curtain_158d0001fd5065:
  name:
  platform: xiaomi_aqara
  unique_id: status_status_pos_curtain_level158d0001fd5065
cover.curtain_158d0001fd5065_2:
  name:
  platform: xiaomi_aqara
  unique_id: pos_curtain_level_status_status158d0001fd5065

Problem-relevant configuration.yaml entries and (fill out even if it seems unimportant):

# Xiaomi Gateway
xiaomi_aqara:
  discovery_retry: 5
  gateways:
   - mac: !secret aqara_mac
     key: !secret aqara_key

Traceback (if applicable):


Additional information:

nothing related for this issue but I'm using with custom component for Smart Mi Fan from https://github.com/syssi/chuangmi_ir

follwing log is caused by offlined fan. Just i'm not sure this one affect or not.

2018-03-29 03:47:13 ERROR (Thread-7) [miio.device] Unable to discover a device at address 192.168.10.213
2018-03-29 03:47:13 ERROR (MainThread) [homeassistant.components.fan] Error while setting up platform smart_mi_fan
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.5/site-packages/homeassistant/helpers/entity_platform.py", line 82, in async_setup
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/lib/python3.5/asyncio/tasks.py", line 400, in wait_for
    return fut.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 293, in result
    raise self._exception
  File "/usr/lib/python3.5/concurrent/futures/thread.py", line 55, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/fan/smart_mi_fan.py", line 77, in setup_platform
    SmartMiFan(hass, name, host, token),
  File "/home/homeassistant/.homeassistant/custom_components/fan/smart_mi_fan.py", line 165, in __init__
    self._state_attrs = self.fan_get_prop()
  File "/home/homeassistant/.homeassistant/custom_components/fan/smart_mi_fan.py", line 404, in fan_get_prop
    "natural_level", "child_lock", "buzzer", "led_b"])
  File "/srv/homeassistant/lib/python3.5/site-packages/miio/device.py", line 210, in send
    self.do_discover()
  File "/srv/homeassistant/lib/python3.5/site-packages/miio/device.py", line 148, in do_discover
    raise DeviceException("Unable to discover the device %s" % self.ip)
miio.device.DeviceException: Unable to discover the device 192.168.10.213
@OttoWinter
Copy link
Member

This is the expected behavior. Entity IDs are generated based on the friendly name of the entity. And if the friendly name of two entities are the same, _2 _3 and so on are appended to the name to have unique IDs as before.

What the entity registry does is it allows you to manually change the entity id by the user. For example you can now rename your _2 entity to cover.curtain_cover. The entity registry isn't there to improve automatic entity id generating, but to allow users to manually change them.

@zaywalker
Copy link
Author

zaywalker commented Mar 29, 2018

@OttoWinter Thanks for the infomation about "_2" tailing and usage for entity registry. But the problem is if I made some automation with this entity, it's not working sometimes because the device is matched with another _2 entity, not the original one. I think xiaomi aqara platform has a bit problem because the device already has entity and unique id, the platform makes another one. Other xiaomi zigbee devices are super fine, but not the cover.
For the clear infomation, I have one cover not 2 :)

cover.curtain_158d0001fd5065:
unique_id: status_status_pos_curtain_level158d0001fd5065

cover.curtain_158d0001fd5065_2:
unique_id: pos_curtain_level_status_status158d0001fd5065

cover has more than one prefix for generated unique_id.

sensor.temperature_158d0001ddb487:
  name:
  platform: xiaomi_aqara
  unique_id: temperature158d0001ddb487
binary_sensor.switch_158d00016da2a7:
  name:
  platform: xiaomi_aqara
  unique_id: status158d00016da2a7
binary_sensor.door_window_sensor_158d00016fdc87:
  name:
  platform: xiaomi_aqara
  unique_id: status158d00016fdc87
binary_sensor.motion_sensor_158d00012300c1:
  name:
  platform: xiaomi_aqara
  unique_id: status158d00012300c1

Other devices have just one prefix.

@OttoWinter
Copy link
Member

First up, I don't know anything about xiaomi, but there are probably reasons for splitting up the cover platform into two entities. Now, I think the behavior you're referring to with having "unstable" entity IDs is from before the entity registry times. Because now, you can override which entity ID home assistant uses for entities in the entity registry. For example, if you want to rename the entity ID of your _2 cover you can just put this in your entity_registry.yaml:

cover.curtain_number_one:
  name:
  platform: xiaomi_aqara
  unique_id: status_status_pos_curtain_level158d0001fd5065
cover.curtain_number_two:
  name:
  platform: xiaomi_aqara
  unique_id: pos_curtain_level_status_status158d0001fd5065

Now the entity IDs of the covers should be cover.curtain_number_one and cover.curtain_number_two (apart from #13316, but I doubt that's the case here). Once you put that in your registry, Home Assistant will no longer suffix the entity ids with numbers like _2. Again, I have no idea why xiaomi does this with 2 cover entities, but there are probably reasons for that.

@zaywalker
Copy link
Author

@OttoWinter OK I see. I understand there is no way to merge these entities fundamentally or prevent split. The weird thing is if I don't reboot or restart HA, it's just fine not be split. But for the automation, I'll handle two entities. Thanks again with kind explanation about this problem. :)

@balloobbot
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@GuGu927
Copy link

GuGu927 commented Jul 26, 2018

@balloobbot Same issues. version 0.74.0
cover.curtain_158d00020e43f1:
platform: xiaomi_aqara
unique_id: status_status_pos_curtain_level158d00020e43f1

cover.curtain_158d00020e43f1_2:
platform: xiaomi_aqara
unique_id: pos_curtain_level_status_status158d00020e43f1

These 2 curtains are same curtain.
But "unique_id" is different.
Above is "status_status_pos_curtain_level .... "
Below is " pos_curtain_level_status_status .... "

please help me.

@awarecan
Copy link
Contributor

LOL, it is a bug.

In cover.xaiomi_aqara, when we create XiaominDevice, we pass in a dict as _data_key

https://github.com/home-assistant/home-assistant/blob/9fb8bc8991eda47488e2bfeac37c1d41378a9174/homeassistant/components/cover/xiaomi_aqara.py#L20-L23

And in xiaomi_aqara, we use _data_key as part of device's _unique_id

https://github.com/home-assistant/home-assistant/blob/9fb8bc8991eda47488e2bfeac37c1d41378a9174/homeassistant/components/xiaomi_aqara.py#L227-L229

Fun thing here is in python 3.5 and below, dict will not preserve the order, the output of dict format will be random. So after several HA restart, we end up have two entity id.

cc @syssi @Danielhiversen

@awarecan awarecan changed the title Sometimes entity_registry.yaml makes same entity with tail "_2" Xiaomi_aqara cover generated same entity with tail "_2" Jul 26, 2018
@frekel
Copy link

frekel commented Jul 30, 2018

If this is fixed, will:
homeassistant.exceptions.HomeAssistantError: Entity id already exists: binary_sensor.motion_sensor_xxxxxxxxxxxxxxxxxx. Platform xiaomi_aqara does not generate unique IDs
also be fixed?

@frekel
Copy link

frekel commented Oct 11, 2018

@awarecan: Do you know if this bug is already fixed?

@frekel
Copy link

frekel commented Oct 17, 2018

@Danielhiversen I know we are all busy, but if I buy you a beer, can you take a look here?

@Danielhiversen
Copy link
Member

I do not have any xioami curtains, but can you try this
https://github.com/home-assistant/home-assistant/compare/dev...unique_id_xiaomi?quick_pull=1

What other sensor does your motion sensor has?
Can you post their entity id?

@frekel

This comment has been minimized.

@frekel

This comment has been minimized.

@awarecan

This comment has been minimized.

@frekel

This comment has been minimized.

@awarecan

This comment has been minimized.

@sungokay
Copy link

I do not have any xioami curtains, but can you try this
https://github.com/home-assistant/home-assistant/compare/dev...unique_id_xiaomi?quick_pull=1

What other sensor does your motion sensor has?
Can you post their entity id?

        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "sensor.illumination_7811dcb8fc90",
            "name": "Gateway Bright",
            "platform": "xiaomi_aqara",
            "unique_id": "illumination7811dcb8fc90"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.switch_158d0001f47471",
            "name": "Switch Bathroom",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001f47471"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.switch_158d000214e8b5",
            "name": "Switch Bedroom",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d000214e8b5"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.motion_sensor_158d0001d557b4",
            "name": "Motion Bedroom",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001d557b4"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.motion_sensor_158d0001d5562c",
            "name": "Motion Living room",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001d5562c"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.door_window_sensor_158d0002286d34",
            "name": "Contact Right",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0002286d34"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.door_window_sensor_158d00022b9747",
            "name": "Contact Left",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d00022b9747"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.motion_sensor_158d0001d55679",
            "name": "Motion Bathroom",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001d55679"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.motion_sensor_158d0001d5578c",
            "name": "Motion Kitchen",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001d5578c"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.switch_158d0001e7361b",
            "name": "Switch Door",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001e7361b"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.door_window_sensor_158d0001fd5298",
            "name": "Contact Door",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001fd5298"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.motion_sensor_158d0001d55790",
            "name": "Motion Door",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d0001d55790"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "light.gateway_light_7811dcb8fc90",
            "name": "Gateway Light",
            "platform": "xiaomi_aqara",
            "unique_id": "rgb7811dcb8fc90"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.cube_158d00027d06c0",
            "name": "Cube Living room",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d00027d06c0"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "binary_sensor.cube_158d00027d8fc4",
            "name": "Cube Bedroom",
            "platform": "xiaomi_aqara",
            "unique_id": "status158d00027d8fc4"
        },
        {
            "config_entry_id": null,
            "device_id": null,
            "disabled_by": null,
            "entity_id": "cover.curtain_158d00022c80b5",
            "name": "Curtain",
            "platform": "xiaomi_aqara",
            "unique_id": "curtain158d00022c80b5"
        }

@ReX1983
Copy link

ReX1983 commented Nov 11, 2018

I have a similar issue. Since a couple of weeks (possibly one of the last updates) one of my door-window sensors stopped working in HA.

The error is:

1471-2018-11-11 16:48:36 WARNING (MainThread) [homeassistant.setup] Setup of xiaomi_aqara is taking over 10 seconds.
1583-2018-11-11 16:48:37 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
1695-Traceback (most recent call last):
1730-  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 346, in _async_add_entity
1859-    msg)
1868:homeassistant.exceptions.HomeAssistantError: Entity id already exists: binary_sensor.door_window_sensor_158d00014df7be. Platform xiaomi_aqara does not generate unique IDs

I have 62 devices, 3 gateways and it is always this sensor causing problems (but it works fine in the Xiaomi app & automations so I tend to exclude it is and hardware or battery issue).

Would it make sense to manually remove the entity from core.entity_registry and see if it gets added correctly?

@DavidLP
Copy link
Contributor

DavidLP commented Dec 27, 2018

This is not only an issue for covers anymore, but for all xiaomi_aquara devices. They suddenly got a _2 suffix breaking all automations:
grafik

@princehaku
Copy link

em... this bug works for me...

@princehaku
Copy link

em... this bug works for me...

i have solved this by upgrading my python verion to 3.7 in pi

@syssi
Copy link
Member

syssi commented Jan 4, 2019

@DavidLP May be you are affected of this recent change: #19192

@syssi syssi self-assigned this Jan 4, 2019
syssi added a commit to syssi/home-assistant that referenced this issue Jan 4, 2019
@ghost ghost added the in progress label Jan 4, 2019
@mouth4war
Copy link

@syssi I got hit by this too. Will your fix restore slugified ids?

@syssi
Copy link
Member

syssi commented Jan 4, 2019

Yes. The PR will fix/avoid the issue. I hope you haven't touched your .storage/core.entity_registry in the meantime.

@ghost ghost removed the in progress label Jan 4, 2019
@awarecan
Copy link
Contributor

awarecan commented Jan 5, 2019

This issue will not be fixed by #19770, but #19766, reopen it.

@awarecan awarecan reopened this Jan 5, 2019
syssi added a commit that referenced this issue Jan 7, 2019
balloob pushed a commit that referenced this issue Jan 9, 2019
@balloob balloob mentioned this issue Jan 10, 2019
rymsha pushed a commit to rymsha/home-assistant that referenced this issue Jan 12, 2019
rymsha pushed a commit to rymsha/home-assistant that referenced this issue Jan 12, 2019
alandtse pushed a commit to alandtse/home-assistant that referenced this issue Feb 12, 2019
This was referenced May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment