mirror of
https://github.com/sususweet/midea-meiju-codec.git
synced 2026-02-11 06:13:49 +00:00
- 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
}
}
}
98 lines
3.6 KiB
Python
98 lines
3.6 KiB
Python
from homeassistant.components.select import SelectEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import Platform
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from .const import DOMAIN
|
|
from .midea_entity import MideaEntity
|
|
from . import load_device_config
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
account_bucket = hass.data.get(DOMAIN, {}).get("accounts", {}).get(config_entry.entry_id)
|
|
if not account_bucket:
|
|
async_add_entities([])
|
|
return
|
|
device_list = account_bucket.get("device_list", {})
|
|
coordinator_map = account_bucket.get("coordinator_map", {})
|
|
|
|
devs = []
|
|
for device_id, info in device_list.items():
|
|
device_type = info.get("type")
|
|
sn8 = info.get("sn8")
|
|
config = await load_device_config(hass, device_type, sn8) or {}
|
|
entities_cfg = (config.get("entities") or {}).get(Platform.SELECT, {})
|
|
manufacturer = config.get("manufacturer")
|
|
rationale = config.get("rationale")
|
|
coordinator = coordinator_map.get(device_id)
|
|
device = coordinator.device if coordinator else None
|
|
for entity_key, ecfg in entities_cfg.items():
|
|
devs.append(MideaSelectEntity(coordinator, device, manufacturer, rationale, entity_key, ecfg))
|
|
async_add_entities(devs)
|
|
|
|
|
|
class MideaSelectEntity(MideaEntity, SelectEntity):
|
|
def __init__(self, coordinator, device, manufacturer, rationale, entity_key, config):
|
|
super().__init__(
|
|
coordinator,
|
|
device.device_id,
|
|
device.device_name,
|
|
f"T0x{device.device_type:02X}",
|
|
device.sn,
|
|
device.sn8,
|
|
device.model,
|
|
entity_key,
|
|
device=device,
|
|
manufacturer=manufacturer,
|
|
rationale=rationale,
|
|
config=config,
|
|
)
|
|
self._key_options = self._config.get("options")
|
|
|
|
@property
|
|
def options(self):
|
|
"""Return a list of available options.
|
|
|
|
Note: To translate options, add translations in the translation file under:
|
|
entity.select.{translation_key}.state.{option_key}
|
|
|
|
Home Assistant will automatically use these translations when displaying options.
|
|
"""
|
|
return list(self._key_options.keys())
|
|
|
|
@property
|
|
def current_option(self):
|
|
# Use attribute from config if available, otherwise fall back to entity_key
|
|
attribute = self._config.get("attribute", self._entity_key)
|
|
if attribute and attribute != self._entity_key:
|
|
# For simple attribute access, get the value directly
|
|
return self._get_nested_value(attribute)
|
|
else:
|
|
# For complex mapping, use the existing logic
|
|
return self._dict_get_selected(self._key_options)
|
|
|
|
async def async_select_option(self, option: str):
|
|
new_status = self._key_options.get(option)
|
|
if 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:
|
|
self.schedule_update_ha_state()
|
|
except Exception:
|
|
pass
|
|
|