From 082d9d65e26ae80032f24bd5200d5f30fac5d094 Mon Sep 17 00:00:00 2001 From: grahamjb1 <26122648+GrahamJB1@users.noreply.github.com> Date: Wed, 18 Jan 2023 00:17:55 +0000 Subject: [PATCH 1/7] modbus min/max values --- homeassistant/components/modbus/__init__.py | 8 ++++ .../components/modbus/base_platform.py | 45 +++++++++++++++++++ homeassistant/components/modbus/const.py | 4 ++ 3 files changed, 57 insertions(+) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index a62d8537a3365..528e7a72fed18 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -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, @@ -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, } ), ) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 9463847a4eea5..464265c8bed6c 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -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, @@ -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: + 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.""" @@ -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 + ): + v_temp = self._min_value + elif ( + self._max_value_threshold is not None + and v_temp > self._max_value_threshold + ): + 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 @@ -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 + 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 + 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 diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index 6ed52ae0544ef..8a4e9052274a2 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -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" From 902e2cde07e5e93cc4e631da46b7198a0c4ae36e Mon Sep 17 00:00:00 2001 From: grahamjb1 <26122648+GrahamJB1@users.noreply.github.com> Date: Wed, 18 Jan 2023 22:33:42 +0000 Subject: [PATCH 2/7] modbus zero clamp --- homeassistant/components/modbus/__init__.py | 6 +-- .../components/modbus/base_platform.py | 51 +++++++------------ homeassistant/components/modbus/const.py | 3 +- 3 files changed, 21 insertions(+), 39 deletions(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 528e7a72fed18..9b23926cc0436 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -79,10 +79,8 @@ 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, @@ -108,6 +106,7 @@ CONF_TARGET_TEMP, CONF_VERIFY, CONF_WRITE_TYPE, + CONF_ZERO_CLAMP_VALUE, DEFAULT_HUB, DEFAULT_SCAN_INTERVAL, DEFAULT_TEMP_UNIT, @@ -290,9 +289,8 @@ 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, + vol.Optional(CONF_ZERO_CLAMP_VALUE): number_validator, } ), ) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 464265c8bed6c..7301a6e70f7ac 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -45,9 +45,7 @@ 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, @@ -58,6 +56,7 @@ CONF_SWAP_WORD_BYTE, CONF_VERIFY, CONF_WRITE_TYPE, + CONF_ZERO_CLAMP_VALUE, SIGNAL_START_ENTITY, SIGNAL_STOP_ENTITY, DataType, @@ -107,12 +106,9 @@ def get_optional_numeric_config( 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 + self._zero_clamp_value = get_optional_numeric_config( + CONF_ZERO_CLAMP_VALUE, None ) @abstractmethod @@ -185,6 +181,17 @@ def _swap_registers(self, registers: list[int]) -> list[int]: registers.reverse() return registers + def __process_raw_value(self, entry: float | int) -> float | int: + """Process value from sensor with scaling, offset, min/max etc.""" + val: float | int = self._scale * entry + self._offset + if self._min_value is not None and val < self._min_value: + return self._min_value + if self._max_value is not None and val > self._max_value: + return self._max_value + if self._zero_clamp_value is not None and abs(val) < self._zero_clamp_value: + return 0 + return val + def unpack_structure_result(self, registers: list[int]) -> str | None: """Convert registers to proper result.""" @@ -204,20 +211,10 @@ def unpack_structure_result(self, registers: list[int]) -> str | None: # If unpack() returns a tuple greater than 1, don't try to process the value. # Instead, return the values of unpack(...) separated by commas. if len(val) > 1: - # Apply scale and precision to floats and ints + # Apply scale, precision, limits to floats and ints 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 - ): - v_temp = self._min_value - elif ( - self._max_value_threshold is not None - and v_temp > self._max_value_threshold - ): - v_temp = self._max_value + v_temp = self.__process_raw_value(entry) # 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 @@ -228,20 +225,8 @@ def unpack_structure_result(self, registers: list[int]) -> str | None: v_result.append(f"{float(v_temp):.{self._precision}f}") return ",".join(map(str, v_result)) - # 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 - 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 - and self._max_value is not None - ): - val_result = self._max_value + # Apply scale, precision, limits to floats and ints + val_result = self.__process_raw_value(val[0]) # 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 diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index 8a4e9052274a2..d9a7ed25e022e 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -27,10 +27,8 @@ 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" @@ -70,6 +68,7 @@ CONF_VERIFY = "verify" CONF_VERIFY_REGISTER = "verify_register" CONF_VERIFY_STATE = "verify_state" +CONF_ZERO_CLAMP_VALUE = "zero_clamp_value" CONF_WRITE_TYPE = "write_type" RTUOVERTCP = "rtuovertcp" From 87a5286ec91602fac6509421032e3be2fbf690a8 Mon Sep 17 00:00:00 2001 From: grahamjb1 <26122648+GrahamJB1@users.noreply.github.com> Date: Wed, 18 Jan 2023 22:34:33 +0000 Subject: [PATCH 3/7] modbus zero clamp --- homeassistant/components/modbus/base_platform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 7301a6e70f7ac..3c82e7b80237f 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -188,7 +188,7 @@ def __process_raw_value(self, entry: float | int) -> float | int: return self._min_value if self._max_value is not None and val > self._max_value: return self._max_value - if self._zero_clamp_value is not None and abs(val) < self._zero_clamp_value: + if self._zero_clamp_value is not None and abs(val) <= self._zero_clamp_value: return 0 return val From 72e9a6284613e5e397a0429a620883f9d204225d Mon Sep 17 00:00:00 2001 From: grahamjb1 <26122648+GrahamJB1@users.noreply.github.com> Date: Wed, 18 Jan 2023 23:01:04 +0000 Subject: [PATCH 4/7] modbus zero clamp --- tests/components/modbus/test_sensor.py | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index bb9e4285c427a..c735d5ecc0d8d 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -7,6 +7,8 @@ CONF_DATA_TYPE, CONF_INPUT_TYPE, CONF_LAZY_ERROR, + CONF_MAX_VALUE, + CONF_MIN_VALUE, CONF_PRECISION, CONF_SCALE, CONF_SLAVE_COUNT, @@ -15,6 +17,7 @@ CONF_SWAP_NONE, CONF_SWAP_WORD, CONF_SWAP_WORD_BYTE, + CONF_ZERO_CLAMP_VALUE, MODBUS_DOMAIN, DataType, ) @@ -532,6 +535,42 @@ async def test_config_wrong_struct_sensor(hass, error_message, mock_modbus, capl False, str(int(0x04030201)), ), + ( + { + CONF_DATA_TYPE: DataType.INT32, + CONF_MAX_VALUE: int(0x02010400), + }, + [0x0201, 0x0403], + False, + str(int(0x02010400)), + ), + ( + { + CONF_DATA_TYPE: DataType.INT32, + CONF_MIN_VALUE: int(0x02010404), + }, + [0x0201, 0x0403], + False, + str(int(0x02010404)), + ), + ( + { + CONF_DATA_TYPE: DataType.INT32, + CONF_ZERO_CLAMP_VALUE: int(0x00000001), + }, + [0x0000, 0x0002], + False, + str(int(0x00000002)), + ), + ( + { + CONF_DATA_TYPE: DataType.INT32, + CONF_ZERO_CLAMP_VALUE: int(0x00000002), + }, + [0x0000, 0x0002], + False, + str(int(0)), + ), ( { CONF_INPUT_TYPE: CALL_TYPE_REGISTER_INPUT, From 1bfd916aff1c48822020892b65a73972d2d8366e Mon Sep 17 00:00:00 2001 From: grahamjb1 <26122648+GrahamJB1@users.noreply.github.com> Date: Thu, 19 Jan 2023 09:59:33 +0000 Subject: [PATCH 5/7] modbus remove unused default --- homeassistant/components/modbus/base_platform.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 3c82e7b80237f..9df70eed6c20a 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -95,21 +95,17 @@ 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: + def get_optional_numeric_config(config_name: str) -> int | float | None: + if (val := entry.get(config_name)) is None: 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._max_value = get_optional_numeric_config(CONF_MAX_VALUE, None) - self._zero_clamp_value = get_optional_numeric_config( - CONF_ZERO_CLAMP_VALUE, None - ) + self._min_value = get_optional_numeric_config(CONF_MIN_VALUE) + self._max_value = get_optional_numeric_config(CONF_MAX_VALUE) + self._zero_clamp_value = get_optional_numeric_config(CONF_ZERO_CLAMP_VALUE) @abstractmethod async def async_update(self, now: datetime | None = None) -> None: From 3e4093d741fc09495ece6f97833d2c00260ed1f0 Mon Sep 17 00:00:00 2001 From: grahamjb1 <26122648+GrahamJB1@users.noreply.github.com> Date: Thu, 19 Jan 2023 11:13:50 +0000 Subject: [PATCH 6/7] modbus deadzone --- homeassistant/components/modbus/__init__.py | 4 ++-- homeassistant/components/modbus/base_platform.py | 9 ++++++--- homeassistant/components/modbus/const.py | 2 +- tests/components/modbus/test_sensor.py | 6 +++--- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 9b23926cc0436..7312bbfac6ef1 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -63,6 +63,7 @@ CONF_CLIMATES, CONF_CLOSE_COMM_ON_ERROR, CONF_DATA_TYPE, + CONF_DEADZONE_THRESHOLD, CONF_FANS, CONF_HUB, CONF_HVAC_MODE_AUTO, @@ -106,7 +107,6 @@ CONF_TARGET_TEMP, CONF_VERIFY, CONF_WRITE_TYPE, - CONF_ZERO_CLAMP_VALUE, DEFAULT_HUB, DEFAULT_SCAN_INTERVAL, DEFAULT_TEMP_UNIT, @@ -290,7 +290,7 @@ vol.Optional(CONF_SLAVE_COUNT, default=0): cv.positive_int, vol.Optional(CONF_MIN_VALUE): number_validator, vol.Optional(CONF_MAX_VALUE): number_validator, - vol.Optional(CONF_ZERO_CLAMP_VALUE): number_validator, + vol.Optional(CONF_DEADZONE_THRESHOLD): number_validator, } ), ) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 9df70eed6c20a..1217f0de0b157 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -42,6 +42,7 @@ CALL_TYPE_X_COILS, CALL_TYPE_X_REGISTER_HOLDINGS, CONF_DATA_TYPE, + CONF_DEADZONE_THRESHOLD, CONF_INPUT_TYPE, CONF_LAZY_ERROR, CONF_MAX_VALUE, @@ -56,7 +57,6 @@ CONF_SWAP_WORD_BYTE, CONF_VERIFY, CONF_WRITE_TYPE, - CONF_ZERO_CLAMP_VALUE, SIGNAL_START_ENTITY, SIGNAL_STOP_ENTITY, DataType, @@ -105,7 +105,7 @@ def get_optional_numeric_config(config_name: str) -> int | float | None: self._min_value = get_optional_numeric_config(CONF_MIN_VALUE) self._max_value = get_optional_numeric_config(CONF_MAX_VALUE) - self._zero_clamp_value = get_optional_numeric_config(CONF_ZERO_CLAMP_VALUE) + self._dead_zone_threshold = get_optional_numeric_config(CONF_DEADZONE_THRESHOLD) @abstractmethod async def async_update(self, now: datetime | None = None) -> None: @@ -184,7 +184,10 @@ def __process_raw_value(self, entry: float | int) -> float | int: return self._min_value if self._max_value is not None and val > self._max_value: return self._max_value - if self._zero_clamp_value is not None and abs(val) <= self._zero_clamp_value: + if ( + self._dead_zone_threshold is not None + and abs(val) <= self._dead_zone_threshold + ): return 0 return val diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index d9a7ed25e022e..6362ae046a3cb 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -20,6 +20,7 @@ CONF_CURRENT_TEMP = "current_temp_register" CONF_CURRENT_TEMP_REGISTER_TYPE = "current_temp_register_type" CONF_DATA_TYPE = "data_type" +CONF_DEADZONE_THRESHOLD = "deadzone_threshold" CONF_FANS = "fans" CONF_HUB = "hub" CONF_INPUTS = "inputs" @@ -68,7 +69,6 @@ CONF_VERIFY = "verify" CONF_VERIFY_REGISTER = "verify_register" CONF_VERIFY_STATE = "verify_state" -CONF_ZERO_CLAMP_VALUE = "zero_clamp_value" CONF_WRITE_TYPE = "write_type" RTUOVERTCP = "rtuovertcp" diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index c735d5ecc0d8d..ffb8c85dfa34d 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -5,6 +5,7 @@ CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_INPUT, CONF_DATA_TYPE, + CONF_DEADZONE_THRESHOLD, CONF_INPUT_TYPE, CONF_LAZY_ERROR, CONF_MAX_VALUE, @@ -17,7 +18,6 @@ CONF_SWAP_NONE, CONF_SWAP_WORD, CONF_SWAP_WORD_BYTE, - CONF_ZERO_CLAMP_VALUE, MODBUS_DOMAIN, DataType, ) @@ -556,7 +556,7 @@ async def test_config_wrong_struct_sensor(hass, error_message, mock_modbus, capl ( { CONF_DATA_TYPE: DataType.INT32, - CONF_ZERO_CLAMP_VALUE: int(0x00000001), + CONF_DEADZONE_THRESHOLD: int(0x00000001), }, [0x0000, 0x0002], False, @@ -565,7 +565,7 @@ async def test_config_wrong_struct_sensor(hass, error_message, mock_modbus, capl ( { CONF_DATA_TYPE: DataType.INT32, - CONF_ZERO_CLAMP_VALUE: int(0x00000002), + CONF_DEADZONE_THRESHOLD: int(0x00000002), }, [0x0000, 0x0002], False, From 7611751f49865d6073e983e9e51c23f6e04647d7 Mon Sep 17 00:00:00 2001 From: grahamjb1 <26122648+GrahamJB1@users.noreply.github.com> Date: Thu, 19 Jan 2023 11:57:56 +0000 Subject: [PATCH 7/7] modbus zero suppress --- homeassistant/components/modbus/__init__.py | 4 ++-- homeassistant/components/modbus/base_platform.py | 9 +++------ homeassistant/components/modbus/const.py | 2 +- tests/components/modbus/test_sensor.py | 6 +++--- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 7312bbfac6ef1..e8ba48ba13069 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -63,7 +63,6 @@ CONF_CLIMATES, CONF_CLOSE_COMM_ON_ERROR, CONF_DATA_TYPE, - CONF_DEADZONE_THRESHOLD, CONF_FANS, CONF_HUB, CONF_HVAC_MODE_AUTO, @@ -107,6 +106,7 @@ CONF_TARGET_TEMP, CONF_VERIFY, CONF_WRITE_TYPE, + CONF_ZERO_SUPPRESS, DEFAULT_HUB, DEFAULT_SCAN_INTERVAL, DEFAULT_TEMP_UNIT, @@ -290,7 +290,7 @@ vol.Optional(CONF_SLAVE_COUNT, default=0): cv.positive_int, vol.Optional(CONF_MIN_VALUE): number_validator, vol.Optional(CONF_MAX_VALUE): number_validator, - vol.Optional(CONF_DEADZONE_THRESHOLD): number_validator, + vol.Optional(CONF_ZERO_SUPPRESS): number_validator, } ), ) diff --git a/homeassistant/components/modbus/base_platform.py b/homeassistant/components/modbus/base_platform.py index 1217f0de0b157..337919c81f77f 100644 --- a/homeassistant/components/modbus/base_platform.py +++ b/homeassistant/components/modbus/base_platform.py @@ -42,7 +42,6 @@ CALL_TYPE_X_COILS, CALL_TYPE_X_REGISTER_HOLDINGS, CONF_DATA_TYPE, - CONF_DEADZONE_THRESHOLD, CONF_INPUT_TYPE, CONF_LAZY_ERROR, CONF_MAX_VALUE, @@ -57,6 +56,7 @@ CONF_SWAP_WORD_BYTE, CONF_VERIFY, CONF_WRITE_TYPE, + CONF_ZERO_SUPPRESS, SIGNAL_START_ENTITY, SIGNAL_STOP_ENTITY, DataType, @@ -105,7 +105,7 @@ def get_optional_numeric_config(config_name: str) -> int | float | None: self._min_value = get_optional_numeric_config(CONF_MIN_VALUE) self._max_value = get_optional_numeric_config(CONF_MAX_VALUE) - self._dead_zone_threshold = get_optional_numeric_config(CONF_DEADZONE_THRESHOLD) + self._zero_suppress = get_optional_numeric_config(CONF_ZERO_SUPPRESS) @abstractmethod async def async_update(self, now: datetime | None = None) -> None: @@ -184,10 +184,7 @@ def __process_raw_value(self, entry: float | int) -> float | int: return self._min_value if self._max_value is not None and val > self._max_value: return self._max_value - if ( - self._dead_zone_threshold is not None - and abs(val) <= self._dead_zone_threshold - ): + if self._zero_suppress is not None and abs(val) <= self._zero_suppress: return 0 return val diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index 6362ae046a3cb..b7fcfee9053cb 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -20,7 +20,6 @@ CONF_CURRENT_TEMP = "current_temp_register" CONF_CURRENT_TEMP_REGISTER_TYPE = "current_temp_register_type" CONF_DATA_TYPE = "data_type" -CONF_DEADZONE_THRESHOLD = "deadzone_threshold" CONF_FANS = "fans" CONF_HUB = "hub" CONF_INPUTS = "inputs" @@ -70,6 +69,7 @@ CONF_VERIFY_REGISTER = "verify_register" CONF_VERIFY_STATE = "verify_state" CONF_WRITE_TYPE = "write_type" +CONF_ZERO_SUPPRESS = "zero_suppress" RTUOVERTCP = "rtuovertcp" SERIAL = "serial" diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index ffb8c85dfa34d..50bc8a25daa3c 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -5,7 +5,6 @@ CALL_TYPE_REGISTER_HOLDING, CALL_TYPE_REGISTER_INPUT, CONF_DATA_TYPE, - CONF_DEADZONE_THRESHOLD, CONF_INPUT_TYPE, CONF_LAZY_ERROR, CONF_MAX_VALUE, @@ -18,6 +17,7 @@ CONF_SWAP_NONE, CONF_SWAP_WORD, CONF_SWAP_WORD_BYTE, + CONF_ZERO_SUPPRESS, MODBUS_DOMAIN, DataType, ) @@ -556,7 +556,7 @@ async def test_config_wrong_struct_sensor(hass, error_message, mock_modbus, capl ( { CONF_DATA_TYPE: DataType.INT32, - CONF_DEADZONE_THRESHOLD: int(0x00000001), + CONF_ZERO_SUPPRESS: int(0x00000001), }, [0x0000, 0x0002], False, @@ -565,7 +565,7 @@ async def test_config_wrong_struct_sensor(hass, error_message, mock_modbus, capl ( { CONF_DATA_TYPE: DataType.INT32, - CONF_DEADZONE_THRESHOLD: int(0x00000002), + CONF_ZERO_SUPPRESS: int(0x00000002), }, [0x0000, 0x0002], False,