|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2023 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""Tests for fetching and streaming the phase-to-neutral voltage.""" |
| 5 | + |
| 6 | + |
| 7 | +import asyncio |
| 8 | + |
| 9 | +from pytest_mock import MockerFixture |
| 10 | + |
| 11 | +from frequenz.sdk import microgrid |
| 12 | + |
| 13 | +from .mock_microgrid import MockMicrogrid |
| 14 | + |
| 15 | +# pylint: disable=protected-access |
| 16 | + |
| 17 | + |
| 18 | +async def test_voltage_1(mocker: MockerFixture) -> None: |
| 19 | + """Test the phase-to-neutral voltage with a grid side meter.""" |
| 20 | + mockgrid = MockMicrogrid(grid_meter=True) |
| 21 | + mockgrid.add_batteries(1, no_meter=True) |
| 22 | + mockgrid.add_batteries(1, no_meter=False) |
| 23 | + await mockgrid.start(mocker) |
| 24 | + |
| 25 | + voltage = microgrid.voltage() |
| 26 | + voltage_recv = voltage.new_receiver() |
| 27 | + |
| 28 | + assert voltage._task is not None |
| 29 | + # Wait for voltage requests to be sent, one request per phase. |
| 30 | + await asyncio.sleep(0) |
| 31 | + await asyncio.sleep(0) |
| 32 | + await asyncio.sleep(0) |
| 33 | + |
| 34 | + for count in range(10): |
| 35 | + volt_delta = 1 if count % 2 == 0 else -1 |
| 36 | + volt_phases: list[float | None] = [ |
| 37 | + 220.0 * volt_delta, |
| 38 | + 219.8 * volt_delta, |
| 39 | + 220.2 * volt_delta, |
| 40 | + ] |
| 41 | + |
| 42 | + await mockgrid.mock_resampler.send_meter_voltage([volt_phases, volt_phases]) |
| 43 | + |
| 44 | + val = await voltage_recv.receive() |
| 45 | + assert val is not None |
| 46 | + assert val.value_p1 and val.value_p2 and val.value_p3 |
| 47 | + assert val.value_p1.as_volts() == volt_phases[0] |
| 48 | + assert val.value_p2.as_volts() == volt_phases[1] |
| 49 | + assert val.value_p3.as_volts() == volt_phases[2] |
| 50 | + |
| 51 | + await mockgrid.cleanup() |
| 52 | + |
| 53 | + |
| 54 | +async def test_voltage_2(mocker: MockerFixture) -> None: |
| 55 | + """Test the phase-to-neutral voltage without a grid side meter.""" |
| 56 | + mockgrid = MockMicrogrid(grid_meter=False) |
| 57 | + mockgrid.add_batteries(1, no_meter=False) |
| 58 | + mockgrid.add_batteries(1, no_meter=True) |
| 59 | + await mockgrid.start(mocker) |
| 60 | + |
| 61 | + voltage = microgrid.voltage() |
| 62 | + voltage_recv = voltage.new_receiver() |
| 63 | + |
| 64 | + assert voltage._task is not None |
| 65 | + # Wait for voltage requests to be sent, one request per phase. |
| 66 | + await asyncio.sleep(0) |
| 67 | + await asyncio.sleep(0) |
| 68 | + await asyncio.sleep(0) |
| 69 | + |
| 70 | + for count in range(10): |
| 71 | + volt_delta = 1 if count % 2 == 0 else -1 |
| 72 | + volt_phases: list[float | None] = [ |
| 73 | + 220.0 * volt_delta, |
| 74 | + 219.8 * volt_delta, |
| 75 | + 220.2 * volt_delta, |
| 76 | + ] |
| 77 | + |
| 78 | + await mockgrid.mock_resampler.send_meter_voltage([volt_phases]) |
| 79 | + |
| 80 | + val = await voltage_recv.receive() |
| 81 | + assert val is not None |
| 82 | + assert val.value_p1 and val.value_p2 and val.value_p3 |
| 83 | + assert val.value_p1.as_volts() == volt_phases[0] |
| 84 | + assert val.value_p2.as_volts() == volt_phases[1] |
| 85 | + assert val.value_p3.as_volts() == volt_phases[2] |
| 86 | + |
| 87 | + await mockgrid.cleanup() |
| 88 | + |
| 89 | + |
| 90 | +async def test_voltage_3(mocker: MockerFixture) -> None: |
| 91 | + """Test the phase-to-neutral voltage with None values.""" |
| 92 | + mockgrid = MockMicrogrid(grid_meter=True) |
| 93 | + mockgrid.add_batteries(2, no_meter=False) |
| 94 | + await mockgrid.start(mocker) |
| 95 | + |
| 96 | + voltage = microgrid.voltage() |
| 97 | + voltage_recv = voltage.new_receiver() |
| 98 | + |
| 99 | + assert voltage._task is not None |
| 100 | + # Wait for voltage requests to be sent, one request per phase. |
| 101 | + await asyncio.sleep(0) |
| 102 | + await asyncio.sleep(0) |
| 103 | + await asyncio.sleep(0) |
| 104 | + |
| 105 | + for count in range(10): |
| 106 | + volt_delta = 1 if count % 2 == 0 else -1 |
| 107 | + volt_phases: list[float | None] = [ |
| 108 | + 220.0 * volt_delta, |
| 109 | + 219.8 * volt_delta, |
| 110 | + 220.2 * volt_delta, |
| 111 | + ] |
| 112 | + |
| 113 | + await mockgrid.mock_resampler.send_meter_voltage( |
| 114 | + [volt_phases, [None, None, None], [None, 219.8, 220.2]] |
| 115 | + ) |
| 116 | + |
| 117 | + val = await voltage_recv.receive() |
| 118 | + assert val is not None |
| 119 | + assert val.value_p1 and val.value_p2 and val.value_p3 |
| 120 | + assert val.value_p1.as_volts() == volt_phases[0] |
| 121 | + assert val.value_p2.as_volts() == volt_phases[1] |
| 122 | + assert val.value_p3.as_volts() == volt_phases[2] |
| 123 | + |
| 124 | + await mockgrid.cleanup() |
0 commit comments