From 51361fbd2b03c522c2449169dcbaea5b94319079 Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Wed, 25 Aug 2021 11:50:54 +0200 Subject: [PATCH] Add configurable `state_class` to Modbus sensors (#54103) * add configurable state_class * Add test of new parameter. Co-authored-by: jan Iversen --- homeassistant/components/modbus/__init__.py | 3 +++ homeassistant/components/modbus/const.py | 1 + homeassistant/components/modbus/sensor.py | 2 ++ tests/components/modbus/test_sensor.py | 7 ++++++- 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 0ff3b67c79f..7d598a5464a 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -13,6 +13,7 @@ from homeassistant.components.cover import ( ) from homeassistant.components.sensor import ( DEVICE_CLASSES_SCHEMA as SENSOR_DEVICE_CLASSES_SCHEMA, + STATE_CLASSES_SCHEMA as SENSOR_STATE_CLASSES_SCHEMA, ) from homeassistant.components.switch import ( DEVICE_CLASSES_SCHEMA as SWITCH_DEVICE_CLASSES_SCHEMA, @@ -75,6 +76,7 @@ from .const import ( CONF_RETRY_ON_EMPTY, CONF_REVERSE_ORDER, CONF_SCALE, + CONF_STATE_CLASS, CONF_STATE_CLOSED, CONF_STATE_CLOSING, CONF_STATE_OFF, @@ -269,6 +271,7 @@ SENSOR_SCHEMA = vol.All( BASE_STRUCT_SCHEMA.extend( { vol.Optional(CONF_DEVICE_CLASS): SENSOR_DEVICE_CLASSES_SCHEMA, + vol.Optional(CONF_STATE_CLASS): SENSOR_STATE_CLASSES_SCHEMA, vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string, vol.Optional(CONF_REVERSE_ORDER): cv.boolean, } diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index 3bcd85053d2..b259b93285f 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -41,6 +41,7 @@ CONF_RETRY_ON_EMPTY = "retry_on_empty" CONF_REVERSE_ORDER = "reverse_order" CONF_PRECISION = "precision" CONF_SCALE = "scale" +CONF_STATE_CLASS = "state_class" CONF_STATE_CLOSED = "state_closed" CONF_STATE_CLOSING = "state_closing" CONF_STATE_OFF = "state_off" diff --git a/homeassistant/components/modbus/sensor.py b/homeassistant/components/modbus/sensor.py index 2041f8974da..c2f69065196 100644 --- a/homeassistant/components/modbus/sensor.py +++ b/homeassistant/components/modbus/sensor.py @@ -12,6 +12,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from . import get_hub from .base_platform import BaseStructPlatform +from .const import CONF_STATE_CLASS from .modbus import ModbusHub PARALLEL_UPDATES = 1 @@ -48,6 +49,7 @@ class ModbusRegisterSensor(BaseStructPlatform, RestoreEntity, SensorEntity): """Initialize the modbus register sensor.""" super().__init__(hub, entry) self._attr_native_unit_of_measurement = entry.get(CONF_UNIT_OF_MEASUREMENT) + self._attr_state_class = entry.get(CONF_STATE_CLASS) async def async_added_to_hass(self): """Handle entity which will be added.""" diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index a52f833be1c..0da9d86f262 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -9,6 +9,7 @@ from homeassistant.components.modbus.const import ( CONF_LAZY_ERROR, CONF_PRECISION, CONF_SCALE, + CONF_STATE_CLASS, CONF_SWAP, CONF_SWAP_BYTE, CONF_SWAP_NONE, @@ -20,7 +21,10 @@ from homeassistant.components.modbus.const import ( DATA_TYPE_STRING, DATA_TYPE_UINT, ) -from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.components.sensor import ( + DOMAIN as SENSOR_DOMAIN, + STATE_CLASS_MEASUREMENT, +) from homeassistant.const import ( CONF_ADDRESS, CONF_COUNT, @@ -62,6 +66,7 @@ ENTITY_ID = f"{SENSOR_DOMAIN}.{TEST_ENTITY_NAME}" CONF_PRECISION: 0, CONF_SCALE: 1, CONF_OFFSET: 0, + CONF_STATE_CLASS: STATE_CLASS_MEASUREMENT, CONF_LAZY_ERROR: 10, CONF_INPUT_TYPE: CALL_TYPE_REGISTER_HOLDING, CONF_DEVICE_CLASS: "battery",