05-sep: [parental] preparar API ASUSWRT

This commit is contained in:
juanjo
2026-09-05 18:41:58 +02:00
parent 18f4cb1139
commit 5aff5f2ab7
4 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
"""Lectura segura de datos ASUSWRT mediante la API web del router."""
import asyncio
import json
import os
import aiohttp
from asusrouter import AsusData, AsusRouter
def _load_env():
values = {}
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
try:
with open(path) as stream:
for line in stream:
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
values[key.strip()] = value.strip()
except OSError:
pass
return values
FILE_ENV = _load_env()
def _env(name, default=""):
return os.getenv(name, FILE_ENV.get(name, default)).strip()
async def _parental_data():
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()
return await router.async_get_data(AsusData.PARENTAL_CONTROL, force=True)
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)