fix: resolve invalid entity ID warnings#1639
Conversation
| description: str) -> str: | ||
| return ( | ||
| f'{ha_domain}.{self._model_strs[0][:9]}_{self.did_tag}_' | ||
| f'{self._model_strs[-1][:20]}_s_{siid}_{slugify_name(description)}') |
There was a problem hiding this comment.
Suggest adding a fallback in gen_service_entity_id when slugify_name(description) returns empty
There was a problem hiding this comment.
I've confirmed it still works when empty. I'm just not sure what would make a better fallback here.
There was a problem hiding this comment.
2026-02-06 15:04:08.456 ERROR (MainThread) [homeassistant.components.cover] Error adding entity xiaomi_home.lumi_cn_1049257520_hmcn02_s_2_ for domain cover with platform xiaomi_home
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 679, in _async_add_entities
await self._async_add_entity(
entity, False, entity_registry, config_subentry_id
)
File "/usr/src/homeassistant/homeassistant/helpers/entity_platform.py", line 826, in _async_add_entity
raise HomeAssistantError(f"Invalid entity ID: {entity.entity_id}")
homeassistant.exceptions.HomeAssistantError: Invalid entity ID: xiaomi_home.lumi_cn_1049257520_hmcn02_s_2_
The entity ID ends with a trailing underscore _s_2_ because the service description is empty.
Why the description is empty
The MIoT vendor-specific spec for this device (urn:...:curtain:00007816:lumi-hmcn02:1:0000C817) returns "description": "" from the cloud API. The description_trans is correctly resolved to "Curtain" via std_lib.service_translate(), but description (used in gen_service_entity_id) remains empty.
Why HA 2026.2 rejects trailing underscores
HA 2026.2 tightened entity ID validation — IDs must match ^[a-z][a-z0-9_]*[a-z0-9]$ (cannot start or end with _). See home-assistant/core#137065. A trailing _ now raises HomeAssistantError during entity registration.
In addition, the entity ID of my cover before the update was cover.lumi_cn_1049257520_hmcn02_s_2_2
There was a problem hiding this comment.
Thanks for catching that! I’m honestly not sure why my previous tests passed.
6ffeb55 to
9159a98
Compare
|
Thanks for the update! The new commit fixes the entity_id generation for empty descriptions — devices now load correctly. However, there's a migration gap for users who deployed the previous version of this PR. ProblemSince the old code had This causes a unique_id mismatch:
HA can't find the existing entity and creates a new one, orphaning the old entity_id (including any HA-generated dedup suffix). Observed ResultWhat I observed on my system after upgrading:
SuggestionAdd a fallback migration that also checks for stale v2-format unique_ids when the config entry is already version 3. |
This is getting a bit messy. Does this mean we'll need v4? |
|
In short, when HA encounters conflicts when generating entity_id, it automatically adds a serial number (_2, _3, ...) to the end of the entity_id. Given my device's configuration, I'm unsure why these conflicts occurred; perhaps a reinstallation caused them. As for whether the version number should be v4, it depends on whether there is a need to resolve the migration of conflicting entity_ids, and how many people have pulled this PR (I haven't encountered this issue; I've already handled it manually). By the way, the HA official team has relaxed the restrictions until 2027.2.0:
|
|
@parkghost 我不太想继续增加复杂度,毕竟这个中间版本也只存在了几个小时。 这里我提供一个手动操作方法来帮助 拉取过这个PR初始提交(372a579) 的用户,迁移剩下的几个没有description的实体。 如果你又拉取了最新提交(9159a98),导致已经产生重复实体的情况,那么首先要把这些新建出来的实体删掉,参考上面的例子就是 created_at 2026-02-06 的那些。
这样就相当于回到了前一个有点小问题的v3,接下来要手动把版本号改到2,然后重跑一下新的2->3迁移。
应该就好了 |
Glad to see the HA official team relaxed the restrictions; looks like I was just a bit too early with this PR! |
問題的原因與解決方式我並沒有深入研究,主要是回報我遇到的問題狀況,請仍以您的判斷為準 由於 HA 官方目前僅將限制放寬至 2027.2.0,未來 謝謝您的協助 |
|
I don’t understand Chinese. Can we update HA already to 2026.2? |
Yes, you can update to 2026.2.1 now. (not 2026.2.0) |
Update
HA 2026.2.1 已修复此问题,将限制放宽到 2027.2.0
此 PR 仍有价值,这里提供了一种迁移到合法 unique_id 的方案。
The current warning:
Why
更新到 HA 2026.2.0 之后,由于 Invalid entity ID 会导致部分实体不可用。
Fixed
这个 PR 是给那些有着大量设备、大量界面配置和自动化的用户用的。
如果只是简单更换 unique_id 的方式修,而不提供迁移步骤,会导致大量旧的配置不可用。就和之前 #972 那样。
由于目前官方还没有出解决方案,这里先简单使用
slugify_name来处理不合法的 unique_id。这个 PR 也包含了 #972 的代码,使用了一样的方式来实现 unique_id 的修改,
不管是 v1 还是 v2 的用户都可以直接使用,使用之后版本号会变成 3。
fix: #1627
fix: #1632
fix: #1634
fix: #1635
fix: #1636
fix: #1638
Usage
Caution
版本号变了之后就不能直接切回主线了,(但现在主线还没修,本来就也没法切回去)