mirror of
https://github.com/sususweet/midea-meiju-codec.git
synced 2026-02-11 06:13:49 +00:00
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
}
}
}
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user