Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

❄️ add second power_cost entities for lumi.acpartner.mcn04 #1895

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {})
Expand Down Expand Up @@ -1612,16 +1628,17 @@ 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 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'),
'key': k,
'day': 32,
'limit': 31,
'attribute': None,
'template': 'micloud_statistics_power_cost',
}
cls.append(dic)
if cls:
await self.async_update_micloud_statistics(cls)

Expand Down Expand Up @@ -1788,7 +1805,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)

Expand Down
13 changes: 12 additions & 1 deletion custom_components/xiaomi_miot/core/device_customizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down