Filter out zero readings for DSMR enery sensors (#104843)
This commit is contained in:
parent
262e59f293
commit
d67d2d9566
2 changed files with 20 additions and 3 deletions
|
@ -799,6 +799,10 @@ class DSMREntity(SensorEntity):
|
||||||
float(value), self._entry.data.get(CONF_PRECISION, DEFAULT_PRECISION)
|
float(value), self._entry.data.get(CONF_PRECISION, DEFAULT_PRECISION)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Make sure we do not return a zero value for an energy sensor
|
||||||
|
if not value and self.state_class == SensorStateClass.TOTAL_INCREASING:
|
||||||
|
return None
|
||||||
|
|
||||||
return value
|
return value
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
@ -10,6 +10,8 @@ from decimal import Decimal
|
||||||
from itertools import chain, repeat
|
from itertools import chain, repeat
|
||||||
from unittest.mock import DEFAULT, MagicMock
|
from unittest.mock import DEFAULT, MagicMock
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
ATTR_OPTIONS,
|
ATTR_OPTIONS,
|
||||||
|
@ -22,6 +24,7 @@ from homeassistant.const import (
|
||||||
ATTR_FRIENDLY_NAME,
|
ATTR_FRIENDLY_NAME,
|
||||||
ATTR_ICON,
|
ATTR_ICON,
|
||||||
ATTR_UNIT_OF_MEASUREMENT,
|
ATTR_UNIT_OF_MEASUREMENT,
|
||||||
|
STATE_UNKNOWN,
|
||||||
UnitOfEnergy,
|
UnitOfEnergy,
|
||||||
UnitOfPower,
|
UnitOfPower,
|
||||||
UnitOfVolume,
|
UnitOfVolume,
|
||||||
|
@ -308,7 +311,17 @@ async def test_v4_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_v5_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
|
@pytest.mark.parametrize(
|
||||||
|
("value", "state"),
|
||||||
|
[
|
||||||
|
(Decimal(745.690), "745.69"),
|
||||||
|
(Decimal(745.695), "745.695"),
|
||||||
|
(Decimal(0.000), STATE_UNKNOWN),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
async def test_v5_meter(
|
||||||
|
hass: HomeAssistant, dsmr_connection_fixture, value: Decimal, state: str
|
||||||
|
) -> None:
|
||||||
"""Test if v5 meter is correctly parsed."""
|
"""Test if v5 meter is correctly parsed."""
|
||||||
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
(connection_factory, transport, protocol) = dsmr_connection_fixture
|
||||||
|
|
||||||
|
@ -335,7 +348,7 @@ async def test_v5_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
|
||||||
HOURLY_GAS_METER_READING,
|
HOURLY_GAS_METER_READING,
|
||||||
[
|
[
|
||||||
{"value": datetime.datetime.fromtimestamp(1551642213)},
|
{"value": datetime.datetime.fromtimestamp(1551642213)},
|
||||||
{"value": Decimal(745.695), "unit": "m3"},
|
{"value": value, "unit": "m3"},
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
ELECTRICITY_ACTIVE_TARIFF: CosemObject(
|
ELECTRICITY_ACTIVE_TARIFF: CosemObject(
|
||||||
|
@ -371,7 +384,7 @@ async def test_v5_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
|
||||||
|
|
||||||
# check if gas consumption is parsed correctly
|
# check if gas consumption is parsed correctly
|
||||||
gas_consumption = hass.states.get("sensor.gas_meter_gas_consumption")
|
gas_consumption = hass.states.get("sensor.gas_meter_gas_consumption")
|
||||||
assert gas_consumption.state == "745.695"
|
assert gas_consumption.state == state
|
||||||
assert gas_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS
|
assert gas_consumption.attributes.get(ATTR_DEVICE_CLASS) == SensorDeviceClass.GAS
|
||||||
assert (
|
assert (
|
||||||
gas_consumption.attributes.get(ATTR_STATE_CLASS)
|
gas_consumption.attributes.get(ATTR_STATE_CLASS)
|
||||||
|
|
Loading…
Add table
Reference in a new issue