本帖最后由 1120319827 于 2024-9-21 00:29 编辑
import logging
from homeassistant.components.button import ButtonEntity
from miio.miot_device import MiotDevice
_LOGGER = logging.getLogger(__name__)
_MAPPING = {
'key-num': {"siid": 3, "piid": 12}
}
class MyIntegrationButton(ButtonEntity):
def __init__(self, name, ip, token, siid, piid, value):
self._unique_id = f"{ip}_{value}"
self._name = name
self._ip = ip
self._token = token
self._siid = siid
self._piid = piid
self._value = value
self._device = MiotDevice(ip=ip, token=token, mapping=_MAPPING, timeout=3)
def _send_command(self):
"""Send the command to the Miot device."""
try:
property_request = [
{"siid": self._siid, "piid": self._piid, "value": self._value}
]
self._device.send("set_properties", property_request)
_LOGGER.info(f"Sent value {self._value} to siid={self._siid}, piid={self._piid}")
except Exception as e:
_LOGGER.error(f"Failed to send command to the device: {e}")</span>
复制代码
上面是核心代码部分, 每次点击具体按钮后设备会执行相应的命令, 但是self._device.send("set_properties", property_request)内部代码似乎没有收到设备的反馈自动重试4次, 调整了timeout也没有用
错误日志如下:
2024-09-21 00:02:40.152 ERROR (SyncWorker_60) [miio.miioprotocol] Got error when receiving: timed out < 报错位置: self._device.send("set_properties", property_request)
2024-09-21 00:02:40.155 ERROR (SyncWorker_60) [custom_components.mx_control.button] Failed to send command to the device: No response from the device