From 1cf52a3f78679dd9da47f5d9762edbaa5300f3bd Mon Sep 17 00:00:00 2001 From: GavinIves Date: Tue, 8 Jul 2025 03:10:33 +0000 Subject: [PATCH] Search entityid through unique_id to avoid the user modifying entityid and causing command_send_mode to not match --- custom_components/xiaomi_home/light.py | 14 ++++++++++++-- custom_components/xiaomi_home/select.py | 11 ++++------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/custom_components/xiaomi_home/light.py b/custom_components/xiaomi_home/light.py index 894f494..3f8c07d 100644 --- a/custom_components/xiaomi_home/light.py +++ b/custom_components/xiaomi_home/light.py @@ -48,11 +48,13 @@ Light entities for Xiaomi Home. from __future__ import annotations import logging +from tkinter import N from typing import Any, Optional, List, Dict from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_COLOR_TEMP_KELVIN, @@ -123,6 +125,7 @@ class Light(MIoTServiceEntity, LightEntity): self._prop_mode = None self._brightness_scale = None self._mode_map = None + self._command_send_mode_entity_id = None # properties for prop in entity_data.props: @@ -252,8 +255,15 @@ class Light(MIoTServiceEntity, LightEntity): # on # Dirty logic for lumi.gateway.mgl03 indicator light # Determine whether the device sends the light-on properties in batches or one by one - select_entity_id = f"select.{self.miot_device.gen_device_entity_id(DOMAIN).split('.')[-1]}_command_send_mode" - command_send_mode = self.hass.states.get(select_entity_id) + # Search entityid through unique_id to avoid the user modifying entityid and causing command_send_mode to not match + if self._command_send_mode_entity_id is None: + entity_registry = async_get_entity_registry(self.hass) + device_id = list( + self.miot_device.device_info.get("identifiers"))[0][1] + self._command_send_mode_entity_id = entity_registry.async_get_entity_id( + "select", DOMAIN, f"light_{device_id}_command_send_mode") + command_send_mode = self.hass.states.get( + self._command_send_mode_entity_id) if command_send_mode and command_send_mode.state == "Send Together": set_properties_list: List[Dict[str, Any]] = [] # Do not send the light on command here. Otherwise, diff --git a/custom_components/xiaomi_home/select.py b/custom_components/xiaomi_home/select.py index 8697f95..0f16cff 100644 --- a/custom_components/xiaomi_home/select.py +++ b/custom_components/xiaomi_home/select.py @@ -88,11 +88,8 @@ async def async_setup_entry( if miot_device.entity_list.get("light", []): device_id = list( miot_device.device_info.get("identifiers"))[0][1] - light_entity_id = miot_device.gen_device_entity_id(DOMAIN) new_light_select_entities.append( - LightCommandSendMode(hass=hass, - light_entity_id=light_entity_id, - device_id=device_id)) + LightCommandSendMode(hass=hass, device_id=device_id)) if new_light_select_entities: async_add_entities(new_light_select_entities) @@ -123,16 +120,16 @@ class LightCommandSendMode(SelectEntity, RestoreEntity): then send other color temperatures and brightness or send them all at the same time. The default is to send one by one.""" - def __init__(self, hass: HomeAssistant, light_entity_id: str, - device_id: str): + def __init__(self, hass: HomeAssistant, device_id: str): super().__init__() self.hass = hass self._device_id = device_id self._attr_name = "Command Send Mode" - self._attr_unique_id = f"{light_entity_id}_command_send_mode" + self._attr_unique_id = f"light_{device_id}_command_send_mode" self._attr_options = [ "Send One by One", "Send Turn On First", "Send Together" ] + self._attr_device_info = {"identifiers": {(DOMAIN, device_id)}} self._attr_current_option = self._attr_options[0] self._attr_entity_category = EntityCategory.CONFIG