feat: add multiple records in one request

This commit is contained in:
2025-10-04 22:34:39 +01:00
parent 633d87fb65
commit 8ea8954710

34
main.py
View File

@@ -17,15 +17,22 @@ router = AsusRouter(
use_ssl=credentials.get('use_ssl', False) use_ssl=credentials.get('use_ssl', False)
) )
async def main(rule_name): existing_rules = []
# Start connection to router new_rules = []
async def main(rule_tag, new_ip):
global existing_rules
global new_rules
await router.async_connect() await router.async_connect()
# Get port forwarding rules # Get port forwarding rules
pfwd = await router.async_get_data(AsusData.PORT_FORWARDING) pfwd = await router.async_get_data(AsusData.PORT_FORWARDING)
# Store existing rules that match tag
for rule in pfwd['rules']: for rule in pfwd['rules']:
if(rule_name in rule.name): if rule_tag in rule.name:
rules = [ existing_rules += [
PortForwardingRule( PortForwardingRule(
name=rule.name, name=rule.name,
ip_address=rule.ip_address, ip_address=rule.ip_address,
@@ -35,12 +42,21 @@ async def main(rule_name):
port_external=rule.port_external port_external=rule.port_external
) )
] ]
result = await router.async_remove_port_forwarding_rules(rules=rules) new_rules += [
print(result) 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() await router.async_disconnect()
if __name__ == "__main__": if __name__ == "__main__":
asyncio.run(main(""))