From 8f5c81c7ff7c54c19c5b66c2c5575f510d4503d4 Mon Sep 17 00:00:00 2001 From: Alone Date: Tue, 3 Dec 2024 12:59:15 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A2=20improve=20for=20number=20sensor?= =?UTF-8?q?=20(#1980)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- custom_components/xiaomi_miot/core/miot_spec.py | 4 ++++ custom_components/xiaomi_miot/sensor.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/custom_components/xiaomi_miot/core/miot_spec.py b/custom_components/xiaomi_miot/core/miot_spec.py index 422b53e65..b9e146710 100644 --- a/custom_components/xiaomi_miot/core/miot_spec.py +++ b/custom_components/xiaomi_miot/core/miot_spec.py @@ -862,6 +862,10 @@ def range_valid(self, val): return True if val == None: return False + try: + val = float(val) + except (TypeError, ValueError): + return False return range_min <= val <= range_max @property diff --git a/custom_components/xiaomi_miot/sensor.py b/custom_components/xiaomi_miot/sensor.py index f3c2b5998..e2d45adb8 100644 --- a/custom_components/xiaomi_miot/sensor.py +++ b/custom_components/xiaomi_miot/sensor.py @@ -134,7 +134,7 @@ def set_state(self, data: dict): value = data.get(self.attr) prop = self._miot_property if prop and prop.value_range: - if not prop.range_min() <= value <= prop.range_max(): + if not prop.range_valid(value): value = None if value is not None: try: