From e14a04df2e90537e4727b91159d5f8468fca33cc Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 28 Jul 2021 23:58:28 -0500 Subject: [PATCH] Add device class energy and last reset to sense (#53667) --- homeassistant/components/sense/sensor.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/homeassistant/components/sense/sensor.py b/homeassistant/components/sense/sensor.py index dd522d012a5..5a352969c3b 100644 --- a/homeassistant/components/sense/sensor.py +++ b/homeassistant/components/sense/sensor.py @@ -1,7 +1,10 @@ """Support for monitoring a Sense energy sensor.""" +import datetime + from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity from homeassistant.const import ( ATTR_ATTRIBUTION, + DEVICE_CLASS_ENERGY, DEVICE_CLASS_POWER, ELECTRIC_POTENTIAL_VOLT, ENERGY_KILO_WATT_HOUR, @@ -9,6 +12,7 @@ from homeassistant.const import ( ) from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect +import homeassistant.util.dt as dt_util from .const import ( ACTIVE_NAME, @@ -218,6 +222,8 @@ class SenseVoltageSensor(SensorEntity): class SenseTrendsSensor(SensorEntity): """Implementation of a Sense energy sensor.""" + _attr_device_class = DEVICE_CLASS_ENERGY + _attr_state_class = STATE_CLASS_MEASUREMENT _attr_unit_of_measurement = ENERGY_KILO_WATT_HOUR _attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION} _attr_icon = ICON @@ -252,6 +258,13 @@ class SenseTrendsSensor(SensorEntity): """Return if entity is available.""" return self._had_any_update and self._coordinator.last_update_success + @property + def last_reset(self) -> datetime.datetime: + """Return the time when the sensor was last reset, if any.""" + if self._sensor_type == "DAY": + return dt_util.start_of_local_day() + return None + @callback def _async_update(self): """Track if we had an update so we do not report zero data."""