Skip to content

Commit

Permalink
Fix rounding error for values just under a whole number (#31)
Browse files Browse the repository at this point in the history
The float formatting function is getting a little ridiculous. Maybe it
could be optimized to avoid bugs like this.
  • Loading branch information
cottsay authored Mar 28, 2024
1 parent f913115 commit 0d1a9b4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions aioraven/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ def convert_float_formatted(
difference = len(frac) - places
if difference > 0:
frac = str((int(frac) + 5 ** difference) // 10 ** difference)
if len(frac) > places:
whole = str(int(whole) + 1)
frac = frac[1:]
elif difference < 0:
frac += '0' * (-difference)
if digits_left is not None:
Expand Down
35 changes: 35 additions & 0 deletions test/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,41 @@ async def test_get_current_summation_delivered(meter):
summation_received='0016.00')


@pytest.mark.asyncio
async def test_get_current_summation_delivered_rounding():
"""
Verify rounding behavior of the ``get_current_summation_delivered``
command.
"""
responses = {
b'<Command><Name>get_current_summation_delivered</Name></Command>':
b'<CurrentSummationDelivered>'
b' <DeviceMacId>0x0123456789ABCDEF</DeviceMacId>'
b' <MeterMacId>0xFEDCBA9876543210</MeterMacId>'
b' <TimeStamp>0x29bd58a7</TimeStamp>'
b' <SummationDelivered>0x00000C7B</SummationDelivered>'
b' <SummationReceived>0x00000644</SummationReceived>'
b' <Multiplier>0x00000002</Multiplier>'
b' <Divisor>0x000000C8</Divisor>'
b' <DigitsRight>0x01</DigitsRight>'
b' <DigitsLeft>0x03</DigitsLeft>'
b' <SuppressLeadingZero>N</SuppressLeadingZero>'
b'</CurrentSummationDelivered>',
}

async with mock_device(responses) as (host, port):
async with RAVEnNetworkDevice(host, port) as dut:
actual = await dut.get_current_summation_delivered()

assert actual == CurrentSummationDelivered(
device_mac_id=bytes.fromhex('0123456789ABCDEF'),
meter_mac_id=bytes.fromhex('FEDCBA9876543210'),
time_stamp=datetime(
2022, 3, 11, 0, 47, 35, tzinfo=timezone.utc),
summation_delivered='032.0',
summation_received='016.0')


@pytest.mark.asyncio
async def test_get_current_summation_delivered_no_received():
"""
Expand Down

0 comments on commit 0d1a9b4

Please sign in to comment.