From 8ea8954710c222b61d8feb67397d4ab0c29dfa8f Mon Sep 17 00:00:00 2001 From: Jordan Walster Date: Sat, 4 Oct 2025 22:34:39 +0100 Subject: [PATCH] feat: add multiple records in one request --- main.py | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index ca0396a..c10dbbf 100644 --- a/main.py +++ b/main.py @@ -17,15 +17,22 @@ router = AsusRouter( use_ssl=credentials.get('use_ssl', False) ) -async def main(rule_name): - # Start connection to router +existing_rules = [] +new_rules = [] + +async def main(rule_tag, new_ip): + + global existing_rules + global new_rules await router.async_connect() + # Get port forwarding rules pfwd = await router.async_get_data(AsusData.PORT_FORWARDING) + # Store existing rules that match tag for rule in pfwd['rules']: - if(rule_name in rule.name): - rules = [ + if rule_tag in rule.name: + existing_rules += [ PortForwardingRule( name=rule.name, ip_address=rule.ip_address, @@ -35,12 +42,21 @@ async def main(rule_name): port_external=rule.port_external ) ] - result = await router.async_remove_port_forwarding_rules(rules=rules) - print(result) + new_rules += [ + PortForwardingRule( + name=rule.name, + ip_address=rule.ip_address, + port=rule.port, + protocol=rule.protocol, + ip_external=new_ip, + port_external=rule.port_external + ) + ] + + result = await router.async_set_port_forwarding_rules(new_rules) + if result: + print("New records set.") - # End connection to router await router.async_disconnect() if __name__ == "__main__": - asyncio.run(main("")) -