Skip to content

Commit

Permalink
Add AsusBlockAll states to block internet access
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaskivskyi committed Dec 3, 2023
1 parent 8d84c3a commit 6ec622b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions asusrouter/modules/data_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
MAP_WIREGUARD_SERVER,
)
from asusrouter.modules.parental_control import (
KEY_PARENTAL_CONTROL_BLOCK_ALL,
KEY_PARENTAL_CONTROL_MAC,
KEY_PARENTAL_CONTROL_NAME,
KEY_PARENTAL_CONTROL_STATE,
Expand Down Expand Up @@ -110,6 +111,7 @@ def __init__(
for key, _, _ in [converters.safe_unpack_keys(element)]
],
"parental_control": [
KEY_PARENTAL_CONTROL_BLOCK_ALL,
KEY_PARENTAL_CONTROL_MAC,
KEY_PARENTAL_CONTROL_NAME,
KEY_PARENTAL_CONTROL_STATE,
Expand Down
7 changes: 7 additions & 0 deletions asusrouter/modules/endpoint/hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
from asusrouter.modules.endpoint.error import AccessError
from asusrouter.modules.led import AsusLED
from asusrouter.modules.parental_control import (
KEY_PARENTAL_CONTROL_BLOCK_ALL,
KEY_PARENTAL_CONTROL_MAC,
KEY_PARENTAL_CONTROL_STATE,
KEY_PARENTAL_CONTROL_TYPE,
MAP_PARENTAL_CONTROL_ITEM,
MAP_PARENTAL_CONTROL_TYPE,
AsusBlockAll,
AsusParentalControl,
ParentalControlRule,
)
Expand Down Expand Up @@ -335,6 +337,11 @@ def process_parental_control(data: dict[str, Any]) -> dict[str, Any]:
safe_int(data.get(KEY_PARENTAL_CONTROL_STATE), default=-999)
)

# Block all
parental_control["block_all"] = AsusBlockAll(
safe_int(data.get(KEY_PARENTAL_CONTROL_BLOCK_ALL), default=-999)
)

# Rules
rules = {}

Expand Down
32 changes: 25 additions & 7 deletions asusrouter/modules/parental_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

_LOGGER = logging.getLogger(__name__)

KEY_PARENTAL_CONTROL_BLOCK_ALL = "MULTIFILTER_BLOCK_ALL"
KEY_PARENTAL_CONTROL_MAC = "MULTIFILTER_MAC"
KEY_PARENTAL_CONTROL_NAME = "MULTIFILTER_DEVICENAME"
KEY_PARENTAL_CONTROL_STATE = "MULTIFILTER_ALL"
Expand Down Expand Up @@ -51,29 +52,46 @@ class AsusParentalControl(IntEnum):
ON = 1


class AsusBlockAll(IntEnum):
"""Asus block all state."""

UNKNOWN = -999
OFF = 0
ON = 1


async def set_state(
callback: Callable[..., Awaitable[bool]],
state: AsusParentalControl,
state: AsusParentalControl | AsusBlockAll,
**kwargs: Any,
) -> bool:
"""Set the parental control state."""

# Check if state is available and valid
if not isinstance(state, AsusParentalControl) or not state.value in (0, 1):
_LOGGER.debug("No state found in arguments")
if not isinstance(
state, (AsusParentalControl, AsusBlockAll)
) or not state.value in (0, 1):
return False

arguments = {
KEY_PARENTAL_CONTROL_STATE: 1 if state == AsusParentalControl.ON else 0
}
service_arguments = {}

match state:
case a if isinstance(a, AsusParentalControl):
service_arguments = {
KEY_PARENTAL_CONTROL_STATE: 1 if state == AsusParentalControl.ON else 0
}
case a if isinstance(a, AsusBlockAll):
service_arguments = {
KEY_PARENTAL_CONTROL_BLOCK_ALL: 1 if state == AsusBlockAll.ON else 0
}

# Get the correct service call
service = "restart_firewall"

# Call the service
return await callback(
service=service,
arguments=arguments,
arguments=service_arguments,
apply=True,
expect_modify=kwargs.get("expect_modify", False),
)
4 changes: 3 additions & 1 deletion asusrouter/modules/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from asusrouter.modules.connection import ConnectionState
from asusrouter.modules.data import AsusData, AsusDataState
from asusrouter.modules.parental_control import AsusParentalControl
from asusrouter.modules.parental_control import AsusBlockAll, AsusParentalControl
from asusrouter.modules.port_forwarding import AsusPortForwarding
from asusrouter.modules.system import AsusSystem
from asusrouter.modules.vpnc import AsusVPNC
Expand All @@ -37,6 +37,7 @@ class AsusState(Enum):
"""Asus state."""

NONE = AsusStateNone
BLOCK_ALL = AsusBlockAll
CONNECTION = ConnectionState
LED = AsusLED
OPENVPN_CLIENT = AsusOVPNClient
Expand All @@ -52,6 +53,7 @@ class AsusState(Enum):

AsusStateMap: dict[AsusState, Optional[AsusData]] = {
AsusState.NONE: None,
AsusState.BLOCK_ALL: AsusData.PARENTAL_CONTROL,
AsusState.CONNECTION: None,
AsusState.LED: AsusData.LED,
AsusState.OPENVPN_CLIENT: AsusData.OPENVPN_CLIENT,
Expand Down
4 changes: 4 additions & 0 deletions tests/modules/test_parental_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import pytest

from asusrouter.modules.parental_control import (
KEY_PARENTAL_CONTROL_BLOCK_ALL,
KEY_PARENTAL_CONTROL_STATE,
AsusBlockAll,
AsusParentalControl,
set_state,
)
Expand All @@ -20,6 +22,8 @@
# Correct states
(AsusParentalControl.ON, True, True, {KEY_PARENTAL_CONTROL_STATE: 1}),
(AsusParentalControl.OFF, False, True, {KEY_PARENTAL_CONTROL_STATE: 0}),
(AsusBlockAll.ON, True, True, {KEY_PARENTAL_CONTROL_BLOCK_ALL: 1}),
(AsusBlockAll.OFF, False, True, {KEY_PARENTAL_CONTROL_BLOCK_ALL: 0}),
# Wrong states
(AsusParentalControl.UNKNOWN, False, False, {}),
(None, False, False, {}),
Expand Down

0 comments on commit 6ec622b

Please sign in to comment.