From 19d77b4f3f80889bdfd2970708536028faff80fd Mon Sep 17 00:00:00 2001 From: Alex Ruddick Date: Wed, 22 May 2024 16:30:33 -0500 Subject: [PATCH] remove code to set X address (digital inputs can't be set) --- clickplc/driver.py | 28 ---------------------------- clickplc/tests/test_driver.py | 29 ++++++++++++++++------------- 2 files changed, 16 insertions(+), 41 deletions(-) diff --git a/clickplc/driver.py b/clickplc/driver.py index 701f755..484f39f 100644 --- a/clickplc/driver.py +++ b/clickplc/driver.py @@ -367,34 +367,6 @@ async def _get_ctd(self, start: int, end: int) -> dict: return decoder.decode_32bit_int() return {f'ctd{n}': decoder.decode_32bit_int() for n in range(start, end + 1)} - async def _set_x(self, start: int, data: list[bool] | bool): - """Set X addresses. Called by `set`. - - For more information on the quirks of X coils, read the `_get_x` - docstring. - """ - if start % 100 == 0 or start % 100 > 16: - raise ValueError('X start address must be *01-*16.') - if start < 1 or start > 816: - raise ValueError('X start address must be in [001, 816].') - coil = 32 * (start // 100) + start % 100 - 1 - - if isinstance(data, list): - if len(data) > 16 * (9 - start // 100) - start % 100: - raise ValueError('Data list longer than available addresses.') - payload = [] - if (start % 100) + len(data) > 16: - i = 17 - (start % 100) - payload += data[:i] + [False] * 16 - data = data[i:] - while len(data) > 16: - payload += data[:16] + [False] * 16 - data = data[16:] - payload += data - await self.write_coils(coil, payload) - else: - await self.write_coil(coil, data) - async def _set_y(self, start: int, data: list[bool] | bool): """Set Y addresses. Called by `set`. diff --git a/clickplc/tests/test_driver.py b/clickplc/tests/test_driver.py index 225d30e..2ca9157 100644 --- a/clickplc/tests/test_driver.py +++ b/clickplc/tests/test_driver.py @@ -87,14 +87,13 @@ async def test_tagged_driver(tagged_driver, expected_tags): @pytest.mark.asyncio -@pytest.mark.parametrize('prefix', ['x', 'y']) -async def test_bool_roundtrip(plc_driver, prefix): - """Confirm x and y bools are read back correctly after being set.""" - await plc_driver.set(f'{prefix}2', True) - await plc_driver.set(f'{prefix}3', [False, True]) - expected = {f'{prefix}001': False, f'{prefix}002': True, f'{prefix}003': False, - f'{prefix}004': True, f'{prefix}005': False} - assert expected == await plc_driver.get(f'{prefix}1-{prefix}5') +async def test_y_roundtrip(plc_driver): + """Confirm y (output bools) are read back correctly after being set.""" + await plc_driver.set('y2', True) + await plc_driver.set('y3', [False, True]) + expected = {'y001': False, 'y002': True, 'y003': False, + 'y004': True, 'y005': False} + assert expected == await plc_driver.get('y1-y5') @pytest.mark.asyncio @@ -146,8 +145,8 @@ async def test_set_error_handling(plc_driver): @pytest.mark.asyncio @pytest.mark.parametrize('prefix', ['x', 'y']) -async def test_xy_error_handling(plc_driver, prefix): - """Ensure errors are handled for invalid requests of x and y registers.""" +async def test_get_xy_error_handling(plc_driver, prefix): + """Ensure errors are handled for invalid get requests of x and y registers.""" with pytest.raises(ValueError, match=r'address must be \*01-\*16.'): await plc_driver.get(f'{prefix}17') with pytest.raises(ValueError, match=r'address must be in \[001, 816\].'): @@ -156,12 +155,16 @@ async def test_xy_error_handling(plc_driver, prefix): await plc_driver.get(f'{prefix}1-{prefix}17') with pytest.raises(ValueError, match=r'address must be in \[001, 816\].'): await plc_driver.get(f'{prefix}1-{prefix}1001') + +@pytest.mark.asyncio +async def test_set_y_error_handling(plc_driver): + """Ensure errors are handled for invalid set requests of y registers.""" with pytest.raises(ValueError, match=r'address must be \*01-\*16.'): - await plc_driver.set(f'{prefix}17', True) + await plc_driver.set('y17', True) with pytest.raises(ValueError, match=r'address must be in \[001, 816\].'): - await plc_driver.set(f'{prefix}1001', True) + await plc_driver.set('y1001', True) with pytest.raises(ValueError, match=r'Data list longer than available addresses.'): - await plc_driver.set(f'{prefix}816', [True, True]) + await plc_driver.set('y816', [True, True]) @pytest.mark.asyncio