From eb59e19c0a0bec23f6559cdad78362aaead8522a Mon Sep 17 00:00:00 2001 From: Stefan Allius <122395479+s-allius@users.noreply.github.com> Date: Sat, 21 Jun 2025 12:18:48 +0200 Subject: [PATCH] Fix Sonar Qube errors and warnings (#464) * replace constructor call with a literal https://sonarcloud.io/project/issues?open=AZeMhhlEyR1Wrs09sNyb&id=s-allius_tsun-gen3-proxy * re-raise cancel error after cleanup https://sonarcloud.io/project/issues?open=AZeMhhltyR1Wrs09sNyc&id=s-allius_tsun-gen3-proxy * remove duplicated line * change send_modbus_cmd into a synchronous function * make send_start_cmd synchronous https://sonarcloud.io/project/issues?open=AZeMhhhyyR1Wrs09sNya&id=s-allius_tsun-gen3-proxy * make more functions synchronous * update changelog --- CHANGELOG.md | 1 + app/src/gen3plus/solarman_v5.py | 6 +++--- app/src/messages.py | 2 +- app/src/modbus_tcp.py | 2 +- app/src/mqtt.py | 14 +++++++------- app/src/server.py | 2 +- app/src/web/i18n.py | 6 +++--- app/tests/test_mqtt.py | 10 +++++----- app/tests/test_solarman.py | 8 ++++---- app/tests/test_solarman_emu.py | 8 ++++---- app/tests/test_talent.py | 4 ++-- 11 files changed, 32 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 75c26ec..168f7c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +- fix some SonarQube warnings - remove unused 32-bit architectures - Babel don't build new po file if only the pot creation-date was changed - Improve Makefile diff --git a/app/src/gen3plus/solarman_v5.py b/app/src/gen3plus/solarman_v5.py index 355890b..76d9297 100755 --- a/app/src/gen3plus/solarman_v5.py +++ b/app/src/gen3plus/solarman_v5.py @@ -341,9 +341,9 @@ class SolarmanV5(SolarmanBase): self.log_lvl.clear() super().close() - async def send_start_cmd(self, snr: int, host: str, - forward: bool, - start_timeout=MB_CLIENT_DATA_UP): + def send_start_cmd(self, snr: int, host: str, + forward: bool, + start_timeout=MB_CLIENT_DATA_UP): self.no_forwarding = True self.establish_inv_emu = forward self.snr = snr diff --git a/app/src/messages.py b/app/src/messages.py index 7c04121..1b50972 100644 --- a/app/src/messages.py +++ b/app/src/messages.py @@ -193,7 +193,7 @@ class Message(ProtocolIfc): return self.mb.build_msg(dev_id, func, addr, val, log_lvl) - async def send_modbus_cmd(self, func, addr, val, log_lvl) -> None: + def send_modbus_cmd(self, func, addr, val, log_lvl) -> None: self._send_modbus_cmd(Modbus.INV_ADDR, func, addr, val, log_lvl) def _send_modbus_scan(self): diff --git a/app/src/modbus_tcp.py b/app/src/modbus_tcp.py index 49786df..f51eef0 100644 --- a/app/src/modbus_tcp.py +++ b/app/src/modbus_tcp.py @@ -66,7 +66,7 @@ class ModbusTcp(): try: async with ModbusConn(host, port) as inverter: stream = inverter.local.stream - await stream.send_start_cmd(snr, host, forward) + stream.send_start_cmd(snr, host, forward) await stream.ifc.loop() logger.info(f'[{stream.node_id}:{stream.conn_no}] ' f'Connection closed - Shutdown: ' diff --git a/app/src/mqtt.py b/app/src/mqtt.py index 886b264..2594512 100755 --- a/app/src/mqtt.py +++ b/app/src/mqtt.py @@ -112,7 +112,7 @@ class Mqtt(metaclass=Singleton): except asyncio.CancelledError: logger_mqtt.debug("MQTT task cancelled") self.__client = None - return + raise except Exception: # self.inc_counter('SW_Exception') # fixme self.ctime = None @@ -151,7 +151,7 @@ class Mqtt(metaclass=Singleton): if self.__cb_mqtt_is_up: await self.__cb_mqtt_is_up() - async def _out_coeff(self, message): + def _out_coeff(self, message): payload = message.payload.decode("UTF-8") try: val = round(float(payload) * 1024/100) @@ -160,9 +160,9 @@ class Mqtt(metaclass=Singleton): 'the range 0..100,' f' got: {payload}') else: - await self._modbus_cmd(message, - Modbus.WRITE_SINGLE_REG, - 0, 0x202c, val) + self._modbus_cmd(message, + Modbus.WRITE_SINGLE_REG, + 0, 0x202c, val) except Exception: pass @@ -182,7 +182,7 @@ class Mqtt(metaclass=Singleton): else: logger_mqtt.warning(f'Node_id: {node_id} not found') - async def _modbus_cmd(self, message, func, params=0, addr=0, val=0): + def _modbus_cmd(self, message, func, params=0, addr=0, val=0): payload = message.payload.decode("UTF-8") for fnc in self.each_inverter(message, "send_modbus_cmd"): res = payload.split(',') @@ -195,7 +195,7 @@ class Mqtt(metaclass=Singleton): elif params == 2: addr = int(res[0], base=16) val = int(res[1]) # lenght - await fnc(func, addr, val, logging.INFO) + fnc(func, addr, val, logging.INFO) async def _at_cmd(self, message): payload = message.payload.decode("UTF-8") diff --git a/app/src/server.py b/app/src/server.py index 60a2fe8..292a2de 100644 --- a/app/src/server.py +++ b/app/src/server.py @@ -60,7 +60,7 @@ class Server(): @app.context_processor def utility_processor(): - return dict(version=self.version) + return {'version': self.version} def parse_args(self, arg_list: list[str] | None): parser = argparse.ArgumentParser() diff --git a/app/src/web/i18n.py b/app/src/web/i18n.py index 3520983..771b063 100644 --- a/app/src/web/i18n.py +++ b/app/src/web/i18n.py @@ -29,9 +29,9 @@ def get_tz(): @web.context_processor def utility_processor(): - return dict(lang=babel_get_locale(), - lang_str=LANGUAGES.get(str(babel_get_locale()), "English"), - languages=LANGUAGES) + return {'lang': babel_get_locale(), + 'lang_str': LANGUAGES.get(str(babel_get_locale()), "English"), + 'languages': LANGUAGES} @web.route('/language/') diff --git a/app/tests/test_mqtt.py b/app/tests/test_mqtt.py index 2266132..623fb01 100755 --- a/app/tests/test_mqtt.py +++ b/app/tests/test_mqtt.py @@ -286,23 +286,23 @@ async def test_mqtt_dispatch(config_mqtt_conn, aiomqtt_mock, spy_modbus_cmd): assert m.ha_restarts == 1 await m.receive(topic= 'tsun/inv_1/rated_load', payload= b'2') - spy.assert_awaited_once_with(Modbus.WRITE_SINGLE_REG, 0x2008, 2, logging.INFO) + spy.assert_called_once_with(Modbus.WRITE_SINGLE_REG, 0x2008, 2, logging.INFO) spy.reset_mock() await m.receive(topic= 'tsun/inv_1/out_coeff', payload= b'100') - spy.assert_awaited_once_with(Modbus.WRITE_SINGLE_REG, 0x202c, 1024, logging.INFO) + spy.assert_called_once_with(Modbus.WRITE_SINGLE_REG, 0x202c, 1024, logging.INFO) spy.reset_mock() await m.receive(topic= 'tsun/inv_1/out_coeff', payload= b'50') - spy.assert_awaited_once_with(Modbus.WRITE_SINGLE_REG, 0x202c, 512, logging.INFO) + spy.assert_called_once_with(Modbus.WRITE_SINGLE_REG, 0x202c, 512, logging.INFO) spy.reset_mock() await m.receive(topic= 'tsun/inv_1/modbus_read_regs', payload= b'0x3000, 10') - spy.assert_awaited_once_with(Modbus.READ_REGS, 0x3000, 10, logging.INFO) + spy.assert_called_once_with(Modbus.READ_REGS, 0x3000, 10, logging.INFO) spy.reset_mock() await m.receive(topic= 'tsun/inv_1/modbus_read_inputs', payload= b'0x3000, 10') - spy.assert_awaited_once_with(Modbus.READ_INPUTS, 0x3000, 10, logging.INFO) + spy.assert_called_once_with(Modbus.READ_INPUTS, 0x3000, 10, logging.INFO) # test dispatching with empty mapping table m.topic_defs.clear() diff --git a/app/tests/test_solarman.py b/app/tests/test_solarman.py index 92874f5..71ad8f7 100755 --- a/app/tests/test_solarman.py +++ b/app/tests/test_solarman.py @@ -1624,7 +1624,7 @@ async def test_msg_build_modbus_req(my_loop, config_tsun_inv1, device_ind_msg, d assert m.ifc.tx_fifo.get()==device_rsp_msg assert m.ifc.fwd_fifo.get()==device_ind_msg - await m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) + m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) assert 0 == m.send_msg_ofs assert m.ifc.fwd_fifo.get() == b'' assert m.sent_pdu == b'' # modbus command must be ignore, cause connection is still not up @@ -1642,7 +1642,7 @@ async def test_msg_build_modbus_req(my_loop, config_tsun_inv1, device_ind_msg, d assert m.ifc.tx_fifo.get()==inverter_rsp_msg assert m.ifc.fwd_fifo.get()==inverter_ind_msg - await m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) + m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) assert 0 == m.send_msg_ofs assert m.ifc.fwd_fifo.get() == b'' assert m.sent_pdu == msg_modbus_cmd @@ -2318,7 +2318,7 @@ async def test_start_client_mode(my_loop, config_tsun_inv1, str_test_ip): assert m.no_forwarding == False assert m.mb_timer.tim == None assert asyncio.get_running_loop() == m.mb_timer.loop - await m.send_start_cmd(get_sn_int(), str_test_ip, False, m.mb_first_timeout) + m.send_start_cmd(get_sn_int(), str_test_ip, False, m.mb_first_timeout) assert m.sent_pdu==bytearray(b'\xa5\x17\x00\x10E\x01\x00!Ce{\x02\xb0\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x030\x00\x000J\xde\xf1\x15') assert m.db.get_db_value(Register.IP_ADDRESS) == str_test_ip assert isclose(m.db.get_db_value(Register.POLLING_INTERVAL), 0.5) @@ -2351,7 +2351,7 @@ async def test_start_client_mode_scan(config_tsun_scan_dcu, str_test_ip, dcu_mod assert m.no_forwarding == False assert m.mb_timer.tim == None assert asyncio.get_running_loop() == m.mb_timer.loop - await m.send_start_cmd(get_dcu_sn_int(), str_test_ip, False, m.mb_first_timeout) + m.send_start_cmd(get_dcu_sn_int(), str_test_ip, False, m.mb_first_timeout) assert m.mb_start_reg == 0x0000 assert m.mb_step == 0x100 assert m.mb_bytes == 0x2d diff --git a/app/tests/test_solarman_emu.py b/app/tests/test_solarman_emu.py index a3d517c..d511f22 100644 --- a/app/tests/test_solarman_emu.py +++ b/app/tests/test_solarman_emu.py @@ -144,7 +144,7 @@ async def test_emu_start(my_loop, config_tsun_inv1, msg_modbus_rsp, str_test_ip, inv = InvStream(msg_modbus_rsp) assert asyncio.get_running_loop() == inv.mb_timer.loop - await inv.send_start_cmd(get_sn_int(), str_test_ip, True, inv.mb_first_timeout) + inv.send_start_cmd(get_sn_int(), str_test_ip, True, inv.mb_first_timeout) inv.read() # read complete msg, and dispatch msg assert not inv.header_valid # must be invalid, since msg was handled and buffer flushed assert inv.msg_count == 1 @@ -161,7 +161,7 @@ async def test_snd_hb(my_loop, config_tsun_inv1, heartbeat_ind): inv = InvStream() cld = CldStream(inv) - # await inv.send_start_cmd(get_sn_int(), str_test_ip, False, inv.mb_first_timeout) + # inv.send_start_cmd(get_sn_int(), str_test_ip, False, inv.mb_first_timeout) cld.send_heartbeat_cb(0) assert cld.ifc.tx_fifo.peek() == heartbeat_ind cld.close() @@ -178,7 +178,7 @@ async def test_snd_inv_data(my_loop, config_tsun_inv1, inverter_ind_msg, inverte inv.db.set_db_def_value(Register.GRID_FREQUENCY, 50.05) inv.db.set_db_def_value(Register.PROD_COMPL_TYPE, 6) assert asyncio.get_running_loop() == inv.mb_timer.loop - await inv.send_start_cmd(get_sn_int(), str_test_ip, False, inv.mb_first_timeout) + inv.send_start_cmd(get_sn_int(), str_test_ip, False, inv.mb_first_timeout) inv.db.set_db_def_value(Register.DATA_UP_INTERVAL, 17) # set test value cld = CldStream(inv) @@ -213,7 +213,7 @@ async def test_rcv_invalid(my_loop, config_tsun_inv1, inverter_ind_msg, inverter _ = config_tsun_inv1 inv = InvStream() assert asyncio.get_running_loop() == inv.mb_timer.loop - await inv.send_start_cmd(get_sn_int(), str_test_ip, False, inv.mb_first_timeout) + inv.send_start_cmd(get_sn_int(), str_test_ip, False, inv.mb_first_timeout) inv.db.set_db_def_value(Register.DATA_UP_INTERVAL, 17) # set test value cld = CldStream(inv) diff --git a/app/tests/test_talent.py b/app/tests/test_talent.py index fa42eed..01ef0fe 100644 --- a/app/tests/test_talent.py +++ b/app/tests/test_talent.py @@ -2411,14 +2411,14 @@ async def test_msg_build_modbus_req(config_tsun_inv1, msg_modbus_cmd): _ = config_tsun_inv1 m = MemoryStream(b'', (0,), True) m.id_str = b"R170000000000001" - await m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) + m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) assert 0 == m.send_msg_ofs assert m.ifc.fwd_fifo.get() == b'' assert m.ifc.tx_fifo.get() == b'' assert m.sent_pdu == b'' m.state = State.up - await m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) + m.send_modbus_cmd(Modbus.WRITE_SINGLE_REG, 0x2008, 0, logging.DEBUG) assert 0 == m.send_msg_ofs assert m.ifc.fwd_fifo.get() == b'' assert m.ifc.tx_fifo.get() == b''