diff --git a/app/proxy.svg b/app/proxy.svg index 6493fc7..a8e328d 100644 --- a/app/proxy.svg +++ b/app/proxy.svg @@ -4,193 +4,257 @@ - - + + G - + A0 - - - -You can stick notes -on diagrams too! + + + +You can stick notes +on diagrams too! A1 - -IterRegistry - - -__iter__ - - - -A3 - -Inverter - -cls.db_stat -cls.entity_prfx -cls.discovery_prfx -cls.proxy_node_id -cls.proxy_unique_id -cls.mqtt:Mqtt -__ha_restarts - -async_create_remote(inv_prot, conn_class)async_publ_mqtt() - - - -A1->A3 - - - - - -A2 - -Mqtt -<<Singleton>> - -<static>ha_restarts -<static>__client -<static>__cb_MqttIsUp - -<async>publish() -<async>close() - - - -A3->A2 - - - + +<<AbstractIterMeta>> + + +__iter__ A4 - -InverterBase - - -<async>disc(shutdown_started) -<async>async_create_remote() -healthy() + +<<InverterIfc>> + + +healthy()->bool +<async>disc(shutdown_started=False) +<async>create_remote() - + -A3->A4 - - +A1->A4 + + + + + +A2 + +Mqtt +<<Singleton>> + +<static>ha_restarts +<static>__client +<static>__cb_MqttIsUp + +<async>publish() +<async>close() + + + +A3 + +Proxy + +<cls>db_stat +<cls>entity_prfx +<cls>discovery_prfx +<cls>proxy_node_id +<cls>proxy_unique_id +<cls>mqtt:Mqtt +__ha_restarts + +class_init() +class_close() +<async>_cb_mqtt_is_up() +<async>_register_proxy_stat_home_assistant() +<async>_async_publ_mqtt_proxy_stat(key) + + + +A3->A2 + + + A5 - -InverterG3 - -addr -remote:StreamPtr -local:StreamPtr - -async_create_remote() -close() + +InverterBase + +_registry +__ha_restarts +addr +config_id:str +prot_class:MessageProt +remote:StreamPtr +local:StreamPtr + +healthy()->bool +<async>disc(shutdown_started=False) +<async>create_remote() +<async>async_publ_mqtt() + + + +A3->A5 + + A4->A5 - - + + A6 - -InverterG3P - -addr -remote:StreamPtr -local:StreamPtr - -async_create_remote() -close() + +StreamPtr + + +stream:MessageProt +ifc:AsyncIfc - - -A4->A6 - - + + +A5->A6 + + +2 A7 - -<<AsyncIfc>> + +InverterG3 - + A5->A7 - - -1..2 - - - -A8 - -<<MessageProt>> - - - -A5->A8 - - -1..2 - - - -A6->A7 - - -1..2 - - - -A6->A8 - - -1..2 - - - -A8->A7 - - -use + + A9 - -ModbusConn - -host -port -addr -stream:InverterG3P - + +InverterG3P - + + +A5->A9 + + + + + +A11 + +<<AsyncIfc>> + + -A9->A6 - - -1 -has +A6->A11 + + +1 + + + +A12 + +<<MessageProt>> + + + +A6->A12 + + +1 + + + +A8 + + + +Creates an GEN3 +inverter instance +with +prot_class:Talent + + + +A7->A8 + + + + +A10 + + + +Creates an GEN3PLUS +inverter instance +with +prot_class:SolarmanV5 + + + +A9->A10 + + + + +A12->A11 + + +use + + + +A13 + +ModbusConn + +host +port +addr +stream:InverterG3P + + + + +A13->A9 + + +1 +has + + + +A14 + +ModbusTcp + + + +A14->A13 + + +* +creates diff --git a/app/proxy.yuml b/app/proxy.yuml index daf04aa..9d007cb 100644 --- a/app/proxy.yuml +++ b/app/proxy.yuml @@ -3,29 +3,34 @@ // {generate:true} [note: You can stick notes on diagrams too!{bg:cornsilk}] -[IterRegistry||__iter__] +[<>||__iter__] [Mqtt;<>|ha_restarts;__client;__cb_MqttIsUp|publish();close()] -[Inverter|cls.db_stat;cls.entity_prfx;cls.discovery_prfx;cls.proxy_node_id;cls.proxy_unique_id;cls.mqtt:Mqtt;;__ha_restarts|async_create_remote(inv_prot, conn_class)async_publ_mqtt()] -[InverterBase||disc(shutdown_started);async_create_remote();healthy()] -[Inverter]^[InverterBase] -[InverterBase]^[InverterG3|addr;remote:StreamPtr;local:StreamPtr|async_create_remote();;close()] -[InverterBase]^[InverterG3P|addr;remote:StreamPtr;local:StreamPtr|async_create_remote();;close()] -[Inverter]++->[Mqtt;<>] +[Proxy|db_stat;entity_prfx;discovery_prfx;proxy_node_id;proxy_unique_id;mqtt:Mqtt;;__ha_restarts|class_init();class_close();;_cb_mqtt_is_up();_register_proxy_stat_home_assistant();_async_publ_mqtt_proxy_stat(key)] -[IterRegistry]^[Inverter|server_side:bool;header_valid:bool;header_len:unsigned;data_len:unsigned;unique_id;node_id;sug_area;_recv_buffer:bytearray;_send_buffer:bytearray;_forward_buffer:bytearray;db:Infos;new_data:list;state|_read():void;close():void;inc_counter():void;dec_counter():void] +[<>||healthy()->bool;disc(shutdown_started=False);create_remote();] +[<>]^-.-[<>] +[InverterBase|_registry;__ha_restarts;;addr;config_id:str;prot_class:MessageProt;remote:StreamPtr;local:StreamPtr;|healthy()->bool;disc(shutdown_started=False);create_remote();async_publ_mqtt()] +[StreamPtr||stream:MessageProt;ifc:AsyncIfc] +[<>]^-.-[InverterBase] +[InverterG3]-[note: Creates an GEN3 inverter instance with prot_class:Talent{bg:cornsilk}] +[InverterG3P]-[note: Creates an GEN3PLUS inverter instance with prot_class:SolarmanV5{bg:cornsilk}] +[InverterBase]^[InverterG3] +[InverterBase]^[InverterG3P] +[Proxy]^[InverterBase] +[InverterBase]-2>[StreamPtr] +[Proxy]++->[Mqtt;<>] [<>] -[InverterG3]-1..2>[<>] -[InverterG3]-1..2>[<>] +[StreamPtr]-1>[<>] +[StreamPtr]-1>[<>] -[InverterG3P]-1..2>[<>] -[InverterG3P]-1..2>[<>] [<>]use-.->[<>] [ModbusConn|host;port;addr;stream:InverterG3P;|]has-1>[InverterG3P] +[ModbusTcp]creates-*>[ModbusConn] diff --git a/app/proxy_2.svg b/app/proxy_2.svg index 63e6657..2aab16e 100644 --- a/app/proxy_2.svg +++ b/app/proxy_2.svg @@ -54,7 +54,7 @@ remote:StreamPtr local:StreamPtr -async_create_remote() +create_remote() close() @@ -63,7 +63,7 @@ AsyncStreamServer -async_create_remote +create_remote <async>server_loop() <async>_async_forward() @@ -105,7 +105,7 @@ remote:StreamPtr local:StreamPtr -async_create_remote() +create_remote() close() diff --git a/app/proxy_2.yuml b/app/proxy_2.yuml index 2d96500..33d4679 100644 --- a/app/proxy_2.yuml +++ b/app/proxy_2.yuml @@ -5,13 +5,13 @@ [note: You can stick notes on diagrams too!{bg:cornsilk}] [IterRegistry||__iter__] -[InverterG3|addr;remote:StreamPtr;local:StreamPtr|async_create_remote();;close()] -[InverterG3P|addr;remote:StreamPtr;local:StreamPtr|async_create_remote();;close()] +[InverterG3|addr;remote:StreamPtr;local:StreamPtr|create_remote();;close()] +[InverterG3P|addr;remote:StreamPtr;local:StreamPtr|create_remote();;close()] [<>||set_node_id();get_conn_no();;tx_add();tx_flush();tx_get();tx_peek();tx_log();tx_clear();tx_len();;fwd_add();fwd_flush();fwd_log();fwd_clear();rx_get();rx_peek();rx_log();rx_clear();rx_len();rx_set_cb();;prot_set_timeout_cb()] [AsyncIfcImpl|fwd_fifo:ByteFifo;tx_fifo:ByteFifo;rx_fifo:ByteFifo;conn_no:Count;node_id;timeout_cb] [AsyncStream|reader;writer;addr;r_addr;l_addr|;loop;disc();close();healthy();;__async_read();__async_write();__async_forward()] -[AsyncStreamServer|async_create_remote|server_loop();_async_forward();publish_outstanding_mqtt();close()] +[AsyncStreamServer|create_remote|server_loop();_async_forward();publish_outstanding_mqtt();close()] [AsyncStreamClient||client_loop();_async_forward())] [<>]^-.-[AsyncIfcImpl] [AsyncIfcImpl]^[AsyncStream] diff --git a/app/src/async_stream.py b/app/src/async_stream.py index 268a612..e260272 100644 --- a/app/src/async_stream.py +++ b/app/src/async_stream.py @@ -7,12 +7,12 @@ from typing import Self from itertools import count if __name__ == "app.src.async_stream": - from app.src.inverter import Inverter + from app.src.proxy import Proxy from app.src.byte_fifo import ByteFifo from app.src.async_ifc import AsyncIfc from app.src.infos import Infos else: # pragma: no cover - from inverter import Inverter + from proxy import Proxy from byte_fifo import ByteFifo from async_ifc import AsyncIfc from infos import Infos @@ -240,7 +240,7 @@ class AsyncStream(AsyncIfcImpl): await self._writer.wait_closed() def close(self) -> None: - logging.info(f'AsyncStream.close1() l{self.l_addr} | r{self.r_addr}') + logging.debug(f'AsyncStream.close() l{self.l_addr} | r{self.r_addr}') """close handler for a no waiting disconnect hint: must be called before releasing the connection instance @@ -249,7 +249,6 @@ class AsyncStream(AsyncIfcImpl): self._reader.feed_eof() # abort awaited read if self._writer.is_closing(): return - logger.info(f'AsyncStream.close2() l{self.l_addr} | r{self.r_addr}') self._writer.close() def healthy(self) -> bool: @@ -325,15 +324,15 @@ class AsyncStream(AsyncIfcImpl): class AsyncStreamServer(AsyncStream): def __init__(self, reader: StreamReader, writer: StreamWriter, - async_publ_mqtt, async_create_remote, + async_publ_mqtt, create_remote, rstream: "StreamPtr") -> None: AsyncStream.__init__(self, reader, writer, rstream) - self.async_create_remote = async_create_remote + self.create_remote = create_remote self.async_publ_mqtt = async_publ_mqtt def close(self) -> None: - logging.info('AsyncStreamServer.close()') - self.async_create_remote = None + logging.debug('AsyncStreamServer.close()') + self.create_remote = None self.async_publ_mqtt = None super().close() @@ -342,7 +341,7 @@ class AsyncStreamServer(AsyncStream): logger.info(f'[{self.node_id}:{self.conn_no}] ' f'Accept connection from {self.r_addr}') Infos.inc_counter('Inverter_Cnt') - await self.publish_outstanding_mqtt() + await self.publish_outstanding_mqtugt() await self.loop() Infos.dec_counter('Inverter_Cnt') await self.publish_outstanding_mqtt() @@ -360,7 +359,7 @@ class AsyncStreamServer(AsyncStream): async def _async_forward(self) -> None: """forward handler transmits data over the remote connection""" if not self.remote.stream: - await self.async_create_remote() + await self.create_remote() if self.remote.stream and \ self.remote.ifc.init_new_client_conn_cb(): await self.remote.ifc._AsyncStream__async_write() @@ -375,7 +374,7 @@ class AsyncStreamServer(AsyncStream): '''Publish all outstanding MQTT topics''' try: await self.async_publ_mqtt() - await Inverter._async_publ_mqtt_proxy_stat('proxy') + await Proxy._async_publ_mqtt_proxy_stat('proxy') except Exception: pass @@ -387,7 +386,7 @@ class AsyncStreamClient(AsyncStream): self.close_cb = close_cb def close(self) -> None: - logging.info('AsyncStreamClient.close()') + logging.debug('AsyncStreamClient.close()') self.close_cb = None super().close() diff --git a/app/src/inverter_base.py b/app/src/inverter_base.py index da062d3..9ed685a 100644 --- a/app/src/inverter_base.py +++ b/app/src/inverter_base.py @@ -9,7 +9,7 @@ from asyncio import StreamReader, StreamWriter if __name__ == "app.src.inverter_base": from app.src.iter_registry import AbstractIterMeta - from app.src.inverter import Inverter + from app.src.proxy import Proxy from app.src.async_stream import StreamPtr from app.src.async_stream import AsyncStreamClient from app.src.async_stream import AsyncStreamServer @@ -17,7 +17,7 @@ if __name__ == "app.src.inverter_base": from app.src.infos import Infos else: # pragma: no cover from iter_registry import AbstractIterMeta - from inverter import Inverter + from proxy import Proxy from async_stream import StreamPtr from async_stream import AsyncStreamClient from async_stream import AsyncStreamServer @@ -28,6 +28,7 @@ logger_mqtt = logging.getLogger('mqtt') class InverterIfc(metaclass=AbstractIterMeta): + _registry = [] @abstractmethod def __init__(self, reader: StreamReader, writer: StreamWriter, @@ -52,17 +53,16 @@ class InverterIfc(metaclass=AbstractIterMeta): pass # pragma: no cover @abstractmethod - async def async_create_remote(self) -> None: + async def create_remote(self) -> None: pass # pragma: no cover -class InverterBase(InverterIfc, Inverter): - _registry = [] +class InverterBase(InverterIfc, Proxy): def __init__(self, reader: StreamReader, writer: StreamWriter, config_id: str, prot_class, client_mode: bool = False): - Inverter.__init__(self) + Proxy.__init__(self) self._registry.append(weakref.ref(self)) self.addr = writer.get_extra_info('peername') self.config_id = config_id @@ -71,7 +71,7 @@ class InverterBase(InverterIfc, Inverter): self.remote = StreamPtr(None) ifc = AsyncStreamServer(reader, writer, self.async_publ_mqtt, - self.async_create_remote, + self.create_remote, self.remote) self.local = StreamPtr( @@ -113,7 +113,7 @@ class InverterBase(InverterIfc, Inverter): await self.local.ifc.disc() def healthy(self) -> bool: - logging.debug('Inverter healthy()') + logging.debug('InverterBase healthy()') if self.local.ifc and not self.local.ifc.healthy(): return False @@ -121,7 +121,7 @@ class InverterBase(InverterIfc, Inverter): return False return True - async def async_create_remote(self) -> None: + async def create_remote(self) -> None: '''Establish a client connection to the TSUN cloud''' tsun = Config.get(self.config_id) @@ -179,7 +179,7 @@ class InverterBase(InverterIfc, Inverter): for key in stream.new_data: await self.__async_publ_mqtt_packet(stream, key) for key in Infos.new_stat_data: - await Inverter._async_publ_mqtt_proxy_stat(key) + await Proxy._async_publ_mqtt_proxy_stat(key) except MqttCodeError as error: logging.error(f'Mqtt except: {error}') diff --git a/app/src/iter_registry.py b/app/src/iter_registry.py index 884d849..34ebb25 100644 --- a/app/src/iter_registry.py +++ b/app/src/iter_registry.py @@ -13,6 +13,5 @@ class AbstractIterMeta(ABCMeta): def __iter__(cls): for ref in cls._registry: obj = ref() - print(f'obj: {obj}') if obj is not None: yield obj diff --git a/app/src/inverter.py b/app/src/proxy.py similarity index 92% rename from app/src/inverter.py rename to app/src/proxy.py index dc224ee..9b75c37 100644 --- a/app/src/inverter.py +++ b/app/src/proxy.py @@ -2,7 +2,7 @@ import asyncio import logging import json -if __name__ == "app.src.inverter": +if __name__ == "app.src.proxy": from app.src.config import Config from app.src.mqtt import Mqtt from app.src.infos import Infos @@ -14,8 +14,8 @@ else: # pragma: no cover logger_mqtt = logging.getLogger('mqtt') -class Inverter(): - '''class Inverter is a baseclass +class Proxy(): + '''class Proxy is a baseclass The class has some class method for managing common resources like a connection to the MQTT broker or proxy error counter which are common @@ -34,12 +34,12 @@ class Inverter(): destroyed methods: - async_create_remote(): Establish a client connection to the TSUN cloud + create_remote(): Establish a client connection to the TSUN cloud async_publ_mqtt(): Publish data to MQTT broker ''' @classmethod def class_init(cls) -> None: - logging.debug('Inverter.class_init') + logging.debug('Proxy.class_init') # initialize the proxy statistics Infos.static_init() cls.db_stat = Infos() @@ -61,7 +61,7 @@ class Inverter(): # reset at midnight when you restart the proxy just before # midnight! inverters = Config.get('inverters') - # logger.debug(f'Inverters: {inverters}') + # logger.debug(f'Proxys: {inverters}') for inv in inverters.values(): if (type(inv) is dict): node_id = inv['node_id'] @@ -100,7 +100,7 @@ class Inverter(): @classmethod def class_close(cls, loop) -> None: # pragma: no cover - logging.debug('Inverter.class_close') + logging.debug('Proxy.class_close') logging.info('Close MQTT Task') loop.run_until_complete(cls.mqtt.close()) cls.mqtt = None diff --git a/app/src/server.py b/app/src/server.py index 1cca411..754b918 100644 --- a/app/src/server.py +++ b/app/src/server.py @@ -5,7 +5,7 @@ import os from asyncio import StreamReader, StreamWriter from aiohttp import web from logging import config # noqa F401 -from inverter import Inverter +from proxy import Proxy from inverter_base import InverterIfc from gen3.inverter_g3 import InverterG3 from gen3plus.inverter_g3p import InverterG3P @@ -87,7 +87,7 @@ async def handle_shutdown(web_task): # # first, disc all open TCP connections gracefully # - async for inverter in InverterIfc: + for inverter in InverterIfc: await inverter.disc(True) logging.info('Proxy disconnecting done') @@ -152,7 +152,7 @@ if __name__ == "__main__": ConfigErr = Config.class_init() if ConfigErr is not None: logging.info(f'ConfigErr: {ConfigErr}') - Inverter.class_init() + Proxy.class_init() Schedule.start() ModbusTcp(loop) @@ -185,7 +185,7 @@ if __name__ == "__main__": pass finally: logging.info("Event loop is stopped") - Inverter.class_close(loop) + Proxy.class_close(loop) logging.debug('Close event loop') loop.close() logging.info(f'Finally, exit Server "{serv_name}"') diff --git a/app/tests/test_inverter_g3.py b/app/tests/test_inverter_g3.py index 702186a..45438bb 100644 --- a/app/tests/test_inverter_g3.py +++ b/app/tests/test_inverter_g3.py @@ -7,7 +7,7 @@ from mock import patch from enum import Enum from app.src.infos import Infos from app.src.config import Config -from app.src.inverter import Inverter +from app.src.proxy import Proxy from app.src.inverter_base import InverterBase from app.src.singleton import Singleton from app.src.gen3.inverter_g3 import InverterG3 @@ -126,14 +126,14 @@ async def test_remote_conn(config_conn, patch_open_connection): assert asyncio.get_running_loop() with InverterG3(FakeReader(), FakeWriter()) as inverter: - await inverter.async_create_remote() + await inverter.create_remote() await asyncio.sleep(0) assert inverter.remote.stream del inverter cnt = 0 for inv in InverterBase: - print(f'Inverter refs:{gc.get_referrers(inv)}') + print(f'InverterBase refs:{gc.get_referrers(inv)}') cnt += 1 assert cnt == 0 @@ -147,12 +147,12 @@ async def test_remote_except(config_conn, patch_open_connection): test = TestType.RD_TEST_TIMEOUT with InverterG3(FakeReader(), FakeWriter()) as inverter: - await inverter.async_create_remote() + await inverter.create_remote() await asyncio.sleep(0) assert inverter.remote.stream==None test = TestType.RD_TEST_EXCEPT - await inverter.async_create_remote() + await inverter.create_remote() await asyncio.sleep(0) assert inverter.remote.stream==None del inverter @@ -169,7 +169,7 @@ async def test_mqtt_publish(config_conn, patch_open_connection): _ = patch_open_connection assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() with InverterG3(FakeReader(), FakeWriter()) as inverter: stream = inverter.local.stream @@ -197,7 +197,7 @@ async def test_mqtt_err(config_conn, patch_open_connection, patch_mqtt_err): _ = patch_mqtt_err assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() with InverterG3(FakeReader(), FakeWriter()) as inverter: stream = inverter.local.stream @@ -214,7 +214,7 @@ async def test_mqtt_except(config_conn, patch_open_connection, patch_mqtt_except _ = patch_mqtt_except assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() with InverterG3(FakeReader(), FakeWriter()) as inverter: stream = inverter.local.stream diff --git a/app/tests/test_inverter_g3p.py b/app/tests/test_inverter_g3p.py index f25b768..0f47cbe 100644 --- a/app/tests/test_inverter_g3p.py +++ b/app/tests/test_inverter_g3p.py @@ -6,7 +6,7 @@ from mock import patch from enum import Enum from app.src.infos import Infos from app.src.config import Config -from app.src.inverter import Inverter +from app.src.proxy import Proxy from app.src.inverter_base import InverterBase from app.src.singleton import Singleton from app.src.gen3plus.inverter_g3p import InverterG3P @@ -110,7 +110,7 @@ async def test_remote_conn(config_conn, patch_open_connection): assert asyncio.get_running_loop() with InverterG3P(FakeReader(), FakeWriter(), client_mode=False) as inverter: - await inverter.async_create_remote() + await inverter.create_remote() await asyncio.sleep(0) assert inverter.remote.stream @@ -124,12 +124,12 @@ async def test_remote_except(config_conn, patch_open_connection): test = TestType.RD_TEST_TIMEOUT with InverterG3P(FakeReader(), FakeWriter(), client_mode=False) as inverter: - await inverter.async_create_remote() + await inverter.create_remote() await asyncio.sleep(0) assert inverter.remote.stream==None test = TestType.RD_TEST_EXCEPT - await inverter.async_create_remote() + await inverter.create_remote() await asyncio.sleep(0) assert inverter.remote.stream==None @@ -139,7 +139,7 @@ async def test_mqtt_publish(config_conn, patch_open_connection): _ = patch_open_connection assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() with InverterG3P(FakeReader(), FakeWriter(), client_mode=False) as inverter: stream = inverter.local.stream @@ -167,7 +167,7 @@ async def test_mqtt_err(config_conn, patch_open_connection, patch_mqtt_err): _ = patch_mqtt_err assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() with InverterG3P(FakeReader(), FakeWriter(), client_mode=False) as inverter: stream = inverter.local.stream @@ -184,7 +184,7 @@ async def test_mqtt_except(config_conn, patch_open_connection, patch_mqtt_except _ = patch_mqtt_except assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() with InverterG3P(FakeReader(), FakeWriter(), client_mode=False) as inverter: stream = inverter.local.stream diff --git a/app/tests/test_modbus_tcp.py b/app/tests/test_modbus_tcp.py index 5611a03..a8a8f5a 100644 --- a/app/tests/test_modbus_tcp.py +++ b/app/tests/test_modbus_tcp.py @@ -10,7 +10,7 @@ from app.src.config import Config from app.src.infos import Infos from app.src.mqtt import Mqtt from app.src.messages import Message, State -from app.src.inverter import Inverter +from app.src.proxy import Proxy from app.src.modbus_tcp import ModbusConn, ModbusTcp @@ -175,7 +175,7 @@ async def test_modbus_cnf1(config_conn, patch_open): _ = patch_open global test assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() test = TestType.RD_TEST_TIMEOUT assert Infos.stat['proxy']['Inverter_Cnt'] == 0 @@ -196,7 +196,7 @@ async def test_modbus_cnf2(config_conn, patch_no_mqtt, patch_open): _ = patch_no_mqtt global test assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() test = TestType.RD_TEST_0_BYTES assert Infos.stat['proxy']['Inverter_Cnt'] == 0 @@ -222,7 +222,7 @@ async def test_modbus_cnf3(config_conn, patch_no_mqtt, patch_open): _ = patch_no_mqtt global test assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() test = TestType.RD_TEST_0_BYTES assert Infos.stat['proxy']['Inverter_Cnt'] == 0 @@ -255,7 +255,7 @@ async def test_mqtt_err(config_conn, patch_mqtt_err, patch_open): _ = patch_mqtt_err global test assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() test = TestType.RD_TEST_0_BYTES assert Infos.stat['proxy']['Inverter_Cnt'] == 0 @@ -288,7 +288,7 @@ async def test_mqtt_except(config_conn, patch_mqtt_except, patch_open): _ = patch_mqtt_except global test assert asyncio.get_running_loop() - Inverter.class_init() + Proxy.class_init() test = TestType.RD_TEST_0_BYTES assert Infos.stat['proxy']['Inverter_Cnt'] == 0 diff --git a/app/tests/test_inverter.py b/app/tests/test_proxy.py similarity index 79% rename from app/tests/test_inverter.py rename to app/tests/test_proxy.py index c59ff38..bee01fd 100644 --- a/app/tests/test_inverter.py +++ b/app/tests/test_proxy.py @@ -6,7 +6,7 @@ import logging from mock import patch, Mock from app.src.singleton import Singleton -from app.src.inverter import Inverter +from app.src.proxy import Proxy from app.src.mqtt import Mqtt from app.src.gen3plus.solarman_v5 import SolarmanV5 from app.src.config import Config @@ -63,13 +63,13 @@ def config_conn(test_hostname, test_port): async def test_inverter_cb(config_conn): _ = config_conn - with patch.object(Inverter, '_cb_mqtt_is_up', wraps=Inverter._cb_mqtt_is_up) as spy: - print('call Inverter.class_init') - Inverter.class_init() - assert 'homeassistant/' == Inverter.discovery_prfx - assert 'tsun/' == Inverter.entity_prfx - assert 'test_1/' == Inverter.proxy_node_id - await Inverter._cb_mqtt_is_up() + with patch.object(Proxy, '_cb_mqtt_is_up', wraps=Proxy._cb_mqtt_is_up) as spy: + print('call Proxy.class_init') + Proxy.class_init() + assert 'homeassistant/' == Proxy.discovery_prfx + assert 'tsun/' == Proxy.entity_prfx + assert 'test_1/' == Proxy.proxy_node_id + await Proxy._cb_mqtt_is_up() spy.assert_called_once() @pytest.mark.asyncio @@ -77,8 +77,8 @@ async def test_mqtt_is_up(config_conn): _ = config_conn with patch.object(Mqtt, 'publish') as spy: - Inverter.class_init() - await Inverter._cb_mqtt_is_up() + Proxy.class_init() + await Proxy._cb_mqtt_is_up() spy.assert_called() @pytest.mark.asyncio @@ -86,6 +86,6 @@ async def test_mqtt_proxy_statt_invalid(config_conn): _ = config_conn with patch.object(Mqtt, 'publish') as spy: - Inverter.class_init() - await Inverter._async_publ_mqtt_proxy_stat('InValId_kEy') + Proxy.class_init() + await Proxy._async_publ_mqtt_proxy_stat('InValId_kEy') spy.assert_not_called() diff --git a/app/tests/test_solarman.py b/app/tests/test_solarman.py index e4e8edc..a980744 100644 --- a/app/tests/test_solarman.py +++ b/app/tests/test_solarman.py @@ -682,6 +682,7 @@ def config_tsun_inv1(): Config.act_config = {'solarman':{'enabled': True},'inverters':{'Y170000000000001':{'monitor_sn': 2070233889, 'node_id':'inv1', 'modbus_polling': True, 'suggested_area':'roof', 'sensor_list': 688}}} def test_read_message(device_ind_msg): + Config.act_config = {'solarman':{'enabled': True}} m = MemoryStream(device_ind_msg, (0,)) m.read() # read complete msg, and dispatch msg assert not m.header_valid # must be invalid, since msg was handled and buffer flushed