mirror of
https://github.com/suaveolent/hoymiles-wifi.git
synced 2024-09-20 03:10:11 +00:00
add local_addr parameter to allow to bind to specific interface
This commit is contained in:
parent
18f88d810c
commit
26148448cb
@ -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 = {
|
||||
|
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user