Skip to content

Commit

Permalink
Filter out zero readings for DSMR enery sensors (#104843)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed Nov 30, 2023
1 parent 00e57ab commit 7767bb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 4 additions & 0 deletions homeassistant/components/dsmr/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,10 @@ def native_value(self) -> StateType:
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

@staticmethod
Expand Down
19 changes: 16 additions & 3 deletions tests/components/dsmr/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from itertools import chain, repeat
from unittest.mock import DEFAULT, MagicMock

import pytest

from homeassistant import config_entries
from homeassistant.components.sensor import (
ATTR_OPTIONS,
Expand All @@ -22,6 +24,7 @@
ATTR_FRIENDLY_NAME,
ATTR_ICON,
ATTR_UNIT_OF_MEASUREMENT,
STATE_UNKNOWN,
UnitOfEnergy,
UnitOfPower,
UnitOfVolume,
Expand Down Expand Up @@ -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."""
(connection_factory, transport, protocol) = dsmr_connection_fixture

Expand All @@ -335,7 +348,7 @@ async def test_v5_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:
HOURLY_GAS_METER_READING,
[
{"value": datetime.datetime.fromtimestamp(1551642213)},
{"value": Decimal(745.695), "unit": "m3"},
{"value": value, "unit": "m3"},
],
),
ELECTRICITY_ACTIVE_TARIFF: CosemObject(
Expand Down Expand Up @@ -371,7 +384,7 @@ async def test_v5_meter(hass: HomeAssistant, dsmr_connection_fixture) -> None:

# check if gas consumption is parsed correctly
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_STATE_CLASS)
Expand Down

0 comments on commit 7767bb3

Please sign in to comment.