Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out zero readings for DSMR enery sensors #104843

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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