Skip to content

Commit

Permalink
🔢 improve for number sensor (#1970)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Nov 25, 2024
1 parent 1e49372 commit cf35f39
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion custom_components/xiaomi_miot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2519,7 +2519,7 @@ def update_with_properties(self):
if not prop:
continue
val = prop.from_dict(self.parent_attributes)
if prop.value_range and not (prop.range_min() <= val <= prop.range_max()):
if not prop.range_valid(val):
val = None
self._extra_attrs[prop.name] = val

Expand Down
11 changes: 11 additions & 0 deletions custom_components/xiaomi_miot/core/miot_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,17 @@ def range_step(self):
return self.value_range[2]
return None

def range_valid(self, val):
if not self.value_range:
return True
range_min = self.range_min()
range_max = self.range_max()
if None in [range_min, range_max]:
return True
if val == None:
return False
return range_min <= val <= range_max

@property
def is_bool(self):
return self.format == 'bool'
Expand Down
10 changes: 4 additions & 6 deletions custom_components/xiaomi_miot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,8 @@ def native_value(self):
if key in self._state_attrs:
return f'{self._state_attrs[key]}'.lower()
val = prop.from_dict(self._state_attrs)
if prop.value_range:
if not prop.range_min() <= val <= prop.range_max():
val = None
if not prop.range_valid(val):
val = None
return val

def before_select_modes(self, prop, option, **kwargs):
Expand Down Expand Up @@ -517,9 +516,8 @@ def native_value(self):
if key in self._state_attrs:
return f'{self._state_attrs[key]}'.lower()
val = prop.from_dict(self._state_attrs)
if prop.value_range:
if not prop.range_min() <= val <= prop.range_max():
val = None
if not prop.range_valid(val):
val = None
if val is not None:
svd = self.custom_config_number('value_ratio') or 0
if svd:
Expand Down

1 comment on commit cf35f39

@al-one
Copy link
Owner Author

@al-one al-one commented on cf35f39 Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.