diff --git a/microkodi/cec.py b/microkodi/cec.py new file mode 100644 index 0000000..8f41d79 --- /dev/null +++ b/microkodi/cec.py @@ -0,0 +1,10 @@ +import cec + +class CECHandler: + def __init__(self): + cec.init() + + def power_on_if_needed(self): + tc = cec.Device(cec.CECDEVICE_TV) + if not tc.is_on(): + tc.power_on() diff --git a/microkodi/config.py b/microkodi/config.py index 9ecab8c..283bf84 100644 --- a/microkodi/config.py +++ b/microkodi/config.py @@ -37,6 +37,9 @@ class Config: # The entire configuration file for use in user scripts options: dict[str, Any] + + # Enables the use of CEC + cec: bool def load_config(config_path: Path | None) -> Config: if config_path is None: @@ -64,4 +67,5 @@ def load_config(config_path: Path | None) -> Config: config_data.get("players", {}), ), options=config_data.get("options", {}), + cec=config_data.get("cec", False), ) diff --git a/microkodi/jsonrpc.py b/microkodi/jsonrpc.py index 5b93c87..e025f74 100644 --- a/microkodi/jsonrpc.py +++ b/microkodi/jsonrpc.py @@ -151,6 +151,11 @@ class PlayerRpcObject(JsonRpcObject): @after(lambda: I.get("DataBridge").set_loading(False)) def open(self, params: dict[str, Any]) -> Any: + # Turn on the TV + config: Config = I.get("Config") + if config.cec: + I.get("CECHandler").power_of_if_needed() + I.get("DataBridge").set_loading(True) url = urlparse(params["item"]["file"]) @@ -161,7 +166,6 @@ class PlayerRpcObject(JsonRpcObject): url = urlparse(url.query) # Find out what player class to use - config: Config = I.get("Config") scheme_configuration = config.players.get(url.scheme) if scheme_configuration is None: I.get("DataBridge").notification.emit(f"No player available for {url.scheme}") diff --git a/microkodi/main.py b/microkodi/main.py index b3aea6d..6d0927c 100644 --- a/microkodi/main.py +++ b/microkodi/main.py @@ -13,6 +13,7 @@ from PySide6.QtQml import QQmlApplicationEngine from microkodi.jsonrpc import JsonRpcHandler, GlobalMethodHandler from microkodi.ui.bridge import DataBridge from microkodi.config import Config, load_config +from microkodi.cec import CECHandler from microkodi.repository import I @@ -23,6 +24,11 @@ def run_kodi_server(): method_handler = GlobalMethodHandler() I.register("GlobalMethodHandler", method_handler) + # Setup CEC + if config.cec: + I.register("CECHandler", CECHandler()) + logger.info("Enabling CEC support") + # Load extra plugins if config.scripts: logger.info("Loading scripts...") diff --git a/pyproject.toml b/pyproject.toml index b23d560..76a1b44 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,8 @@ version = "0.1.0" dependencies = [ "pyside6", "requests", - "yt-dlp" + "yt-dlp", + "cec" ] [tools.build]