Utility meter, add STATE_CLASS_TOTAL_INCREASING ()

* Utility meter, STATE_CLASS_TOTAL_INCREASING

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* update test

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* update test

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
pull/55209/head
Daniel Hjelseth Høyer 2021-08-25 14:03:30 +02:00 committed by GitHub
parent f4fbc083e6
commit 7dd169b48e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions
homeassistant/components/utility_meter
tests/components/utility_meter

View File

@ -5,7 +5,11 @@ import logging
import voluptuous as vol
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 (
ATTR_UNIT_OF_MEASUREMENT,
CONF_NAME,
@ -330,7 +334,11 @@ class UtilityMeterSensor(RestoreEntity, SensorEntity):
@property
def state_class(self):
"""Return the device class of the sensor."""
return STATE_CLASS_MEASUREMENT
return (
STATE_CLASS_MEASUREMENT
if self._sensor_net_consumption
else STATE_CLASS_TOTAL_INCREASING
)
@property
def native_unit_of_measurement(self):

View File

@ -3,7 +3,11 @@ from contextlib import contextmanager
from datetime import timedelta
from unittest.mock import patch
from homeassistant.components.sensor import ATTR_STATE_CLASS, STATE_CLASS_MEASUREMENT
from homeassistant.components.sensor import (
ATTR_STATE_CLASS,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_TOTAL_INCREASING,
)
from homeassistant.components.utility_meter.const import (
ATTR_TARIFF,
ATTR_VALUE,
@ -165,6 +169,7 @@ async def test_device_class(hass):
"utility_meter": {
"energy_meter": {
"source": "sensor.energy",
"net_consumption": True,
},
"gas_meter": {
"source": "sensor.gas",
@ -197,7 +202,7 @@ async def test_device_class(hass):
assert state is not None
assert state.state == "0"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) is None
hass.states.async_set(
@ -219,7 +224,7 @@ async def test_device_class(hass):
assert state is not None
assert state.state == "1"
assert state.attributes.get(ATTR_DEVICE_CLASS) is None
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_MEASUREMENT
assert state.attributes.get(ATTR_STATE_CLASS) == STATE_CLASS_TOTAL_INCREASING
assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "some_archaic_unit"