From 24a752742d074ba1f3dc5f7e004ba0998ac40037 Mon Sep 17 00:00:00 2001 From: Alone Date: Sun, 7 Jul 2024 19:13:22 +0800 Subject: [PATCH] =?UTF-8?q?=E2=8C=9B=20add=20option=20`parallel=5Fupdates`?= =?UTF-8?q?=20(#1749)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/xiaomi_miot/__init__.py | 20 +++++++++++++++++++ .../xiaomi_miot/core/device_customizes.py | 1 + custom_components/xiaomi_miot/switch.py | 1 - 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/custom_components/xiaomi_miot/__init__.py b/custom_components/xiaomi_miot/__init__.py index 505cf0f22..9c7ef98eb 100644 --- a/custom_components/xiaomi_miot/__init__.py +++ b/custom_components/xiaomi_miot/__init__.py @@ -843,6 +843,25 @@ def update_custom_scan_interval(self, only_custom=False): self.platform.scan_interval = tim _LOGGER.debug('%s: Update custom scan interval: %s', self.name_model, tim) + def update_custom_parallel_updates(self): + if not self.hass: + return False + if not hasattr(self, '_unique_did'): + return False + num = self.custom_config_integer('parallel_updates', 0) + if not num: + return False + did = self._unique_did + self.hass.data[DOMAIN].setdefault(did, {}) + dcs = self.hass.data[DOMAIN].get(did, {}) + pus = dcs.get('parallel_updates') + if not pus: + pus = asyncio.Semaphore(num) + self.hass.data[DOMAIN][did]['parallel_updates'] = pus + _LOGGER.warning('%s: Update custom parallel updates: %s', self.name_model, num) + self.parallel_updates = pus + return pus + def filter_state_attributes(self, dat: dict): if exl := self.global_config('exclude_state_attributes'): exl = cv.ensure_list(exl) @@ -973,6 +992,7 @@ async def async_added_to_hass(self): await super().async_added_to_hass() if self.platform: self.update_custom_scan_interval() + self.update_custom_parallel_updates() if self.platform.config_entry: eid = self.platform.config_entry.entry_id self._add_entities = self.hass.data[DOMAIN][eid].get('add_entities') or {} diff --git a/custom_components/xiaomi_miot/core/device_customizes.py b/custom_components/xiaomi_miot/core/device_customizes.py index b12eb42a9..fcdad3f4e 100644 --- a/custom_components/xiaomi_miot/core/device_customizes.py +++ b/custom_components/xiaomi_miot/core/device_customizes.py @@ -427,6 +427,7 @@ }, 'cuco.plug.*': { 'main_miot_services': 'switch-2', + 'parallel_updates': 3, }, 'cuco.plug.*:electric_current': { 'state_class': 'measurement', diff --git a/custom_components/xiaomi_miot/switch.py b/custom_components/xiaomi_miot/switch.py index 9c8d905b3..2ed10b2b4 100644 --- a/custom_components/xiaomi_miot/switch.py +++ b/custom_components/xiaomi_miot/switch.py @@ -36,7 +36,6 @@ _LOGGER = logging.getLogger(__name__) DATA_KEY = f'{ENTITY_DOMAIN}.{DOMAIN}' -PARALLEL_UPDATES = 3 # issue#1749 SERVICE_TO_METHOD = {}