本帖最后由 x01673 于 2019-8-2 15:00 编辑
----------------------------------------------------------------------------------------------------------
8/2号更新,提交的PR,S大已合并,可以到https://github.com/syssi/xiaomi_airconditioningcompanion 直接下载了
----------------------------------------------------------------------------------------------------------
HA 0.96更新,Climate组件改动很大,导致syssi大神的空调伴侣已不能再用。
S大的repo已经有人提交了issue https://github.com/syssi/xiaomi_airconditioningcompanion/issues/67
但是啥时候更新,目前还是未知数(S大感觉很忙)
今天 @neroxps 建议我提交PR给S大。
但是由于对Python不是很熟,代码写的可能不符合规范。
这里就分享出来,论坛的Python大神可以给个参考哇?。
也请各位有空调伴侣的帮忙测试一下是否有问题。
如果没有问题的话,我就直接提交PR了。
很少发帖,感觉很乱,见谅。
以下是关键代码修改部分:
@asyncio.coroutine
def async_update(self):
"""Update the state of this climate device."""
from miio import DeviceException
try:
state = yield from self.hass.async_add_job(self._device.status)
_LOGGER.debug("Got new state: %s", state)
self._available = True
self._state_attrs.update({
ATTR_AIR_CONDITION_MODEL: state.air_condition_model.hex(),
ATTR_LOAD_POWER: state.load_power,
ATTR_TEMPERATURE: state.target_temperature,
ATTR_SWING_MODE: state.swing_mode.name.lower(),
ATTR_FAN_MODE: state.fan_speed.name.lower(),
ATTR_HVAC_MODE: state.mode.name.lower() if self._state else "off",
ATTR_LED: state.led,
})
"""新增变量(属性)_last_on_operation从miio获取的空调的操作(heat, cool, fan_only, dry)"""
self._last_on_operation = OperationMode[state.mode.name].value
"""miio获取到空调处于关机状态时, hvac_mode值为off"""
if state.power == 'off':
self._hvac_mode = HVAC_MODE_OFF
self._state = False
"""开机状态时, hvac_mode值等于_last_on_operation"""
else:
self._hvac_mode = self._last_on_operation
self._state = True
self._target_temperature = state.target_temperature
self._fan_mode = state.fan_speed
self._swing_mode = state.swing_mode
if self._air_condition_model is None:
self._air_condition_model = state.air_condition_model.hex()
except DeviceException as ex:
self._available = False
_LOGGER.error("Got exception while fetching the state: %s", ex)
@asyncio.coroutine
def async_set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode."""
if hvac_mode == OperationMode.Off.value:
result = yield from self._try_command(
"Turning the miio device off failed.", self._device.off)
if result:
self._state = False
self._hvac_mode = HVAC_MODE_OFF
else:
self._hvac_mode = OperationMode(hvac_mode).value
self._state = True
yield from self._send_configuration()
@asyncio.coroutine
def _send_configuration(self):
from miio.airconditioningcompanion import \
Power, Led, OperationMode as MiioOperationMode
"""确保不将hvac_mode为off状态时,直接传给miio,否者会报错"""
if self._air_condition_model is not None:
yield from self._try_command(
"Sending new air conditioner configuration failed.",
self._device.send_configuration,
self._air_condition_model,
Power(int(self._state)),
MiioOperationMode[OperationMode(self._hvac_mode).name] if self._state else MiioOperationMode[OperationMode(self._last_on_operation).name],
int(self._target_temperature),
self._fan_mode,
self._swing_mode,
Led.Off,
)
else:
_LOGGER.error('Model number of the air condition unknown. '
'Configuration cannot be sent.')
附件是完整的修改版
|