Search entityid through unique_id to avoid the user modifying entityid and causing command_send_mode to not match

This commit is contained in:
GavinIves 2025-07-08 03:10:33 +00:00
parent 8ad214a672
commit 1cf52a3f78
2 changed files with 16 additions and 9 deletions

View File

@ -48,11 +48,13 @@ Light entities for Xiaomi Home.
from __future__ import annotations from __future__ import annotations
import logging import logging
from tkinter import N
from typing import Any, Optional, List, Dict from typing import Any, Optional, List, Dict
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback 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 ( from homeassistant.components.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS,
ATTR_COLOR_TEMP_KELVIN, ATTR_COLOR_TEMP_KELVIN,
@ -123,6 +125,7 @@ class Light(MIoTServiceEntity, LightEntity):
self._prop_mode = None self._prop_mode = None
self._brightness_scale = None self._brightness_scale = None
self._mode_map = None self._mode_map = None
self._command_send_mode_entity_id = None
# properties # properties
for prop in entity_data.props: for prop in entity_data.props:
@ -252,8 +255,15 @@ class Light(MIoTServiceEntity, LightEntity):
# on # on
# Dirty logic for lumi.gateway.mgl03 indicator light # Dirty logic for lumi.gateway.mgl03 indicator light
# Determine whether the device sends the light-on properties in batches or one by one # 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" # Search entityid through unique_id to avoid the user modifying entityid and causing command_send_mode to not match
command_send_mode = self.hass.states.get(select_entity_id) 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": if command_send_mode and command_send_mode.state == "Send Together":
set_properties_list: List[Dict[str, Any]] = [] set_properties_list: List[Dict[str, Any]] = []
# Do not send the light on command here. Otherwise, # Do not send the light on command here. Otherwise,

View File

@ -88,11 +88,8 @@ async def async_setup_entry(
if miot_device.entity_list.get("light", []): if miot_device.entity_list.get("light", []):
device_id = list( device_id = list(
miot_device.device_info.get("identifiers"))[0][1] miot_device.device_info.get("identifiers"))[0][1]
light_entity_id = miot_device.gen_device_entity_id(DOMAIN)
new_light_select_entities.append( new_light_select_entities.append(
LightCommandSendMode(hass=hass, LightCommandSendMode(hass=hass, device_id=device_id))
light_entity_id=light_entity_id,
device_id=device_id))
if new_light_select_entities: if new_light_select_entities:
async_add_entities(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. then send other color temperatures and brightness or send them all at the same time.
The default is to send one by one.""" The default is to send one by one."""
def __init__(self, hass: HomeAssistant, light_entity_id: str, def __init__(self, hass: HomeAssistant, device_id: str):
device_id: str):
super().__init__() super().__init__()
self.hass = hass self.hass = hass
self._device_id = device_id self._device_id = device_id
self._attr_name = "Command Send Mode" 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 = [ self._attr_options = [
"Send One by One", "Send Turn On First", "Send Together" "Send One by One", "Send Turn On First", "Send Together"
] ]
self._attr_device_info = {"identifiers": {(DOMAIN, device_id)}} self._attr_device_info = {"identifiers": {(DOMAIN, device_id)}}
self._attr_current_option = self._attr_options[0] self._attr_current_option = self._attr_options[0]
self._attr_entity_category = EntityCategory.CONFIG self._attr_entity_category = EntityCategory.CONFIG