mirror of
https://github.com/sususweet/midea-meiju-codec.git
synced 2025-11-13 00:11:54 +00:00
feat: add device support for T0x21 switch.
This commit is contained in:
@@ -5,6 +5,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .core.logger import MideaLogger
|
||||
from .midea_entity import MideaEntity
|
||||
from . import load_device_config
|
||||
|
||||
@@ -43,6 +44,9 @@ class MideaSwitchEntity(MideaEntity, SwitchEntity):
|
||||
"""Midea switch entity."""
|
||||
|
||||
def __init__(self, coordinator, device, manufacturer, rationale, entity_key, config):
|
||||
# 自动判断是否为中央空调设备(T0x21)
|
||||
self._is_central_ac = device.device_type == 0x21
|
||||
|
||||
super().__init__(
|
||||
coordinator,
|
||||
device.device_id,
|
||||
@@ -67,12 +71,37 @@ class MideaSwitchEntity(MideaEntity, SwitchEntity):
|
||||
|
||||
async def async_turn_on(self):
|
||||
"""Turn the switch on."""
|
||||
# Use attribute from config if available, otherwise fall back to entity_key
|
||||
attribute = self._config.get("attribute", self._entity_key)
|
||||
await self._async_set_status_on_off(attribute, True)
|
||||
if self._is_central_ac:
|
||||
await self._async_set_central_ac_switch_status(True)
|
||||
else:
|
||||
await self._async_set_status_on_off(attribute, True)
|
||||
|
||||
async def async_turn_off(self):
|
||||
"""Turn the switch off."""
|
||||
# Use attribute from config if available, otherwise fall back to entity_key
|
||||
attribute = self._config.get("attribute", self._entity_key)
|
||||
await self._async_set_status_on_off(attribute, False)
|
||||
if self._is_central_ac:
|
||||
await self._async_set_central_ac_switch_status(False)
|
||||
else:
|
||||
await self._async_set_status_on_off(attribute, False)
|
||||
|
||||
async def _async_set_central_ac_switch_status(self, is_on: bool):
|
||||
"""设置中央空调开关设备的状态"""
|
||||
# 从entity_key中提取endpoint ID
|
||||
# entity_key格式: endpoint_1_OnOff -> 提取出 1
|
||||
endpoint_id = 1 # 默认值
|
||||
if self._entity_key.startswith("endpoint_"):
|
||||
try:
|
||||
# 提取endpoint_后面的数字
|
||||
parts = self._entity_key.split("_")
|
||||
if len(parts) >= 2:
|
||||
endpoint_id = int(parts[1])
|
||||
except (ValueError, IndexError):
|
||||
MideaLogger.warning(f"Failed to extract endpoint ID from {self._entity_key}, using default 1")
|
||||
|
||||
# 构建控制命令
|
||||
control = {
|
||||
"run_mode": "1" if is_on else "0",
|
||||
"endpoint": endpoint_id
|
||||
}
|
||||
await self.coordinator.async_send_switch_control(control)
|
||||
|
||||
Reference in New Issue
Block a user