How to Schedule a Reboot of Your Sky Router from a Raspberry Pi

•

Is your internet connection dropping unexpectedly? A scheduled reboot of your router might help keep things running smoothly. In this guide, we’ll walk you through setting up a Raspberry Pi to automate the reboot of your Sky router, saving you time and hassle.

import asyncio
from playwright.async_api import async_playwright
from dotenv import load_dotenv
import os
import logging
from datetime import datetime

# Set up logging (rotates weekly by overwriting each weekday's log)
log_dir = "/home/pi/scripts/python/sky_router/logs"
os.makedirs(log_dir, exist_ok=True)
weekday = datetime.now().strftime("%a")  # Mon, Tue, etc.
log_file = os.path.join(log_dir, f"{weekday}.log")

logging.basicConfig(
    filename=log_file,
    filemode='w',  # Overwrite daily (rotates weekly)
    format='%(asctime)s %(levelname)s: %(message)s',
    datefmt='%Y-%m-%d %H:%M:%S',
    level=logging.INFO
)

load_dotenv()
ROUTER_USERNAME = "admin"
ROUTER_PASSWORD = os.getenv("ROUTER_PASSWORD")

async def reboot_router():
    try:
        async with async_playwright() as p:
            browser = await p.chromium.launch(headless=True, slow_mo=300)
            context = await browser.new_context(
                http_credentials={"username": ROUTER_USERNAME, "password": ROUTER_PASSWORD}
            )
            page = await context.new_page()
            await page.goto("http://192.168.0.1/sky_diagnostics.html")
            logging.info("Navigated to diagnostics page.")

            # Set up a handler for the dialog before clicking
            async def handle_dialog(dialog):
                logging.warning(f"Dialog message: {dialog.message}")
                await dialog.accept()

            page.on("dialog", handle_dialog)

            # Target the reboot button by text
            reboot_button = page.locator('a:has-text("Reboot")')
            if await reboot_button.is_visible():
                logging.info("Found reboot button, clicking to reboot...")
                await reboot_button.click()
                await asyncio.sleep(15)
                logging.info("Reboot initiated successfully.")
            else:
                logging.error("Could not find the reboot button.")

            await browser.close()
            logging.info("Browser session closed.")

    except Exception as e:
        logging.exception(f"An error occurred while rebooting: {e}")

if __name__ == "__main__":
    asyncio.run(reboot_router())

Leave a Reply

Your email address will not be published. Required fields are marked *