From f0a0b9f3004970b23b7688b56479005cf201e39b Mon Sep 17 00:00:00 2001 From: Alone Date: Thu, 21 Nov 2024 18:13:17 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A2=20improve=20for=20number=20sensor?= =?UTF-8?q?=20(#1968)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/xiaomi_miot/sensor.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/custom_components/xiaomi_miot/sensor.py b/custom_components/xiaomi_miot/sensor.py index 3f1178f14..3e970d165 100644 --- a/custom_components/xiaomi_miot/sensor.py +++ b/custom_components/xiaomi_miot/sensor.py @@ -311,12 +311,17 @@ def device_class(self): @property def native_value(self): - if not self._prop_state: + prop = self._prop_state + if not prop: return None - key = f'{self._prop_state.full_name}_desc' + key = f'{prop.full_name}_desc' if key in self._state_attrs: return f'{self._state_attrs[key]}'.lower() - return self._prop_state.from_dict(self._state_attrs) + val = prop.from_dict(self._state_attrs) + if prop.value_range: + if not prop.range_min() <= val <= prop.range_max(): + val = None + return val def before_select_modes(self, prop, option, **kwargs): if prop := self._miot_service.get_property('on'): @@ -506,11 +511,15 @@ def update(self, data=None): @property def native_value(self): + prop = self._miot_property if not self._attr_native_unit_of_measurement: - key = f'{self._miot_property.full_name}_desc' + key = f'{prop.full_name}_desc' if key in self._state_attrs: return f'{self._state_attrs[key]}'.lower() - val = self._miot_property.from_dict(self._state_attrs) + val = prop.from_dict(self._state_attrs) + if prop.value_range: + if not prop.range_min() <= val <= prop.range_max(): + val = None if val is not None: svd = self.custom_config_number('value_ratio') or 0 if svd: