Debian Codegraph @

asusrouter/modules/port_forwarding.py

raw
1 """Port forwarding module."""
2
3 from collections.abc import Awaitable, Callable
4 from dataclasses import dataclass
5 from enum import IntEnum
6 import logging
7 from typing import Any
8
9 _LOGGER = logging.getLogger(__name__)
10
11 KEY_PORT_FORWARDING_LIST = "vts_rulelist"
12 KEY_PORT_FORWARDING_STATE = "vts_enable_x"
13
14
15 @dataclass(frozen=True)
16 class PortForwardingRule:
17 """Port forwarding class."""
18
19 name: str = ""
20 ip_address: str = ""
21 port: str = ""
22 protocol: str = ""
23 ip_external: str = ""
24 port_external: str = ""
25
26
27 class AsusPortForwarding(IntEnum):
28 """Asus port forwarding state."""
29
30 UNKNOWN = -999
31 OFF = 0
32 ON = 1
33
34
35 async def set_state(
36 callback: Callable[..., Awaitable[bool]],
37 state: AsusPortForwarding,
38 **kwargs: Any,
39 ) -> bool:
40 """Set the parental control state."""
41
42 # Check if state is available and valid
43 if not isinstance(state, AsusPortForwarding) or state.value not in (0, 1):
44 _LOGGER.debug("No state found in arguments")
45 return False
46
47 arguments = {
48 KEY_PORT_FORWARDING_STATE: 1 if state == AsusPortForwarding.ON else 0
49 }
50
51 # Get the correct service call
52 service = "restart_firewall"
53
54 # Call the service
55 return await callback(
56 service=service,
57 arguments=arguments,
58 apply=True,
59 expect_modify=kwargs.get("expect_modify", False),
60 )