From 2ac5880eb6c404a97181811f94f8b0d78b2b96a1 Mon Sep 17 00:00:00 2001 From: Cyborg2017 Date: Tue, 6 Jan 2026 22:06:07 +0800 Subject: [PATCH] feat: add command template support for device control - Add command field support to MideaNumberEntity and MideaSelectEntity - Command templates allow defining fixed parameters in configuration - Options/values are merged with command template before sending to device - Backward compatible - existing configurations continue to work Examples in device_mapping: 1. For NUMBER platform - brightness control with protocol template: ```yaml Platform.NUMBER: { "lightness": { "min": 10, "max": 100, "step": 5, "command": { "electronic_control_version": 2, "type": "b6", "b6_action": "setting", "setting": "light", "lightness": "{value}" # {value} placeholder for actual number value } } } 2.For SELECT platform - gesture selection with protocol template: ```yaml Platform.SELECT: { "gesture": { "options": { "off": {"gesture": "off"}, "on": {"gesture": "on"} }, "command": { # Protocol template for gesture control "electronic_control_version": 2, "type": "b6", "b6_action": "setting", "setting": "gesture" # {gesture} value from options will be merged automatically } } } --- .../midea_auto_cloud/device_mapping/T0xB6.py | 20 +++++++++++-------- custom_components/midea_auto_cloud/number.py | 12 +++++++++++ custom_components/midea_auto_cloud/select.py | 10 +++++++++- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/custom_components/midea_auto_cloud/device_mapping/T0xB6.py b/custom_components/midea_auto_cloud/device_mapping/T0xB6.py index 77e1d4d..e05729a 100644 --- a/custom_components/midea_auto_cloud/device_mapping/T0xB6.py +++ b/custom_components/midea_auto_cloud/device_mapping/T0xB6.py @@ -44,14 +44,6 @@ DEVICE_MAPPING = { } }, Platform.BUTTON: { - "light_off": { - "command": {"electronic_control_version": 2, "type": "b6", "b6_action": "setting", - "setting": "light", "light": "off"}, - }, - "light_on": { - "command": {"electronic_control_version": 2, "type": "b6", "b6_action": "setting", - "setting": "light", "light": "on"}, - }, "left_stove_off": { "command": {"electronic_control_version": 2, "type": "b7", "b7_work_burner_control": 1, "b7_function_control": 1}, @@ -84,6 +76,18 @@ DEVICE_MAPPING = { "extreme": {"gear": 4}, } }, + "light": { + "options": { + "off": {"light": "off"}, + "on": {"light": "on"} + }, + "command": { + "electronic_control_version": 2, + "type": "b6", + "b6_action": "setting", + "setting": "light" + } + } }, } } diff --git a/custom_components/midea_auto_cloud/number.py b/custom_components/midea_auto_cloud/number.py index c0733f3..fa80549 100644 --- a/custom_components/midea_auto_cloud/number.py +++ b/custom_components/midea_auto_cloud/number.py @@ -107,6 +107,18 @@ class MideaNumberEntity(MideaEntity, NumberEntity): """Set the value of the number entity.""" # 确保值在有效范围内 value = max(self._min_value, min(self._max_value, value)) + + # 首先尝试使用command字段(如果存在) + command = self._config.get("command") + if command and isinstance(command, dict): + # 替换{value}模板 + import copy + merged_command = copy.deepcopy(command) + for key, val in merged_command.items(): + if isinstance(val, str) and "{value}" in val: + merged_command[key] = val.replace("{value}", str(int(value))) + await self.async_set_attributes(merged_command) + return # Use attribute from config if available, otherwise fall back to entity_key attribute = self._config.get("attribute", self._entity_key) diff --git a/custom_components/midea_auto_cloud/select.py b/custom_components/midea_auto_cloud/select.py index 191c3cf..7c0c65e 100644 --- a/custom_components/midea_auto_cloud/select.py +++ b/custom_components/midea_auto_cloud/select.py @@ -79,7 +79,15 @@ class MideaSelectEntity(MideaEntity, SelectEntity): async def async_select_option(self, option: str): new_status = self._key_options.get(option) if new_status: - await self.async_set_attributes(new_status) + # 优先使用command字段 + command = self._config.get("command") + if command and isinstance(command, dict): + # 合并选项值到命令 + merged_command = {**command, **new_status} + await self.async_set_attributes(merged_command) + else: + # 原有逻辑(向后兼容) + await self.async_set_attributes(new_status) def update_state(self, status): try: