From 9a38dac1db1a176d37843ad42931b4726eac7ac4 Mon Sep 17 00:00:00 2001 From: Alone Date: Sat, 14 Dec 2024 21:42:24 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A1=20fix=20for=20light=20entity=20(#2?= =?UTF-8?q?039)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/xiaomi_miot/core/device.py | 8 ++- custom_components/xiaomi_miot/light.py | 61 +++++++++----------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/custom_components/xiaomi_miot/core/device.py b/custom_components/xiaomi_miot/core/device.py index 5e3381e7e..f2bcc3bba 100644 --- a/custom_components/xiaomi_miot/core/device.py +++ b/custom_components/xiaomi_miot/core/device.py @@ -350,7 +350,13 @@ def init_converters(self): for p in cfg.get('converters') or []: if not (props := p.get('props')): continue - for prop in service.get_properties(*props, excludes=self._exclude_miot_properties): + for p in props: + if '.' in p: + prop = self.spec.get_property(p) + else: + prop = service.get_property(p) + if not prop: + continue attr = p.get('attr', prop.full_name) c = p.get('class', MiotPropConv) d = p.get('domain', None) diff --git a/custom_components/xiaomi_miot/light.py b/custom_components/xiaomi_miot/light.py index f4d927f1d..f6a4b2ef0 100644 --- a/custom_components/xiaomi_miot/light.py +++ b/custom_components/xiaomi_miot/light.py @@ -86,40 +86,35 @@ def on_init(self): self._brightness_for_on = self.custom_config_number('brightness_for_on') self._brightness_for_off = self.custom_config_number('brightness_for_off') self._attr_color_mode = ColorMode.ONOFF - modes = set() - for conv in self.device.converters: - if not self._miot_service: - break - prop = getattr(conv, 'prop', None) - if isinstance(prop, MiotProperty): - if conv.attr == ATTR_BRIGHTNESS or prop.in_list(['brightness']): - self.listen_attrs.add(conv.attr) - self._attr_names[ATTR_BRIGHTNESS] = conv.attr - self._attr_color_mode = ColorMode.BRIGHTNESS - elif conv.attr == ATTR_COLOR_TEMP or prop.in_list(['color_temperature', 'color_temp']): - self.listen_attrs.add(conv.attr) - self._attr_color_mode = ColorMode.COLOR_TEMP - modes.add(ColorMode.COLOR_TEMP) - if prop.unit in ['kelvin']: - self._attr_min_color_temp_kelvin = prop.range_min() - self._attr_max_color_temp_kelvin = prop.range_max() - self._attr_names[ATTR_COLOR_TEMP_KELVIN] = conv.attr - else: - self._attr_min_mireds = prop.range_min() - self._attr_max_mireds = prop.range_max() - self._attr_names[ATTR_COLOR_TEMP] = conv.attr - elif conv.attr == ATTR_RGB_COLOR or prop.in_list(['color']): - self.listen_attrs.add(conv.attr) - self._attr_names[ATTR_RGB_COLOR] = conv.attr - modes.add(ColorMode.RGB) - elif conv.attr == ATTR_EFFECT or prop.in_list(['mode']): - self.listen_attrs.add(conv.attr) - self._attr_names[ATTR_EFFECT] = conv.attr - self._attr_effect_list = prop.list_descriptions() - self._attr_supported_features |= LightEntityFeature.EFFECT - - self._attr_supported_color_modes = modes if modes else {self._attr_color_mode} + modes = set() + for attr in self.conv.attrs: + prop = self._miot_service.spec.get_property(attr) if self._miot_service else None + if not prop: + continue + if prop.in_list(['brightness']): + self._attr_names[ATTR_BRIGHTNESS] = attr + self._attr_color_mode = ColorMode.BRIGHTNESS + elif prop.in_list(['color_temperature', 'color_temp']): + self._attr_color_mode = ColorMode.COLOR_TEMP + modes.add(ColorMode.COLOR_TEMP) + if prop.unit in ['kelvin']: + self._attr_min_color_temp_kelvin = prop.range_min() + self._attr_max_color_temp_kelvin = prop.range_max() + self._attr_names[ATTR_COLOR_TEMP_KELVIN] = attr + else: + self._attr_min_mireds = prop.range_min() + self._attr_max_mireds = prop.range_max() + self._attr_names[ATTR_COLOR_TEMP] = attr + elif prop.in_list(['color']): + self._attr_names[ATTR_RGB_COLOR] = attr + modes.add(ColorMode.RGB) + elif prop.in_list(['mode']): + self._attr_names[ATTR_EFFECT] = attr + self._attr_effect_list = prop.list_descriptions() + self._attr_supported_features |= LightEntityFeature.EFFECT + + self._attr_supported_color_modes = modes if modes else {self._attr_color_mode} def get_state(self) -> dict: return {