From 7c039436142f6f193fff172e182b776af0c3b627 Mon Sep 17 00:00:00 2001 From: kaiwang10 Date: Sat, 19 Oct 2024 14:28:05 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9D=84=EF=B8=8F=20add=20second=20power?= =?UTF-8?q?=5Fcost=20entities=20for=20lumi.acpartner.mcn04?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/xiaomi_miot/__init__.py | 42 ++++++++++++++----- .../xiaomi_miot/core/device_customizes.py | 13 +++++- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/custom_components/xiaomi_miot/__init__.py b/custom_components/xiaomi_miot/__init__.py index 3e16aac28..f9a5bd50f 100644 --- a/custom_components/xiaomi_miot/__init__.py +++ b/custom_components/xiaomi_miot/__init__.py @@ -1159,6 +1159,22 @@ def update_attrs(self, *args, **kwargs): raise NotImplementedError() +def update_attrs_add_suffix_on_duplicate(attrs, new_dict): + updated_attrs = {} + + for key, value in new_dict.items(): + if key in attrs: + suffix = 2 + while f"{key}_{suffix}" in attrs: + suffix += 1 + updated_key = f"{key}_{suffix}" + else: + updated_key = key + + updated_attrs[updated_key] = value + attrs.update(updated_attrs) + + class MiotEntity(MiioEntity): def __init__(self, miot_service=None, device=None, **kwargs): self._config = dict(kwargs.get('config') or {}) @@ -1613,15 +1629,21 @@ async def async_update_for_main_entity(self): # update micloud statistics in cloud cls = self.custom_config_list('micloud_statistics') or [] if key := self.custom_config('stat_power_cost_key'): - dic = { - 'type': self.custom_config('stat_power_cost_type', 'stat_day_v3'), - 'key': key, - 'day': 32, - 'limit': 31, - 'attribute': None, - 'template': 'micloud_statistics_power_cost', - } - cls = [*cls, dic] + if isinstance(key, list): + keys = key + else: + keys = [key] + + for k in keys: + dic = { + 'type': self.custom_config('stat_power_cost_type', 'stat_day_v3'), + 'key': k, + 'day': 32, + 'limit': 31, + 'attribute': None, + 'template': 'micloud_statistics_power_cost', + } + cls = [*cls, dic] if cls: await self.async_update_micloud_statistics(cls) @@ -1788,7 +1810,7 @@ async def async_update_micloud_statistics(self, lst): if anm := c.get('attribute'): attrs[anm] = rls elif isinstance(rls, dict): - attrs.update(rls) + update_attrs_add_suffix_on_duplicate(attrs, rls) if attrs: await self.async_update_attrs(attrs) diff --git a/custom_components/xiaomi_miot/core/device_customizes.py b/custom_components/xiaomi_miot/core/device_customizes.py index 19b82c8ff..f5ca5b4ac 100644 --- a/custom_components/xiaomi_miot/core/device_customizes.py +++ b/custom_components/xiaomi_miot/core/device_customizes.py @@ -824,14 +824,25 @@ 'select_properties': 'ac_mode', 'miio_cloud_props': [], 'stat_power_cost_type': 'stat_day_v3', - 'stat_power_cost_key': '7.1', + 'stat_power_cost_key': ['7.1', '7.3'], + 'sensor_attributes': 'power_cost_today,power_cost_month,power_cost_today_2,power_cost_month_2' }, 'lumi.acpartner.mcn04:power_consumption': ENERGY_KWH, 'lumi.acpartner.mcn04:power_cost_today': { 'value_ratio': 1, + **ENERGY_KWH }, 'lumi.acpartner.mcn04:power_cost_month': { 'value_ratio': 1, + **ENERGY_KWH + }, + 'lumi.acpartner.mcn04:power_cost_today_2': { + 'value_ratio': 1, + **ENERGY_KWH + }, + 'lumi.acpartner.mcn04:power_cost_month_2': { + 'value_ratio': 1, + **ENERGY_KWH }, 'lumi.acpartner.*': { 'sensor_attributes': 'electric_power,power_cost_today,power_cost_month', From 2b6c76640c77dc49a75436f77d360b21e152c677 Mon Sep 17 00:00:00 2001 From: Alone Date: Thu, 24 Oct 2024 12:48:28 +0800 Subject: [PATCH 2/2] Update __init__.py --- custom_components/xiaomi_miot/__init__.py | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/custom_components/xiaomi_miot/__init__.py b/custom_components/xiaomi_miot/__init__.py index f9a5bd50f..6e211de01 100644 --- a/custom_components/xiaomi_miot/__init__.py +++ b/custom_components/xiaomi_miot/__init__.py @@ -1628,12 +1628,7 @@ async def async_update_for_main_entity(self): # update micloud statistics in cloud cls = self.custom_config_list('micloud_statistics') or [] - if key := self.custom_config('stat_power_cost_key'): - if isinstance(key, list): - keys = key - else: - keys = [key] - + if keys := self.custom_config_list('stat_power_cost_key'): for k in keys: dic = { 'type': self.custom_config('stat_power_cost_type', 'stat_day_v3'), @@ -1643,7 +1638,7 @@ async def async_update_for_main_entity(self): 'attribute': None, 'template': 'micloud_statistics_power_cost', } - cls = [*cls, dic] + cls.append(dic) if cls: await self.async_update_micloud_statistics(cls)