diff --git a/custom_components/xiaomi_home/miot/miot_client.py b/custom_components/xiaomi_home/miot/miot_client.py index 5a6df15..e1918d1 100644 --- a/custom_components/xiaomi_home/miot/miot_client.py +++ b/custom_components/xiaomi_home/miot/miot_client.py @@ -636,79 +636,79 @@ class MIoTClient: traceback.format_exc()) return False - async def set_prop_async(self, did: str, siid: int, piid: int, - value: Any) -> bool: - if did not in self._device_list_cache: - raise MIoTClientError(f"did not exist, {did}") - # Priority local control - if self._ctrl_mode == CtrlMode.AUTO: - # Gateway control - device_gw = self._device_list_gateway.get(did, None) - if (device_gw and device_gw.get("online", False) and - device_gw.get("specv2_access", False) and - "group_id" in device_gw): - mips = self._mips_local.get(device_gw["group_id"], None) - if mips is None: - _LOGGER.error( - 'no gateway route, %s, try control through cloud', - device_gw) - else: - result = await mips.set_prop_async(did=did, - siid=siid, - piid=piid, - value=value) - _LOGGER.debug('gateway set prop, %s.%d.%d, %s -> %s', did, - siid, piid, value, result) - rc = (result or - {}).get('code', - MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value) - if rc in [0, 1]: - return True - raise MIoTClientError(self.__get_exec_error_with_rc(rc=rc)) - # Lan control - device_lan = self._device_list_lan.get(did, None) - if device_lan and device_lan.get("online", False): - result = await self._miot_lan.set_prop_async(did=did, - siid=siid, - piid=piid, - value=value) - _LOGGER.debug("lan set prop, %s.%d.%d, %s -> %s", did, siid, - piid, value, result) - rc = (result or - {}).get("code", - MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value) - if rc in [0, 1]: - return True - raise MIoTClientError(self.__get_exec_error_with_rc(rc=rc)) + # async def set_prop_async(self, did: str, siid: int, piid: int, + # value: Any) -> bool: + # if did not in self._device_list_cache: + # raise MIoTClientError(f"did not exist, {did}") + # # Priority local control + # if self._ctrl_mode == CtrlMode.AUTO: + # # Gateway control + # device_gw = self._device_list_gateway.get(did, None) + # if (device_gw and device_gw.get("online", False) and + # device_gw.get("specv2_access", False) and + # "group_id" in device_gw): + # mips = self._mips_local.get(device_gw["group_id"], None) + # if mips is None: + # _LOGGER.error( + # 'no gateway route, %s, try control through cloud', + # device_gw) + # else: + # result = await mips.set_prop_async(did=did, + # siid=siid, + # piid=piid, + # value=value) + # _LOGGER.debug('gateway set prop, %s.%d.%d, %s -> %s', did, + # siid, piid, value, result) + # rc = (result or + # {}).get('code', + # MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value) + # if rc in [0, 1]: + # return True + # raise MIoTClientError(self.__get_exec_error_with_rc(rc=rc)) + # # Lan control + # device_lan = self._device_list_lan.get(did, None) + # if device_lan and device_lan.get("online", False): + # result = await self._miot_lan.set_prop_async(did=did, + # siid=siid, + # piid=piid, + # value=value) + # _LOGGER.debug("lan set prop, %s.%d.%d, %s -> %s", did, siid, + # piid, value, result) + # rc = (result or + # {}).get("code", + # MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value) + # if rc in [0, 1]: + # return True + # raise MIoTClientError(self.__get_exec_error_with_rc(rc=rc)) - # Cloud control - device_cloud = self._device_list_cloud.get(did, None) - if device_cloud and device_cloud.get("online", False): - result = await self._http.set_prop_async(params=[{ - "did": did, - "siid": siid, - "piid": piid, - "value": value - }]) - _LOGGER.debug('cloud set prop, %s.%d.%d, %s -> %s', did, siid, piid, - value, result) - if result and len(result) == 1: - rc = result[0].get("code", - MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value) - if rc in [0, 1]: - return True - if rc in [-704010000, -704042011]: - # Device remove or offline - _LOGGER.error("device may be removed or offline, %s", did) - self._main_loop.create_task( - await - self.__refresh_cloud_device_with_dids_async(dids=[did])) - raise MIoTClientError(self.__get_exec_error_with_rc(rc=rc)) + # # Cloud control + # device_cloud = self._device_list_cloud.get(did, None) + # if device_cloud and device_cloud.get("online", False): + # result = await self._http.set_prop_async(params=[{ + # "did": did, + # "siid": siid, + # "piid": piid, + # "value": value + # }]) + # _LOGGER.debug('cloud set prop, %s.%d.%d, %s -> %s', did, siid, piid, + # value, result) + # if result and len(result) == 1: + # rc = result[0].get("code", + # MIoTErrorCode.CODE_MIPS_INVALID_RESULT.value) + # if rc in [0, 1]: + # return True + # if rc in [-704010000, -704042011]: + # # Device remove or offline + # _LOGGER.error("device may be removed or offline, %s", did) + # self._main_loop.create_task( + # await + # self.__refresh_cloud_device_with_dids_async(dids=[did])) + # raise MIoTClientError(self.__get_exec_error_with_rc(rc=rc)) - # Show error message - raise MIoTClientError( - f"{self._i18n.translate('miot.client.device_exec_error')}, " - f"{self._i18n.translate('error.common.-10007')}") + # # Show error message + # raise MIoTClientError( + # f"{self._i18n.translate('miot.client.device_exec_error')}, " + # f"{self._i18n.translate('error.common.-10007')}") async def set_props_async( self, diff --git a/custom_components/xiaomi_home/miot/miot_device.py b/custom_components/xiaomi_home/miot/miot_device.py index 125b611..9605dc5 100644 --- a/custom_components/xiaomi_home/miot/miot_device.py +++ b/custom_components/xiaomi_home/miot/miot_device.py @@ -1027,32 +1027,35 @@ class MIoTServiceEntity(Entity): update_value: bool = True, write_ha_state: bool = True, ) -> bool: - if not prop: - raise RuntimeError( - f"set property failed, property is None, {self.entity_id}, {self.name}" - ) - value = prop.value_format(value) - if prop not in self.entity_data.props: - raise RuntimeError(f"set property failed, unknown property, " - f"{self.entity_id}, {self.name}, {prop.name}") - if not prop.writable: - raise RuntimeError(f"set property failed, not writable, " - f"{self.entity_id}, {self.name}, {prop.name}") - try: - await self.miot_device.miot_client.set_prop_async( - did=self.miot_device.did, - siid=prop.service.iid, - piid=prop.iid, - value=value, - ) - except MIoTClientError as e: - raise RuntimeError( - f"{e}, {self.entity_id}, {self.name}, {prop.name}") from e - if update_value: - self._prop_value_map[prop] = value - if write_ha_state: - self.async_write_ha_state() - return True + set_properties_list = [{"prop": prop, "value": value}] + return self.set_properties_async(set_properties_list, update_value, + write_ha_state) + # if not prop: + # raise RuntimeError( + # f"set property failed, property is None, {self.entity_id}, {self.name}" + # ) + # value = prop.value_format(value) + # if prop not in self.entity_data.props: + # raise RuntimeError(f"set property failed, unknown property, " + # f"{self.entity_id}, {self.name}, {prop.name}") + # if not prop.writable: + # raise RuntimeError(f"set property failed, not writable, " + # f"{self.entity_id}, {self.name}, {prop.name}") + # try: + # await self.miot_device.miot_client.set_prop_async( + # did=self.miot_device.did, + # siid=prop.service.iid, + # piid=prop.iid, + # value=value, + # ) + # except MIoTClientError as e: + # raise RuntimeError( + # f"{e}, {self.entity_id}, {self.name}, {prop.name}") from e + # if update_value: + # self._prop_value_map[prop] = value + # if write_ha_state: + # self.async_write_ha_state() + # return True async def set_properties_async( self, @@ -1301,12 +1304,19 @@ class MIoTPropertyEntity(Entity): ) value = self.spec.value_format(value) try: - await self.miot_device.miot_client.set_prop_async( - did=self.miot_device.did, - siid=self.spec.service.iid, - piid=self.spec.iid, - value=value, - ) + await self.miot_device.miot_client.set_props_async([{ + "did": self.miot_device.did, + "siid": self.spec.service.iid, + "piid": self.spec.iid, + "value": value, + }]) + # await self.miot_device.miot_client.set_prop_async( + # did=self.miot_device.did, + # siid=self.spec.service.iid, + # piid=self.spec.iid, + # value=value, + # ) + except MIoTClientError as e: raise RuntimeError(f"{e}, {self.entity_id}, {self.name}") from e self._value = value