fix derived rate, fixes #20097 (#21620)

* fix derived rate, fixes #20097

* fix derived rate, fixes #20097

* Fix typo

thnx @amelchio

* Make the test more realistic

Took values from my own smart meter for the test

* Update test to ignore rounding issues
pull/21785/head
Willem Burgers 2019-03-03 23:42:52 +01:00 committed by Anders Melchiorsen
parent 818776d2b4
commit 2017e45d78
2 changed files with 8 additions and 7 deletions

View File

@ -350,7 +350,8 @@ class DerivativeDSMREntity(DSMREntity):
else:
# Recalculate the rate
diff = current_reading - self._previous_reading
self._state = diff
timediff = timestamp - self._previous_timestamp
self._state = diff / timediff * 3600
self._previous_reading = current_reading
self._previous_timestamp = timestamp

View File

@ -104,8 +104,8 @@ def test_derivative():
entity.telegram = {
'1.0.0': MBusObject([
{'value': 1},
{'value': 1, 'unit': 'm3'},
{'value': 1551642213},
{'value': 745.695, 'unit': 'm3'},
])
}
yield from entity.async_update()
@ -115,14 +115,14 @@ def test_derivative():
entity.telegram = {
'1.0.0': MBusObject([
{'value': 2},
{'value': 2, 'unit': 'm3'},
{'value': 1551642543},
{'value': 745.698, 'unit': 'm3'},
])
}
yield from entity.async_update()
assert entity.state == 1, \
'state should be difference between first and second update'
assert abs(entity.state - 0.03272) < 0.00001, \
'state should be hourly usage calculated from first and second update'
assert entity.unit_of_measurement == 'm3/h'