"""Расширяет !тв: управление Hisense через tv_hisense.py.""" p = "/opt/hub-bots/hub/bots.py" b = open(p, encoding="utf-8").read() if "_hisense" in b: print("уже есть") raise SystemExit(0) old_start = " def _tv_cmd(self, arg):" idx = b.find(old_start) if idx < 0: print("не нашёл _tv_cmd") raise SystemExit(1) new_method = ''' def _hisense(self, flag, val=None): import subprocess as _sp cmd = ["/opt/music-tools/venv/bin/python", "/opt/hub-core/tv_hisense.py", "--" + flag] if val: cmd.append(val) try: out = _sp.run(cmd, capture_output=True, text=True, timeout=45) return (out.stdout or out.stderr or "").strip() except Exception as e: return "ошибка: " + str(e)[:80] def _tv_cmd(self, arg): """Телевизоры: список и управление (Chromecast + Hisense).""" import subprocess as _sp py = "/opt/music-tools/venv/bin/python" text = (arg or "").strip() low = text.lower() KEYS = { "домой": "KEY_HOME", "home": "KEY_HOME", "назад": "KEY_BACK", "back": "KEY_BACK", "ок": "KEY_OK", "ok": "KEY_OK", "вверх": "KEY_UP", "вниз": "KEY_DOWN", "влево": "KEY_LEFT", "вправо": "KEY_RIGHT", "меню": "KEY_MENU", "выход": "KEY_EXIT", "пуск": "KEY_PLAY", "пауза": "KEY_PAUSE", "стоп": "KEY_STOP", } # ключевые слова Hisense if low in ("громче", "тише", "mute", "звук"): flag = {"громче": "vol-up", "тише": "vol-down", "mute": "mute", "звук": "mute"}[low] return "**Hisense** — " + self._hisense(flag) if low in ("выключи", "выключить", "off"): return "**Hisense** — " + self._hisense("off") if low in ("включи тв", "включить", "on"): return "**Hisense** — " + self._hisense("on") if low.startswith("пульт"): key_word = text[5:].strip().lower() key = KEYS.get(key_word) or (key_word.upper() if key_word.startswith("key_") else None) if not key: return "Клавиши: " + ", ".join(sorted(KEYS)) + " или KEY_XXX" return "**Hisense** — " + self._hisense("key", key) if low.startswith("приложение") or low.startswith("приложении"): parts = text.split(None, 1) if len(parts) < 2: return "Укажите: !тв приложение youtube" return "**Hisense** — " + self._hisense("app", parts[1].strip()) if low in ("состояние", "статус", "hisense", "хайсенс"): return "**Hisense** — " + self._hisense("state") cmd = [py, "/opt/hub-core/tv.py"] if text.lower().startswith("включи"): url = text.split(None, 1)[1] if len(text.split(None, 1)) > 1 else "" cmd += ["--play", url] elif "пауза" in text.lower(): cmd += ["--pause"] elif "стоп" in text.lower(): cmd += ["--stop"] else: cmd += ["--list"] try: out = _sp.run(cmd, capture_output=True, text=True, timeout=60) except Exception as e: return "Ошибка: " + str(e)[:80] res = (out.stdout or out.stderr or "").strip() hisense = self._hisense("state") head = "**Телевизоры**" + chr(10) + chr(10) body = res if "не найдены" not in res and res else ( "Chromecast-ТВ не в сети (выключены). Известные: «Гостиная» " "192.168.1.147, «Smart TV Pro» 192.168.1.149") return (head + body + chr(10) + chr(10) + "**Hisense** — " + hisense + chr(10) + chr(10) + "_!тв пульт домой · !тв громче · !тв выключи · !тв приложение youtube_") ''' b = b[:idx] + new_method + "\n" + b[b.find("\n def ", idx + 10) + 1:] open(p, "w", encoding="utf-8").write(b) import ast ast.parse(b) print("бот: расширенный !тв")