diff --git a/hoymiles_wifi/__main__.py b/hoymiles_wifi/__main__.py index 877c66f..ac48f2d 100644 --- a/hoymiles_wifi/__main__.py +++ b/hoymiles_wifi/__main__.py @@ -314,6 +314,12 @@ async def main() -> None: parser.add_argument( "--host", type=str, required=True, help="IP address or hostname of the DTU" ) + parser.add_argument( + "--local_addr", + type=str, + required=False, + help="IP address of the interface to bind to", + ) parser.add_argument( "--as-json", action="store_true", @@ -347,7 +353,7 @@ async def main() -> None: ) args = parser.parse_args() - dtu = DTU(args.host) + dtu = DTU(args.host, args.local_addr) # Execute the specified command using a switch case switch = { diff --git a/hoymiles_wifi/dtu.py b/hoymiles_wifi/dtu.py index d855698..740f676 100644 --- a/hoymiles_wifi/dtu.py +++ b/hoymiles_wifi/dtu.py @@ -70,10 +70,11 @@ class NetworkState(Enum): class DTU: """DTU class.""" - def __init__(self, host: str): + def __init__(self, host: str, local_addr: str = None): """Initialize DTU class.""" self.host = host + self.local_addr = local_addr self.state = NetworkState.Unknown self.sequence = 0 self.mutex = asyncio.Lock() @@ -365,11 +366,13 @@ class DTU: + request_as_bytes ) - address = (self.host, dtu_port) + ip_to_bind = (self.local_addr, 0) if self.local_addr is not None else None async with self.mutex: try: - reader, writer = await asyncio.open_connection(*address) + reader, writer = await asyncio.open_connection( + host=self.host, port=dtu_port, local_addr=ip_to_bind + ) writer.write(message) await writer.drain()