Skip to content

Debloat any Android or Google TV over Wi-Fi and get an Apple TV-style home.

tools: philips-api.py takes a port only when /6/system decodes as the API

Damian Wolgastab53688d528d
diff --git a/tests/test_philips_api.py b/tests/test_philips_api.py
index 09d63f94c..9296797e2 100644
--- a/tests/test_philips_api.py
+++ b/tests/test_philips_api.py
@@ -62,6 +62,31 @@ class PhilipsApi(unittest.TestCase):
self.assertEqual(code, 1)
self.assertIn("org.droidtv.xtv", out)
+ def test_a_page_that_is_not_the_api_falls_back_to_1925(self):
+ base = "http://192.168.1.50:1925/6/"
+ answers = {"https://192.168.1.50:1926/6/system": (200, "<html>login</html>"),
+ base + "system": (200, SYSTEM)}
+ answers.update({base + p: (401, "") for p in philips_api.PROBE})
+ code, out, _ = run(answers)
+ self.assertEqual(code, 0)
+ self.assertIn("API on http://192.168.1.50:1925", out)
+ self.assertIn("(not the API)", out)
+
+ def test_json_that_is_not_a_system_object_is_no_api(self):
+ for body in ("{}", "null", "[]", '{"api_version": "6"}', "{broken"):
+ with self.subTest(body=body):
+ code, out, _ = run({"https://192.168.1.50:1926/6/system": (200, body)})
+ self.assertEqual(code, 1)
+ self.assertIn("org.droidtv.xtv", out)
+
+ def test_wrong_typed_feature_metadata_counts_as_unknown(self):
+ system = json.dumps({"api_version": {"Major": 6, "Minor": 1, "Patch": 0},
+ "featuring": {"systemfeatures": [], "jsonfeatures": {"ambilight": 5}}})
+ code, out, _ = run({"https://192.168.1.50:1926/6/system": (200, system)})
+ self.assertEqual(code, 0)
+ self.assertIn("6.1.0", out)
+ self.assertNotIn("LoungeLight", out)
+
def test_no_address_is_a_usage_error(self):
with mock.patch.dict("os.environ", {"TV_HOST": ""}):
code, _, asked = run({}, argv=("philips-api.py",))
diff --git a/tools/philips-api.py b/tools/philips-api.py
index 3203c37e2..cacb66256 100644
--- a/tools/philips-api.py
+++ b/tools/philips-api.py
@@ -47,15 +47,35 @@ def get(url, timeout=8):
return None, str(failure)
+def system_info(body):
+ """The decoded /6/system object, or None when the body is not one (an HTML page, broken JSON, {})."""
+ try:
+ info = json.loads(body)
+ except ValueError:
+ return None
+ if isinstance(info, dict) and isinstance(info.get("api_version"), dict):
+ return info
+ return None
+
+
+def section(parent, key):
+ """parent[key] when it is an object, else {}: optional metadata of the wrong type counts as unknown."""
+ value = parent.get(key)
+ return value if isinstance(value, dict) else {}
+
+
def find_api(host):
- """The base URL whose /6/system answers with a body, or None. Prints each port it tried."""
+ """The base URL and decoded /6/system of the first port that answers as the API, or (None, None).
+ Prints each port it tried."""
for port, scheme in PORTS:
base = f"{scheme}://{host}:{port}"
status, body = get(f"{base}/6/system")
- if status == 200 and body.strip():
- return base, body
- print(f" {base}/6/system -> {status if status is not None else body}")
- return None, ""
+ info = system_info(body) if status == 200 else None
+ if info is not None:
+ return base, info
+ why = status if status is not None else body
+ print(f" {base}/6/system -> {why}{' (not the API)' if status == 200 else ''}")
+ return None, None
def main(argv):
@@ -65,7 +85,7 @@ def main(argv):
print(__doc__.split("\n\n")[1], file=sys.stderr)
return 2
- base, body = find_api(host)
+ base, info = find_api(host)
if not base:
print("\nNo JointSpace API. If this TV used to work with Home Assistant, check that\n"
"org.droidtv.xtv is still enabled:\n"
@@ -73,19 +93,14 @@ def main(argv):
return 1
print(f"API on {base}")
- try:
- info = json.loads(body)
- except ValueError:
- print(" /6/system answered, but not with JSON")
- return 0
- api = info.get("api_version", {})
- feat = info.get("featuring", {})
- ambilight = feat.get("jsonfeatures", {}).get("ambilight")
+ api = info["api_version"]
+ feat = section(info, "featuring")
+ ambilight = section(feat, "jsonfeatures").get("ambilight")
print(f" name {info.get('name')}")
print(f" api_version {api.get('Major')}.{api.get('Minor')}.{api.get('Patch')}")
- print(f" pairing {feat.get('systemfeatures', {}).get('pairing_type')}")
+ print(f" pairing {section(feat, 'systemfeatures').get('pairing_type')}")
print(f" ambilight {ambilight}")
- if ambilight and "LoungeLight" in ambilight:
+ if isinstance(ambilight, list) and "LoungeLight" in ambilight:
print(" (LoungeLight is Ambilight with the panel off. For it to answer while\n"
" the TV is off, turn on Eco settings > Quick start.)")