Allow turning on the TV using a webhook

This commit is contained in:
PapaTutuWawa 2025-01-13 22:51:02 +01:00
parent 50d5e40a46
commit 2cf210bce6
5 changed files with 53 additions and 16 deletions

View File

@ -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()

View File

@ -41,6 +41,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:
config_data = {}
@ -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"),
)

View File

@ -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"])

View File

@ -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:

46
microkodi/tv.py Normal file
View File

@ -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()