2026-01-25 02:37:07 +01:00

70 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import asyncio
import logging
import os
import sys
from config import load_config
from errors import ConfigError
from fritzbox import FritzBox
from homeassistant import HomeAssistantAPI
from logging_config import configure_logging
from sync_controller import SyncController
logger = logging.getLogger(__name__)
async def main():
config_path = sys.argv[1]
try:
config = load_config(config_path)
except ConfigError as exc:
logging.basicConfig(level=logging.ERROR)
logging.error("Invalid config: %s", exc)
return
configure_logging(config.log_level)
logger.debug("Loaded config")
if not config.log_ws_messages:
logging.getLogger("websockets").setLevel(logging.WARNING)
logging.getLogger("websockets.client").setLevel(logging.WARNING)
logging.getLogger("websockets.server").setLevel(logging.WARNING)
if not config.log_http_requests:
logging.getLogger("httpx").setLevel(logging.WARNING)
logging.getLogger("httpcore").setLevel(logging.WARNING)
fritzbox = config.fritzbox
fb = FritzBox(
url=fritzbox.url,
user=fritzbox.username,
password=fritzbox.password,
update_timeout=config.update_timeout,
dry_run=config.dry_run,
force_ipv4=config.force_ipv4,
request_timeout=config.request_timeout,
request_retries=config.request_retries,
)
supervisor_url = "ws://supervisor/core/websocket"
if "SUPERVISOR_URL" in os.environ:
supervisor_url = os.environ["SUPERVISOR_URL"]
supervisor_token = os.environ.get("SUPERVISOR_TOKEN")
if not supervisor_token:
logger.error("Missing SUPERVISOR_TOKEN; cannot connect to Home Assistant")
return
ha = HomeAssistantAPI(supervisor_token, supervisor_url)
controller = SyncController(ha, fb, config.offset_threshold)
controller.load_mappings(config.mappings)
try:
logger.info("Starting sync controller")
await controller.run()
except (KeyboardInterrupt, asyncio.CancelledError):
logger.info("Shutdown requested")
await controller.shutdown()
try:
asyncio.run(main())
except KeyboardInterrupt:
pass