From cd0ae66d58bf74b5e7b2235573b886bf9755d594 Mon Sep 17 00:00:00 2001 From: Matthias Alphart Date: Fri, 27 Aug 2021 05:54:50 +0200 Subject: [PATCH] Add CONF_STATE_CLASS to `sensor/__init__.py` (#54106) * add CONF_STATE_CLASS to const.py * move to `sensor/__init__.py` * move to sensor/const.py * Revert "move to sensor/const.py" This reverts commit 604d0d066bfcb93f1a11e3d7732d430ab6de8d59. * move it to `sensor/const.py` but import it from `sensor/__init__.py` * add Modbus --- homeassistant/components/knx/schema.py | 3 +-- homeassistant/components/knx/sensor.py | 8 ++++++-- homeassistant/components/modbus/__init__.py | 2 +- homeassistant/components/modbus/const.py | 1 - homeassistant/components/modbus/sensor.py | 3 +-- homeassistant/components/mqtt/sensor.py | 2 +- homeassistant/components/sensor/__init__.py | 2 ++ homeassistant/components/sensor/const.py | 4 ++++ homeassistant/components/template/const.py | 1 - homeassistant/components/template/sensor.py | 2 +- tests/components/modbus/test_sensor.py | 2 +- 11 files changed, 18 insertions(+), 12 deletions(-) create mode 100644 homeassistant/components/sensor/const.py diff --git a/homeassistant/components/knx/schema.py b/homeassistant/components/knx/schema.py index 65ff6b3b8fa..89dab40958c 100644 --- a/homeassistant/components/knx/schema.py +++ b/homeassistant/components/knx/schema.py @@ -18,7 +18,7 @@ from homeassistant.components.binary_sensor import ( ) from homeassistant.components.climate.const import HVAC_MODE_HEAT, HVAC_MODES from homeassistant.components.cover import DEVICE_CLASSES as COVER_DEVICE_CLASSES -from homeassistant.components.sensor import STATE_CLASSES_SCHEMA +from homeassistant.components.sensor import CONF_STATE_CLASS, STATE_CLASSES_SCHEMA from homeassistant.const import ( CONF_DEVICE_CLASS, CONF_ENTITY_ID, @@ -730,7 +730,6 @@ class SensorSchema(KNXPlatformSchema): CONF_ALWAYS_CALLBACK = "always_callback" CONF_STATE_ADDRESS = CONF_STATE_ADDRESS - CONF_STATE_CLASS = "state_class" CONF_SYNC_STATE = CONF_SYNC_STATE DEFAULT_NAME = "KNX Sensor" diff --git a/homeassistant/components/knx/sensor.py b/homeassistant/components/knx/sensor.py index 933ba7bf30d..c1f68e4c376 100644 --- a/homeassistant/components/knx/sensor.py +++ b/homeassistant/components/knx/sensor.py @@ -6,7 +6,11 @@ from typing import Any from xknx import XKNX from xknx.devices import Sensor as XknxSensor -from homeassistant.components.sensor import DEVICE_CLASSES, SensorEntity +from homeassistant.components.sensor import ( + CONF_STATE_CLASS, + DEVICE_CLASSES, + SensorEntity, +) from homeassistant.const import CONF_NAME, CONF_TYPE from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback @@ -63,7 +67,7 @@ class KNXSensor(KnxEntity, SensorEntity): self._attr_force_update = self._device.always_callback self._attr_unique_id = str(self._device.sensor_value.group_address_state) self._attr_native_unit_of_measurement = self._device.unit_of_measurement() - self._attr_state_class = config.get(SensorSchema.CONF_STATE_CLASS) + self._attr_state_class = config.get(CONF_STATE_CLASS) @property def native_value(self) -> StateType: diff --git a/homeassistant/components/modbus/__init__.py b/homeassistant/components/modbus/__init__.py index 26d196f8af9..df1fc2f6995 100644 --- a/homeassistant/components/modbus/__init__.py +++ b/homeassistant/components/modbus/__init__.py @@ -12,6 +12,7 @@ from homeassistant.components.cover import ( DEVICE_CLASSES_SCHEMA as COVER_DEVICE_CLASSES_SCHEMA, ) from homeassistant.components.sensor import ( + CONF_STATE_CLASS, DEVICE_CLASSES_SCHEMA as SENSOR_DEVICE_CLASSES_SCHEMA, STATE_CLASSES_SCHEMA as SENSOR_STATE_CLASSES_SCHEMA, ) @@ -76,7 +77,6 @@ from .const import ( CONF_RETRY_ON_EMPTY, CONF_REVERSE_ORDER, CONF_SCALE, - CONF_STATE_CLASS, CONF_STATE_CLOSED, CONF_STATE_CLOSING, CONF_STATE_OFF, diff --git a/homeassistant/components/modbus/const.py b/homeassistant/components/modbus/const.py index b259b93285f..3bcd85053d2 100644 --- a/homeassistant/components/modbus/const.py +++ b/homeassistant/components/modbus/const.py @@ -41,7 +41,6 @@ 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 c2f69065196..83ffafc7441 100644 --- a/homeassistant/components/modbus/sensor.py +++ b/homeassistant/components/modbus/sensor.py @@ -4,7 +4,7 @@ from __future__ import annotations import logging from typing import Any -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import CONF_STATE_CLASS, SensorEntity from homeassistant.const import CONF_NAME, CONF_SENSORS, CONF_UNIT_OF_MEASUREMENT from homeassistant.core import HomeAssistant from homeassistant.helpers.restore_state import RestoreEntity @@ -12,7 +12,6 @@ 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 diff --git a/homeassistant/components/mqtt/sensor.py b/homeassistant/components/mqtt/sensor.py index 16c19c8fc51..c5441840878 100644 --- a/homeassistant/components/mqtt/sensor.py +++ b/homeassistant/components/mqtt/sensor.py @@ -9,6 +9,7 @@ import voluptuous as vol from homeassistant.components import sensor from homeassistant.components.sensor import ( + CONF_STATE_CLASS, DEVICE_CLASSES_SCHEMA, STATE_CLASSES_SCHEMA, SensorEntity, @@ -42,7 +43,6 @@ _LOGGER = logging.getLogger(__name__) CONF_EXPIRE_AFTER = "expire_after" CONF_LAST_RESET_TOPIC = "last_reset_topic" CONF_LAST_RESET_VALUE_TEMPLATE = "last_reset_value_template" -CONF_STATE_CLASS = "state_class" MQTT_SENSOR_ATTRIBUTES_BLOCKED = frozenset( { diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index fafaabbd217..0518495a0f0 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -51,6 +51,8 @@ from homeassistant.helpers.entity import Entity, EntityDescription from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType, StateType +from .const import CONF_STATE_CLASS # noqa: F401 + _LOGGER: Final = logging.getLogger(__name__) ATTR_LAST_RESET: Final = "last_reset" # Deprecated, to be removed in 2021.11 diff --git a/homeassistant/components/sensor/const.py b/homeassistant/components/sensor/const.py new file mode 100644 index 00000000000..54d683242ea --- /dev/null +++ b/homeassistant/components/sensor/const.py @@ -0,0 +1,4 @@ +"""Constants for sensor.""" +from typing import Final + +CONF_STATE_CLASS: Final = "state_class" diff --git a/homeassistant/components/template/const.py b/homeassistant/components/template/const.py index 0309321afbc..54d213be0b1 100644 --- a/homeassistant/components/template/const.py +++ b/homeassistant/components/template/const.py @@ -27,4 +27,3 @@ CONF_AVAILABILITY = "availability" CONF_ATTRIBUTES = "attributes" CONF_PICTURE = "picture" CONF_OBJECT_ID = "object_id" -CONF_STATE_CLASS = "state_class" diff --git a/homeassistant/components/template/sensor.py b/homeassistant/components/template/sensor.py index d51b18e294b..4214323c8ee 100644 --- a/homeassistant/components/template/sensor.py +++ b/homeassistant/components/template/sensor.py @@ -4,6 +4,7 @@ from __future__ import annotations import voluptuous as vol from homeassistant.components.sensor import ( + CONF_STATE_CLASS, DEVICE_CLASSES_SCHEMA, DOMAIN as SENSOR_DOMAIN, ENTITY_ID_FORMAT, @@ -38,7 +39,6 @@ from .const import ( CONF_AVAILABILITY_TEMPLATE, CONF_OBJECT_ID, CONF_PICTURE, - CONF_STATE_CLASS, CONF_TRIGGER, ) from .template_entity import TemplateEntity diff --git a/tests/components/modbus/test_sensor.py b/tests/components/modbus/test_sensor.py index 0da9d86f262..5227be835db 100644 --- a/tests/components/modbus/test_sensor.py +++ b/tests/components/modbus/test_sensor.py @@ -9,7 +9,6 @@ from homeassistant.components.modbus.const import ( CONF_LAZY_ERROR, CONF_PRECISION, CONF_SCALE, - CONF_STATE_CLASS, CONF_SWAP, CONF_SWAP_BYTE, CONF_SWAP_NONE, @@ -22,6 +21,7 @@ from homeassistant.components.modbus.const import ( DATA_TYPE_UINT, ) from homeassistant.components.sensor import ( + CONF_STATE_CLASS, DOMAIN as SENSOR_DOMAIN, STATE_CLASS_MEASUREMENT, )