Remove last_reset attribute and set state class to total_increasing for smartthings energy sensors (#54824)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Erik Montnemery 2021-08-18 17:14:02 +02:00 committed by GitHub
parent e98d50f6d1
commit a6ac55390a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,12 +3,15 @@ from __future__ import annotations
from collections import namedtuple from collections import namedtuple
from collections.abc import Sequence from collections.abc import Sequence
from datetime import datetime
from pysmartthings import Attribute, Capability from pysmartthings import Attribute, Capability
from pysmartthings.device import DeviceEntity from pysmartthings.device import DeviceEntity
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
SensorEntity,
)
from homeassistant.const import ( from homeassistant.const import (
AREA_SQUARE_METERS, AREA_SQUARE_METERS,
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
@ -33,7 +36,6 @@ from homeassistant.const import (
TEMP_FAHRENHEIT, TEMP_FAHRENHEIT,
VOLUME_CUBIC_METERS, VOLUME_CUBIC_METERS,
) )
from homeassistant.util.dt import utc_from_timestamp
from . import SmartThingsEntity from . import SmartThingsEntity
from .const import DATA_BROKERS, DOMAIN from .const import DATA_BROKERS, DOMAIN
@ -133,7 +135,7 @@ CAPABILITY_TO_SENSORS = {
"Energy Meter", "Energy Meter",
ENERGY_KILO_WATT_HOUR, ENERGY_KILO_WATT_HOUR,
DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY,
STATE_CLASS_MEASUREMENT, STATE_CLASS_TOTAL_INCREASING,
) )
], ],
Capability.equivalent_carbon_dioxide_measurement: [ Capability.equivalent_carbon_dioxide_measurement: [
@ -507,13 +509,6 @@ class SmartThingsSensor(SmartThingsEntity, SensorEntity):
unit = self._device.status.attributes[self._attribute].unit unit = self._device.status.attributes[self._attribute].unit
return UNITS.get(unit, unit) if unit else self._default_unit return UNITS.get(unit, unit) if unit else self._default_unit
@property
def last_reset(self) -> datetime | None:
"""Return the time when the sensor was last reset, if any."""
if self._attribute == Attribute.energy:
return utc_from_timestamp(0)
return None
class SmartThingsThreeAxisSensor(SmartThingsEntity, SensorEntity): class SmartThingsThreeAxisSensor(SmartThingsEntity, SensorEntity):
"""Define a SmartThings Three Axis Sensor.""" """Define a SmartThings Three Axis Sensor."""
@ -554,8 +549,9 @@ class SmartThingsPowerConsumptionSensor(SmartThingsEntity, SensorEntity):
"""Init the class.""" """Init the class."""
super().__init__(device) super().__init__(device)
self.report_name = report_name self.report_name = report_name
# This is an exception for STATE_CLASS_MEASUREMENT per @balloob
self._attr_state_class = STATE_CLASS_MEASUREMENT self._attr_state_class = STATE_CLASS_MEASUREMENT
if self.report_name != "power":
self._attr_state_class = STATE_CLASS_TOTAL_INCREASING
@property @property
def name(self) -> str: def name(self) -> str:
@ -590,10 +586,3 @@ class SmartThingsPowerConsumptionSensor(SmartThingsEntity, SensorEntity):
if self.report_name == "power": if self.report_name == "power":
return POWER_WATT return POWER_WATT
return ENERGY_KILO_WATT_HOUR return ENERGY_KILO_WATT_HOUR
@property
def last_reset(self) -> datetime | None:
"""Return the time when the sensor was last reset, if any."""
if self.report_name != "power":
return utc_from_timestamp(0)
return None