Use SensorDeviceClass in goodwe (#69263)

This commit is contained in:
epenet 2022-04-04 17:45:44 +02:00 committed by GitHub
parent c8d4b2ecab
commit 6745e01b80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,18 +8,13 @@ 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 (
SensorDeviceClass,
SensorEntity, SensorEntity,
SensorEntityDescription, SensorEntityDescription,
SensorStateClass, SensorStateClass,
) )
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ( from homeassistant.const import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
ELECTRIC_CURRENT_AMPERE, ELECTRIC_CURRENT_AMPERE,
ELECTRIC_POTENTIAL_VOLT, ELECTRIC_POTENTIAL_VOLT,
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
@ -73,38 +68,38 @@ class GoodweSensorEntityDescription(SensorEntityDescription):
_DESCRIPTIONS = { _DESCRIPTIONS = {
"A": GoodweSensorEntityDescription( "A": GoodweSensorEntityDescription(
key="A", key="A",
device_class=DEVICE_CLASS_CURRENT, device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.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=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.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=SensorDeviceClass.POWER,
state_class=SensorStateClass.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=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.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=SensorDeviceClass.TEMPERATURE,
state_class=SensorStateClass.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=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=FREQUENCY_HERTZ, native_unit_of_measurement=FREQUENCY_HERTZ,
), ),
@ -165,7 +160,7 @@ class InverterSensor(CoordinatorEntity, SensorEntity):
self._attr_icon = _ICONS.get(sensor.kind) self._attr_icon = _ICONS.get(sensor.kind)
# Set the inverter SoC as main device battery sensor # Set the inverter SoC as main device battery sensor
if sensor.id_ == BATTERY_SOC: if sensor.id_ == BATTERY_SOC:
self._attr_device_class = DEVICE_CLASS_BATTERY self._attr_device_class = SensorDeviceClass.BATTERY
self._sensor = sensor self._sensor = sensor
self._previous_value = None self._previous_value = None