05-sep: [parental] pausar y bloquear dispositivos por nombre
This commit is contained in:
@@ -7,6 +7,7 @@ import unicodedata
|
||||
|
||||
import aiohttp
|
||||
from asusrouter import AsusData, AsusRouter
|
||||
from asusrouter.modules.parental_control import PCRuleType, ParentalControlRule
|
||||
|
||||
|
||||
def _load_env():
|
||||
@@ -71,6 +72,41 @@ async def _parental_data():
|
||||
await session.close()
|
||||
|
||||
|
||||
async def _set_block(mac, name, blocked):
|
||||
username = _env("ASUS_USERNAME")
|
||||
password = _env("ASUS_PASSWORD")
|
||||
if not username or not password:
|
||||
raise RuntimeError(
|
||||
"Falta ASUS_USERNAME o ASUS_PASSWORD en el .env de la Raspberry"
|
||||
)
|
||||
session = aiohttp.ClientSession()
|
||||
router = AsusRouter(
|
||||
hostname=_env("ASUS_ROUTER_IP", "192.168.50.1"),
|
||||
username=username,
|
||||
password=password,
|
||||
session=session,
|
||||
)
|
||||
try:
|
||||
await router.async_connect()
|
||||
rule = ParentalControlRule(
|
||||
mac=mac,
|
||||
name=name if blocked else None,
|
||||
type=PCRuleType.BLOCK if blocked else PCRuleType.REMOVE,
|
||||
)
|
||||
return await router.async_set_state(rule)
|
||||
finally:
|
||||
await router.async_disconnect()
|
||||
await session.close()
|
||||
|
||||
|
||||
def parental_status():
|
||||
data = asyncio.run(_parental_data())
|
||||
return json.dumps(data, ensure_ascii=False, indent=2, default=str)
|
||||
|
||||
|
||||
def cambiar_bloqueo(nombre, blocked):
|
||||
device = resolver_dispositivo(nombre)
|
||||
if not device:
|
||||
raise ValueError(f"Dispositivo no reconocido: {nombre}")
|
||||
asyncio.run(_set_block(device["mac"], device["name"], blocked))
|
||||
return f"{'Bloqueado' if blocked else 'Activado'}: {device['name']}"
|
||||
|
||||
Reference in New Issue
Block a user