diff --git a/custom_components/iotawatt/sensor.py b/custom_components/iotawatt/sensor.py index 04112c2..2c01dae 100644 --- a/custom_components/iotawatt/sensor.py +++ b/custom_components/iotawatt/sensor.py @@ -9,25 +9,20 @@ from iotawattpy.sensor import Sensor from homeassistant.components.sensor import ( + SensorDeviceClass, ATTR_LAST_RESET, - STATE_CLASS_MEASUREMENT, - STATE_CLASS_TOTAL_INCREASING, + SensorStateClass, SensorEntity, SensorEntityDescription, ) from homeassistant.const import ( - DEVICE_CLASS_CURRENT, - DEVICE_CLASS_ENERGY, - DEVICE_CLASS_POWER, - DEVICE_CLASS_POWER_FACTOR, - DEVICE_CLASS_VOLTAGE, - ELECTRIC_CURRENT_AMPERE, - ELECTRIC_POTENTIAL_VOLT, - ENERGY_WATT_HOUR, - FREQUENCY_HERTZ, + UnitOfApparentPower, + UnitOfElectricCurrent, + UnitOfElectricPotential, + UnitOfEnergy, + UnitOfFrequency, PERCENTAGE, - POWER_VOLT_AMPERE, - POWER_WATT, + UnitOfPower, ) from homeassistant.core import callback from homeassistant.helpers import entity, entity_registry, update_coordinator @@ -56,64 +51,64 @@ class IotaWattSensorEntityDescription(SensorEntityDescription): ENTITY_DESCRIPTION_KEY_MAP: dict[str, IotaWattSensorEntityDescription] = { "Amps": IotaWattSensorEntityDescription( "Amps", - native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE, - state_class=STATE_CLASS_MEASUREMENT, - device_class=DEVICE_CLASS_CURRENT, + native_unit_of_measurement=UnitOfElectricCurrent.AMPERE, + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.CURRENT, entity_registry_enabled_default=False, ), "Hz": IotaWattSensorEntityDescription( "Hz", - native_unit_of_measurement=FREQUENCY_HERTZ, - state_class=STATE_CLASS_MEASUREMENT, + native_unit_of_measurement=UnitOfFrequency.HERTZ, + state_class=SensorStateClass.MEASUREMENT, icon="mdi:flash", entity_registry_enabled_default=False, ), "PF": IotaWattSensorEntityDescription( "PF", native_unit_of_measurement=PERCENTAGE, - state_class=STATE_CLASS_MEASUREMENT, - device_class=DEVICE_CLASS_POWER_FACTOR, + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.POWER_FACTOR, value=lambda value: value * 100, entity_registry_enabled_default=False, ), "Watts": IotaWattSensorEntityDescription( "Watts", - native_unit_of_measurement=POWER_WATT, - state_class=STATE_CLASS_MEASUREMENT, - device_class=DEVICE_CLASS_POWER, + native_unit_of_measurement=UnitOfPower.WATT, + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.POWER, ), "WattHours": IotaWattSensorEntityDescription( "WattHours", - native_unit_of_measurement=ENERGY_WATT_HOUR, - state_class=STATE_CLASS_TOTAL_INCREASING, - device_class=DEVICE_CLASS_ENERGY, + native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, + state_class=SensorStateClass.TOTAL_INCREASING, + device_class=SensorDeviceClass.ENERGY, ), "VA": IotaWattSensorEntityDescription( "VA", - native_unit_of_measurement=POWER_VOLT_AMPERE, - state_class=STATE_CLASS_MEASUREMENT, + native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE, + state_class=SensorStateClass.MEASUREMENT, icon="mdi:flash", entity_registry_enabled_default=False, ), "VAR": IotaWattSensorEntityDescription( "VAR", native_unit_of_measurement=VOLT_AMPERE_REACTIVE, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, icon="mdi:flash", entity_registry_enabled_default=False, ), "VARh": IotaWattSensorEntityDescription( "VARh", native_unit_of_measurement=VOLT_AMPERE_REACTIVE_HOURS, - state_class=STATE_CLASS_MEASUREMENT, + state_class=SensorStateClass.MEASUREMENT, icon="mdi:flash", entity_registry_enabled_default=False, ), "Volts": IotaWattSensorEntityDescription( "Volts", - native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT, - state_class=STATE_CLASS_MEASUREMENT, - device_class=DEVICE_CLASS_VOLTAGE, + native_unit_of_measurement=UnitOfElectricPotential.VOLT, + state_class=SensorStateClass.MEASUREMENT, + device_class=SensorDeviceClass.VOLTAGE, entity_registry_enabled_default=False, ), } @@ -247,7 +242,7 @@ def __init__( super().__init__(coordinator, key, entity_description) - self._attr_state_class = STATE_CLASS_TOTAL_INCREASING + self._attr_state_class = SensorStateClass.TOTAL_INCREASING if self._attr_unique_id is not None: self._attr_unique_id += ".accumulated" @@ -260,7 +255,7 @@ def _handle_coordinator_update(self) -> None: self._accumulated_value is not None ), "async_added_to_hass must have been called first" self._accumulated_value += float(self._sensor_data.getValue()) - + super()._handle_coordinator_update() @property