Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions homeassistant/components/modbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,11 @@
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_MAX_TEMP,
CONF_MAX_VALUE,
CONF_MAX_VALUE_THRESHOLD,
CONF_MIN_TEMP,
CONF_MIN_VALUE,
CONF_MIN_VALUE_THRESHOLD,
CONF_MSG_WAIT,
CONF_PARITY,
CONF_PRECISION,
Expand Down Expand Up @@ -285,6 +289,10 @@
vol.Optional(CONF_STATE_CLASS): SENSOR_STATE_CLASSES_SCHEMA,
vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_SLAVE_COUNT, default=0): cv.positive_int,
vol.Optional(CONF_MIN_VALUE): number_validator,
vol.Optional(CONF_MIN_VALUE_THRESHOLD): number_validator,
vol.Optional(CONF_MAX_VALUE): number_validator,
vol.Optional(CONF_MAX_VALUE_THRESHOLD): number_validator,
}
),
)
Expand Down
45 changes: 45 additions & 0 deletions homeassistant/components/modbus/base_platform.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
CONF_DATA_TYPE,
CONF_INPUT_TYPE,
CONF_LAZY_ERROR,
CONF_MAX_VALUE,
CONF_MAX_VALUE_THRESHOLD,
CONF_MIN_VALUE,
CONF_MIN_VALUE_THRESHOLD,
CONF_PRECISION,
CONF_SCALE,
CONF_STATE_OFF,
Expand Down Expand Up @@ -92,6 +96,25 @@ def __init__(self, hub: ModbusHub, entry: dict[str, Any]) -> None:
self._lazy_error_count = entry[CONF_LAZY_ERROR]
self._lazy_errors = self._lazy_error_count

def get_optional_numeric_config(
config_name: str, default_val: int | float | None
) -> int | float | None:
if (val := entry.get(config_name, default_val)) is None:
Comment thread
GrahamJB1 marked this conversation as resolved.
Outdated
return None
assert isinstance(
val, (float, int)
), f"Expected float or int but {config_name} was {type(val)}"
return val

self._min_value = get_optional_numeric_config(CONF_MIN_VALUE, None)
self._min_value_threshold = get_optional_numeric_config(
CONF_MIN_VALUE_THRESHOLD, self._min_value
)
self._max_value = get_optional_numeric_config(CONF_MAX_VALUE, None)
self._max_value_threshold = get_optional_numeric_config(
CONF_MAX_VALUE_THRESHOLD, self._max_value
)

@abstractmethod
async def async_update(self, now: datetime | None = None) -> None:
"""Virtual function to be overwritten."""
Expand Down Expand Up @@ -185,6 +208,16 @@ def unpack_structure_result(self, registers: list[int]) -> str | None:
v_result = []
for entry in val:
v_temp = self._scale * entry + self._offset
if (
self._min_value_threshold is not None
and v_temp < self._min_value_threshold
Comment thread
GrahamJB1 marked this conversation as resolved.
Outdated
):
v_temp = self._min_value
elif (
self._max_value_threshold is not None
and v_temp > self._max_value_threshold
Comment thread
GrahamJB1 marked this conversation as resolved.
Outdated
):
v_temp = self._max_value

# We could convert int to float, and the code would still work; however
# we lose some precision, and unit tests will fail. Therefore, we do
Expand All @@ -197,6 +230,18 @@ def unpack_structure_result(self, registers: list[int]) -> str | None:

# Apply scale and precision to floats and ints
val_result: float | int = self._scale * val[0] + self._offset
if (
self._min_value_threshold is not None
Comment thread
GrahamJB1 marked this conversation as resolved.
Outdated
and val_result < self._min_value_threshold
and self._min_value is not None
):
val_result = self._min_value
elif (
self._max_value_threshold is not None
and val_result > self._max_value_threshold
Comment thread
GrahamJB1 marked this conversation as resolved.
Outdated
and self._max_value is not None
):
val_result = self._max_value

# We could convert int to float, and the code would still work; however
# we lose some precision, and unit tests will fail. Therefore, we do
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/modbus/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
CONF_INPUT_TYPE = "input_type"
CONF_LAZY_ERROR = "lazy_error_count"
CONF_MAX_TEMP = "max_temp"
CONF_MAX_VALUE = "max_value"
CONF_MAX_VALUE_THRESHOLD = "max_value_threshold"
CONF_MIN_TEMP = "min_temp"
CONF_MIN_VALUE = "min_value"
CONF_MIN_VALUE_THRESHOLD = "min_value_threshold"
CONF_MSG_WAIT = "message_wait_milliseconds"
CONF_PARITY = "parity"
CONF_REGISTER = "register"
Expand Down