Skip to content

Commit

Permalink
Fix entity Number behavor for min and max limit
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed May 9, 2024
1 parent de5cafe commit 7337cd4
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion custom_components/dabpumps/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Base component constants
DOMAIN = "dabpumps"
NAME = "DAB Pumps"
VERSION="2024.05.1"
VERSION="2024.05.2"
ISSUE_URL = "https://github.com/ankoh/dabpumps/issues"

PLATFORMS: list[Platform] = [
Expand Down
4 changes: 2 additions & 2 deletions custom_components/dabpumps/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,8 +742,8 @@ async def _async_process_device_config_data(self, device, data):
param_type = meta_param.get('type') or ''
param_unit = meta_param.get('unit')
param_weight = meta_param.get('weight')
param_min = meta_param.get('min')
param_max = meta_param.get('max')
param_min = meta_param.get('min') or meta_param.get('warn_low')
param_max = meta_param.get('max') or meta_param.get('warn_hi')
param_family = meta_param.get('family') or ''
param_group = meta_param.get('group') or ''

Expand Down
15 changes: 8 additions & 7 deletions custom_components/dabpumps/entity_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,13 +578,14 @@ def get_number_step(self):
candidates = [1000, 100, 10, 1]

# find first candidate where min, max and diff are all dividable by (without remainder)
min = int(self._params.min)
max = int(self._params.max)
diff = max - min

for c in candidates:
if (min % c == 0) and (max % c == 0) and (diff % c == 0):
return c
if self._params.min is not None and self._params.max is not None:
min = int(self._params.min)
max = int(self._params.max)
diff = max - min

for c in candidates:
if (min % c == 0) and (max % c == 0) and (diff % c == 0):
return c

return None

2 changes: 1 addition & 1 deletion custom_components/dabpumps/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"issue_tracker": "https://github.com/ankohanse/hass-dab-pumps/issues",
"loggers": ["custom_components.dabpumps"],
"requirements": [],
"version": "2024.05.1"
"version": "2024.05.2"
}
14 changes: 8 additions & 6 deletions custom_components/dabpumps/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def _update_attributes(self, status, is_create):
if self._params.weight and self._params.weight != 1 and self._params.weight != 0:
# Convert to float
attr_precision = int(math.floor(math.log10(1.0 / self._params.weight)))
attr_min = round(float(self._params.min) * self._params.weight, attr_precision) if self._params.min is not None else None
attr_max = round(float(self._params.max) * self._params.weight, attr_precision) if self._params.max is not None else None
attr_min = float(self._params.min) if self._params.min is not None else None
attr_max = float(self._params.max) if self._params.max is not None else None
attr_val = round(float(status.val) * self._params.weight, attr_precision) if status.val is not None else None
attr_step = self._params.weight
else:
Expand All @@ -150,8 +150,10 @@ def _update_attributes(self, status, is_create):
self._attr_mode = NumberMode.BOX
self._attr_device_class = self.get_number_device_class()
self._attr_entity_category = self.get_entity_category()
self._attr_native_min_value = attr_min
self._attr_native_max_value = attr_max
if attr_min:
self._attr_native_min_value = attr_min
if attr_max:
self._attr_native_max_value = attr_max
self._attr_native_step = attr_step

self._attr_device_info = DeviceInfo(
Expand Down Expand Up @@ -179,8 +181,8 @@ async def async_set_native_value(self, value: float) -> None:
"""Change the selected option"""

if self._params.weight and self._params.weight != 1 and self._params.weight != 0:
# Convert to float
data_val = float(round(value / self._params.weight))
# Convert from float to int
data_val = int(round(value / self._params.weight))
else:
# Convert to int
data_val = int(value)
Expand Down

0 comments on commit 7337cd4

Please sign in to comment.