From 76247414bfb01293ff13a029a0b0862bc8341608 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 4 Apr 2022 11:45:53 +0200 Subject: [PATCH] Add pylint plugin for deprecated STATE_CLASS_* (#69237) --- homeassistant/components/goodwe/sensor.py | 19 +++++----- homeassistant/components/homewizard/sensor.py | 23 ++++++------ .../components/tankerkoenig/sensor.py | 4 +-- pylint/plugins/hass_imports.py | 35 +++++++++++++++++++ 4 files changed, 57 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/goodwe/sensor.py b/homeassistant/components/goodwe/sensor.py index d3393221c3a..28718e602a6 100644 --- a/homeassistant/components/goodwe/sensor.py +++ b/homeassistant/components/goodwe/sensor.py @@ -8,10 +8,9 @@ from typing import Any from goodwe import Inverter, Sensor, SensorKind from homeassistant.components.sensor import ( - STATE_CLASS_MEASUREMENT, - STATE_CLASS_TOTAL_INCREASING, SensorEntity, SensorEntityDescription, + SensorStateClass, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -75,49 +74,49 @@ _DESCRIPTIONS = { "A": GoodweSensorEntityDescription( key="A", device_class=DEVICE_CLASS_CURRENT, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, ), "V": GoodweSensorEntityDescription( key="V", device_class=DEVICE_CLASS_VOLTAGE, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, ), "W": GoodweSensorEntityDescription( key="W", device_class=DEVICE_CLASS_POWER, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=POWER_WATT, ), "kWh": GoodweSensorEntityDescription( key="kWh", device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_TOTAL_INCREASING, + state_class=SensorStateClass.TOTAL_INCREASING, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, value=lambda sensor, prev, val: prev if "total" in sensor and not val else val, ), "C": GoodweSensorEntityDescription( key="C", device_class=DEVICE_CLASS_TEMPERATURE, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=TEMP_CELSIUS, ), "Hz": GoodweSensorEntityDescription( key="Hz", device_class=DEVICE_CLASS_VOLTAGE, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=FREQUENCY_HERTZ, ), "%": GoodweSensorEntityDescription( key="%", - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, native_unit_of_measurement=PERCENTAGE, ), } DIAG_SENSOR = GoodweSensorEntityDescription( key="_", - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, ) diff --git a/homeassistant/components/homewizard/sensor.py b/homeassistant/components/homewizard/sensor.py index 8863d819db2..d60586888af 100644 --- a/homeassistant/components/homewizard/sensor.py +++ b/homeassistant/components/homewizard/sensor.py @@ -5,10 +5,9 @@ import logging from typing import Final from homeassistant.components.sensor import ( - STATE_CLASS_MEASUREMENT, - STATE_CLASS_TOTAL_INCREASING, SensorEntity, SensorEntityDescription, + SensorStateClass, ) from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( @@ -55,7 +54,7 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = ( name="Wifi Strength", icon="mdi:wifi", native_unit_of_measurement=PERCENTAGE, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, ), @@ -64,63 +63,63 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = ( name="Total Power Import T1", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_TOTAL_INCREASING, + state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="total_power_import_t2_kwh", name="Total Power Import T2", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_TOTAL_INCREASING, + state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="total_power_export_t1_kwh", name="Total Power Export T1", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_TOTAL_INCREASING, + state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="total_power_export_t2_kwh", name="Total Power Export T2", native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, device_class=DEVICE_CLASS_ENERGY, - state_class=STATE_CLASS_TOTAL_INCREASING, + state_class=SensorStateClass.TOTAL_INCREASING, ), SensorEntityDescription( key="active_power_w", name="Active Power", native_unit_of_measurement=POWER_WATT, device_class=DEVICE_CLASS_POWER, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="active_power_l1_w", name="Active Power L1", native_unit_of_measurement=POWER_WATT, device_class=DEVICE_CLASS_POWER, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="active_power_l2_w", name="Active Power L2", native_unit_of_measurement=POWER_WATT, device_class=DEVICE_CLASS_POWER, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="active_power_l3_w", name="Active Power L3", native_unit_of_measurement=POWER_WATT, device_class=DEVICE_CLASS_POWER, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, ), SensorEntityDescription( key="total_gas_m3", name="Total Gas", native_unit_of_measurement=VOLUME_CUBIC_METERS, device_class=DEVICE_CLASS_GAS, - state_class=STATE_CLASS_TOTAL_INCREASING, + state_class=SensorStateClass.TOTAL_INCREASING, ), ) diff --git a/homeassistant/components/tankerkoenig/sensor.py b/homeassistant/components/tankerkoenig/sensor.py index bbaeda44fd7..898a38c3c14 100644 --- a/homeassistant/components/tankerkoenig/sensor.py +++ b/homeassistant/components/tankerkoenig/sensor.py @@ -3,7 +3,7 @@ from __future__ import annotations import logging -from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity +from homeassistant.components.sensor import SensorEntity, SensorStateClass from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( ATTR_ATTRIBUTION, @@ -65,7 +65,7 @@ async def async_setup_entry( class FuelPriceSensor(CoordinatorEntity, SensorEntity): """Contains prices for fuel in a given station.""" - _attr_state_class = STATE_CLASS_MEASUREMENT + _attr_state_class = SensorStateClass.MEASUREMENT _attr_icon = "mdi:gas-station" def __init__(self, fuel_type, station, coordinator, show_on_map): diff --git a/pylint/plugins/hass_imports.py b/pylint/plugins/hass_imports.py index beb7f87616f..9290654e245 100644 --- a/pylint/plugins/hass_imports.py +++ b/pylint/plugins/hass_imports.py @@ -1,12 +1,33 @@ """Plugin for checking imports.""" from __future__ import annotations +from dataclasses import dataclass +import re + from astroid import Import, ImportFrom, Module from pylint.checkers import BaseChecker from pylint.interfaces import IAstroidChecker from pylint.lint import PyLinter +@dataclass +class ObsoleteImportMatch: + """Class for pattern matching.""" + + constant: re.Pattern + reason: str + + +_OBSOLETE_IMPORT: dict[str, list[ObsoleteImportMatch]] = { + "homeassistant.components.sensor": [ + ObsoleteImportMatch( + reason="replaced by SensorStateClass enum", + constant=re.compile(r"^STATE_CLASS_(\w*)$"), + ), + ], +} + + class HassImportsFormatChecker(BaseChecker): # type: ignore[misc] """Checker for imports.""" @@ -20,6 +41,11 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc] "hass-relative-import", "Used when absolute import should be replaced with relative import", ), + "W0012": ( + "%s is deprecated, %s", + "hass-deprecated-import", + "Used when import is deprecated", + ), } options = () @@ -49,6 +75,15 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc] f"{self.current_package}." ): self.add_message("hass-relative-import", node=node) + elif obsolete_imports := _OBSOLETE_IMPORT.get(node.modname): + for name_tuple in node.names: + for obsolete_import in obsolete_imports: + if import_match := obsolete_import.constant.match(name_tuple[0]): + self.add_message( + "hass-deprecated-import", + node=node, + args=(import_match.string, obsolete_import.reason), + ) def register(linter: PyLinter) -> None: