Add pylint plugin for deprecated STATE_CLASS_* (#69237)

This commit is contained in:
epenet 2022-04-04 11:45:53 +02:00 committed by GitHub
parent c79b361927
commit 76247414bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 24 deletions

View File

@ -8,10 +8,9 @@ from typing import Any
from goodwe import Inverter, Sensor, SensorKind from goodwe import Inverter, Sensor, SensorKind
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -75,49 +74,49 @@ _DESCRIPTIONS = {
"A": GoodweSensorEntityDescription( "A": GoodweSensorEntityDescription(
key="A", key="A",
device_class=DEVICE_CLASS_CURRENT, device_class=DEVICE_CLASS_CURRENT,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
), ),
"V": GoodweSensorEntityDescription( "V": GoodweSensorEntityDescription(
key="V", key="V",
device_class=DEVICE_CLASS_VOLTAGE, device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
), ),
"W": GoodweSensorEntityDescription( "W": GoodweSensorEntityDescription(
key="W", key="W",
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
), ),
"kWh": GoodweSensorEntityDescription( "kWh": GoodweSensorEntityDescription(
key="kWh", key="kWh",
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
value=lambda sensor, prev, val: prev if "total" in sensor and not val else val, value=lambda sensor, prev, val: prev if "total" in sensor and not val else val,
), ),
"C": GoodweSensorEntityDescription( "C": GoodweSensorEntityDescription(
key="C", key="C",
device_class=DEVICE_CLASS_TEMPERATURE, device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=TEMP_CELSIUS, native_unit_of_measurement=TEMP_CELSIUS,
), ),
"Hz": GoodweSensorEntityDescription( "Hz": GoodweSensorEntityDescription(
key="Hz", key="Hz",
device_class=DEVICE_CLASS_VOLTAGE, device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=FREQUENCY_HERTZ,
), ),
"%": GoodweSensorEntityDescription( "%": GoodweSensorEntityDescription(
key="%", key="%",
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
), ),
} }
DIAG_SENSOR = GoodweSensorEntityDescription( DIAG_SENSOR = GoodweSensorEntityDescription(
key="_", key="_",
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
) )

View File

@ -5,10 +5,9 @@ import logging
from typing import Final from typing import Final
from homeassistant.components.sensor import ( from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
@ -55,7 +54,7 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
name="Wifi Strength", name="Wifi Strength",
icon="mdi:wifi", icon="mdi:wifi",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
), ),
@ -64,63 +63,63 @@ SENSORS: Final[tuple[SensorEntityDescription, ...]] = (
name="Total Power Import T1", name="Total Power Import T1",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="total_power_import_t2_kwh", key="total_power_import_t2_kwh",
name="Total Power Import T2", name="Total Power Import T2",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="total_power_export_t1_kwh", key="total_power_export_t1_kwh",
name="Total Power Export T1", name="Total Power Export T1",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="total_power_export_t2_kwh", key="total_power_export_t2_kwh",
name="Total Power Export T2", name="Total Power Export T2",
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR, native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
device_class=DEVICE_CLASS_ENERGY, device_class=DEVICE_CLASS_ENERGY,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
SensorEntityDescription( SensorEntityDescription(
key="active_power_w", key="active_power_w",
name="Active Power", name="Active Power",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SensorEntityDescription( SensorEntityDescription(
key="active_power_l1_w", key="active_power_l1_w",
name="Active Power L1", name="Active Power L1",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SensorEntityDescription( SensorEntityDescription(
key="active_power_l2_w", key="active_power_l2_w",
name="Active Power L2", name="Active Power L2",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SensorEntityDescription( SensorEntityDescription(
key="active_power_l3_w", key="active_power_l3_w",
name="Active Power L3", name="Active Power L3",
native_unit_of_measurement=POWER_WATT, native_unit_of_measurement=POWER_WATT,
device_class=DEVICE_CLASS_POWER, device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
SensorEntityDescription( SensorEntityDescription(
key="total_gas_m3", key="total_gas_m3",
name="Total Gas", name="Total Gas",
native_unit_of_measurement=VOLUME_CUBIC_METERS, native_unit_of_measurement=VOLUME_CUBIC_METERS,
device_class=DEVICE_CLASS_GAS, device_class=DEVICE_CLASS_GAS,
state_class=STATE_CLASS_TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
), ),
) )

View File

@ -3,7 +3,7 @@ from __future__ import annotations
import logging 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.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
ATTR_ATTRIBUTION, ATTR_ATTRIBUTION,
@ -65,7 +65,7 @@ async def async_setup_entry(
class FuelPriceSensor(CoordinatorEntity, SensorEntity): class FuelPriceSensor(CoordinatorEntity, SensorEntity):
"""Contains prices for fuel in a given station.""" """Contains prices for fuel in a given station."""
_attr_state_class = STATE_CLASS_MEASUREMENT _attr_state_class = SensorStateClass.MEASUREMENT
_attr_icon = "mdi:gas-station" _attr_icon = "mdi:gas-station"
def __init__(self, fuel_type, station, coordinator, show_on_map): def __init__(self, fuel_type, station, coordinator, show_on_map):

View File

@ -1,12 +1,33 @@
"""Plugin for checking imports.""" """Plugin for checking imports."""
from __future__ import annotations from __future__ import annotations
from dataclasses import dataclass
import re
from astroid import Import, ImportFrom, Module from astroid import Import, ImportFrom, Module
from pylint.checkers import BaseChecker from pylint.checkers import BaseChecker
from pylint.interfaces import IAstroidChecker from pylint.interfaces import IAstroidChecker
from pylint.lint import PyLinter 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] class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
"""Checker for imports.""" """Checker for imports."""
@ -20,6 +41,11 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
"hass-relative-import", "hass-relative-import",
"Used when absolute import should be replaced with 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 = () options = ()
@ -49,6 +75,15 @@ class HassImportsFormatChecker(BaseChecker): # type: ignore[misc]
f"{self.current_package}." f"{self.current_package}."
): ):
self.add_message("hass-relative-import", node=node) 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: def register(linter: PyLinter) -> None: