diff --git a/microkodi/cec_handler.py b/microkodi/cec_handler.py deleted file mode 100644 index 8f41d79..0000000 --- a/microkodi/cec_handler.py +++ /dev/null @@ -1,10 +0,0 @@ -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 283bf84..2b614c3 100644 --- a/microkodi/config.py +++ b/microkodi/config.py @@ -40,6 +40,9 @@ class Config: # Enables the use of CEC cec: bool + + # Webhook to trigger to turn on power to the TV + tv_power_webhook: str | None def load_config(config_path: Path | None) -> Config: if config_path is None: @@ -68,4 +71,5 @@ def load_config(config_path: Path | None) -> Config: ), options=config_data.get("options", {}), cec=config_data.get("cec", False), + tv_power_webhook=config_data.get("tv_power_webhook"), ) diff --git a/microkodi/jsonrpc.py b/microkodi/jsonrpc.py index f5f7fb8..3a90b98 100644 --- a/microkodi/jsonrpc.py +++ b/microkodi/jsonrpc.py @@ -153,8 +153,7 @@ class PlayerRpcObject(JsonRpcObject): def open(self, params: dict[str, Any]) -> Any: # Turn on the TV config: Config = I.get("Config") - if config.cec: - I.get("CECHandler").power_on_if_needed() + I.get("TVHandler").turn_on_tv() I.get("DataBridge").set_loading(True) url = urlparse(params["item"]["file"]) diff --git a/microkodi/main.py b/microkodi/main.py index a2d49da..1aa4d74 100644 --- a/microkodi/main.py +++ b/microkodi/main.py @@ -13,7 +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_handler import CECHandler +from microkodi.cec_handler import TVHandler from microkodi.repository import I @@ -25,9 +25,7 @@ def run_kodi_server(): I.register("GlobalMethodHandler", method_handler) # Setup CEC - if config.cec: - I.register("CECHandler", CECHandler()) - logger.info("Enabling CEC support") + I.register("TVHandler", TVHandler()) # Load extra plugins if config.scripts: diff --git a/microkodi/tv.py b/microkodi/tv.py new file mode 100644 index 0000000..068d70b --- /dev/null +++ b/microkodi/tv.py @@ -0,0 +1,46 @@ +import logging +import time + +import cec +import requests + +from microkodi.config import Config +from microkodi.repository import I + + +class TVHandler: + logger: logging.Logger + + def __init__(self): + self.logger = logging.getLogger("TVHandler") + + config: Config = I.get("Config") + if config.cec: + cec.init() + self.logger.debug("CEC initialised") + + def enable_power_if_configured(self) -> bool: + config: Config = I.get("Config") + if config.tv_power_webhook is not None: + try: + requests.put(config.tv_power_webhook) + self.logger.debug("Triggered webhook to enable power to the TV") + except Exception as ex: + self.logger.warn("Failed to enable power to the TV") + return config.tv_power_webhook is not None + + def power_on_if_needed(self) -> bool: + config: Config = I.get("Config") + if config.cec: + tc = cec.Device(cec.CECDEVICE_TV) + if not tc.is_on(): + tc.power_on() + return config.cec + + + def turn_on_tv(self): + if self.enable_power_if_configured(): + self.logger.debug("Waiting 500ms for TV to get power...") + time.sleep(500) + + self.power_on_if_needed() \ No newline at end of file