Skip to content

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

tools: philips-api.py names the rest of what a colon can carry, and an address it cannot send to

xbsheng217b0cbcc8e1
diff --git a/tests/test_philips_api.py b/tests/test_philips_api.py
index 770e3fa02..78c626400 100644
--- a/tests/test_philips_api.py
+++ b/tests/test_philips_api.py
@@ -17,7 +17,9 @@ only a refused connection is the TV speaking.
An address is the TV's own, not a TV_HOST written IP:port or a URL pasted from a browser: the two ports
below are added here, so `192.168.1.50:1926` built `https://192.168.1.50:1926:1926/6/system`, a name that
does not resolve, and the run answered "nothing answered, the set may be off" over an argument it could
-not use. Both shapes are named before a URL is asked, with the address alone to pass.
+not use. The same happens to the rest of what a colon can carry - `tv.example:`, `tv.example:abc` and a
+bare `fe80::1` - so every shape is named before a URL is asked, with the address alone to pass (a bare
+IPv6 with its brackets, which is the form tools/tv.py documents).
Run from the repository root: python3 -m unittest discover -s tests -v
"""
@@ -153,6 +155,22 @@ class ASilentTVIsNotADisabledPackage(unittest.TestCase):
self.assertEqual(code, 2)
self.assertIn("could not check 192.168.1.51", err)
+ def test_an_address_this_computer_cannot_connect_to_is_not_an_answer(self):
+ # A bracketed link-local IPv6 with no zone index (or an address family this computer does not
+ # have) is refused by the local socket layer with EINVAL, before any packet goes out. It came
+ # back as the TV answering, so `TV_HOST=[fe80::1]` - the form the address guard now asks for -
+ # named org.droidtv.xtv as a package to check over an argument the kernel would not send.
+ invalid = {f"{scheme}://[fe80::1]:{port}/6/system":
+ (None, "Invalid argument", OSError(errno.EINVAL, "Invalid argument"))
+ for port, scheme in philips_api.PORTS}
+ code, out, _, err = run(invalid, argv=("philips-api.py", "[fe80::1]"))
+ self.assertEqual(code, 2)
+ self.assertIn("could not check [fe80::1]", err)
+ self.assertIn("nothing answered on 1926 and 1925", err)
+ self.assertNotIn("No JointSpace API", err + out)
+ self.assertNotIn("still enabled", err + out)
+ self.assertNotIn("org.droidtv.xtv is still", err + out)
+
def test_a_refused_port_is_the_tv_answering(self):
# Nothing listens on 1925, which is what a disabled org.droidtv.xtv looks like: the set itself
# sent the reset, so the package is worth naming even though 1926 stayed silent.
@@ -213,9 +231,43 @@ class AnAddressThisCannotProbe(unittest.TestCase):
self.assertEqual(code, 2)
self.assertEqual(["https://[fe80::1]:1926/6/system", "http://[fe80::1]:1925/6/system"], asked)
+ def test_a_colon_with_nothing_after_it_is_named(self):
+ code, out, asked, err = run({}, argv=("philips-api.py", "tv.example:"))
+ self.assertEqual(code, 2)
+ self.assertEqual(asked, [], "a URL built from a trailing colon was asked: %s" % asked)
+ self.assertEqual(out, "")
+ self.assertIn("tv.example: ends in a colon with nothing after it", err)
+ self.assertIn("pass the TV's address alone: tv.example.", err)
+ self.assertNotIn("nothing answered", err)
+ self.assertNotIn("off, asleep", err)
+
+ def test_a_port_that_is_not_a_number_is_named(self):
+ # TV_HOST=192.168.1.50:abc is the shape tools/tv.py refuses as `not a port number`, so an owner
+ # who mistyped it there had no reason to read this check's answer as the argument's fault either.
+ for host in ("tv.example:abc", "tv.example:1926x", "192.168.1.50: ", "tv.example:-1"):
+ with self.subTest(host=host):
+ code, out, asked, err = run({}, argv=("philips-api.py", host))
+ self.assertEqual(code, 2)
+ self.assertEqual(asked, [])
+ self.assertEqual(out, "")
+ self.assertIn(f"what follows the colon in {host} is", err)
+ self.assertIn("not a port number", err)
+ self.assertNotIn("off, asleep", err)
+
+ def test_an_ipv6_address_written_without_brackets_is_named(self):
+ # `https://fe80::1:1926/6/system` is not a URL: the address has to be bracketed before a port
+ # can follow it, which is the form tools/tv.py documents for IPv6 endpoints.
+ code, out, asked, err = run({}, argv=("philips-api.py", "fe80::1"))
+ self.assertEqual(code, 2)
+ self.assertEqual(asked, [])
+ self.assertEqual(out, "")
+ self.assertIn("fe80::1 is an IPv6 address written without brackets", err)
+ self.assertIn("pass it with brackets", err)
+ self.assertIn("[fe80::1]", err)
+ self.assertNotIn("off, asleep", err)
+
def test_the_guard_reads_only_a_port_after_the_address(self):
- for host in ("192.168.1.50", "192.168.1.50.", "tv.local", "fe80::1", "[fe80::1]",
- "tv-1.local"):
+ for host in ("192.168.1.50", "192.168.1.50.", "tv.local", "[fe80::1]", "tv-1.local"):
with self.subTest(host=host):
self.assertIsNone(philips_api.address_problem(host))
for host in ("192.168.1.50:1926", "tv.local:5555", "[fe80::1]:1926"):
@@ -224,6 +276,16 @@ class AnAddressThisCannotProbe(unittest.TestCase):
for host in ("http://192.168.1.50", "https://tv.local", "HTTP://192.168.1.50"):
with self.subTest(host=host):
self.assertIn("is a URL", philips_api.address_problem(host))
+ self.assertIn("pass the TV's address alone: [fe80::1].",
+ philips_api.address_problem("[fe80::1]:1926"))
+
+ def test_the_splitter_keeps_a_bracketed_address_whole(self):
+ self.assertEqual(("[fe80::1]", None), philips_api.split_address("[fe80::1]"))
+ self.assertEqual(("[fe80::1]", "1926"), philips_api.split_address("[fe80::1]:1926"))
+ self.assertEqual(("tv.local", "5555"), philips_api.split_address("tv.local:5555"))
+ self.assertEqual(("tv.local", ""), philips_api.split_address("tv.local:"))
+ self.assertEqual(("fe80::1", None), philips_api.split_address("fe80::1"))
+ self.assertEqual(("192.168.1.50", None), philips_api.split_address("192.168.1.50"))
class Answered(unittest.TestCase):
@@ -239,7 +301,9 @@ class Answered(unittest.TestCase):
for failure in (TIMEOUT, urllib.error.URLError(TIMEOUT), NO_ROUTE, urllib.error.URLError(NO_ROUTE),
NO_NAME, urllib.error.URLError(NO_NAME),
PermissionError(errno.EACCES, "Permission denied"),
- urllib.error.URLError(PermissionError(errno.EPERM, "Operation not permitted"))):
+ urllib.error.URLError(PermissionError(errno.EPERM, "Operation not permitted")),
+ OSError(errno.EINVAL, "Invalid argument"),
+ urllib.error.URLError(OSError(errno.EINVAL, "Invalid argument"))):
with self.subTest(failure=type(failure).__name__ + str(getattr(failure, "reason", ""))):
self.assertFalse(philips_api.answered(failure))
diff --git a/tools/philips-api.py b/tools/philips-api.py
index f6b8c5978..8aeb6ec00 100644
--- a/tools/philips-api.py
+++ b/tools/philips-api.py
@@ -5,12 +5,15 @@
Exits 0 when the API answers; 1 when a port answered and it is not the JointSpace API (the package to
check is named); and 2 when this run could not tell - nothing answered on either port, so the TV may be
-off, asleep, at another address or out of reach from here - or the command line was not one address: a
-URL, or an address carrying a port of its own, since the two ports this asks are the ones below.
+off, asleep, at another address or out of reach from here - or the command line was not one address:
+a URL; an address carrying a port of its own; one whose colon has nothing after it, or something that
+is not a port number; or an IPv6 written without its brackets. The two ports this asks are the ones
+below, and they go after an address and nothing else.
A port that refuses the connection is the TV answering: nothing listens there, which is what a disabled
-org.droidtv.xtv looks like. A port that times out, answers "No route to host", or a name that does not
-resolve is the network between here and the TV, and says nothing about the package.
+org.droidtv.xtv looks like. A port that times out, answers "No route to host", a name that does not
+resolve, or an address this computer cannot send to at all (a link-local IPv6 with no zone index) is
+the network between here and the TV, and says nothing about the package.
Philips Android TVs are driven from Home Assistant (the built-in philips_js integration), openHAB and
the like over the JointSpace API. It is served by org.droidtv.xtv, which is in the DO NOT DISABLE block
@@ -46,16 +49,20 @@ PORTS = ((1926, "https"), (1925, "http"))
# mistake in the address - and an owner whose TV_HOST is set that way (the form tools/tv.py documents)
# went looking at the set instead of at the argument. The port in TV_HOST is the ADB port and means
# nothing to JointSpace, which this finds on 1926 and 1925 by itself, so the address alone is what the
-# check takes.
-HOST_PORT = re.compile(r"^(?:\[[^\]]+\]|[^:\[\]]+):\d+$")
+# check takes. The same goes for the rest of what a colon can carry: `tv.example:` and
+# `tv.example:1926x` build names that do not resolve either, and a bare `fe80::1` has to be bracketed
+# before it can sit in front of a port. split_address() reads the shape and address_problem() names it.
URL_SCHEME = re.compile(r"^[A-Za-z][A-Za-z0-9+.-]*://")
PROBE = ("ambilight/topology", "ambilight/power", "ambilight/mode", "ambilight/currentconfiguration",
"powerstate")
# errno values that mean this computer never reached the TV: a timeout, a network that is not the
# set's, a host that is gone, or a local permission or sandbox denial that stopped the connect here.
-# What the port said in those cases is about the route, not the TV.
+# EINVAL is the address rather than the route - a bracketed link-local IPv6 with no zone index, an
+# address family this computer does not have - and it is answered here too, because it comes from the
+# local socket layer and never from the set: what the port said in those cases is about the route, not
+# the TV.
UNREACHED = (errno.EHOSTUNREACH, errno.ENETUNREACH, errno.EHOSTDOWN, errno.ENETDOWN, errno.ETIMEDOUT,
- errno.ECONNABORTED, errno.EACCES, errno.EPERM)
+ errno.ECONNABORTED, errno.EACCES, errno.EPERM, errno.EINVAL)
def answered(failure):
@@ -126,23 +133,52 @@ def find_api(host):
return None, None, reached
+def split_address(text):
+ """(address, what follows it) the way tools/tv.py splits a TV_HOST: [ipv6]:port or host:port.
+
+ `what follows it` is None when nothing does, else the text after the colon - which is the port in
+ the form tv.py documents, and can be anything else a reader typed.
+ """
+ if text.startswith("["):
+ end = text.find("]")
+ if end != -1:
+ rest = text[end + 1:]
+ return text[:end + 1], (rest[1:] if rest.startswith(":") else None)
+ if text.count(":") == 1:
+ return tuple(text.split(":", 1))
+ return text, None
+
+
def address_problem(host):
"""Why `host` is not an address this check can probe, or None.
tv.py's TV_HOST takes IP:port, and that port is the ADB one; a JointSpace API is on 1926 (Android
- TV) or 1925 (older sets), which find_api() tries itself. A URL is the other copy-paste shape.
+ TV) or 1925 (older sets), which find_api() tries itself. A URL is the other copy-paste shape. Both
+ are named rather than probed, and so is anything else that leaves the host unusable in the URLs
+ below - a colon with nothing after it, a port that is not a number, an IPv6 without its brackets -
+ because each of them builds a name that does not resolve, and the run then answered "nothing
+ answered on 1926 and 1925, the set may be off": a diagnosis of the TV's network for an argument it
+ could not use.
"""
if URL_SCHEME.match(host):
return (f"{host} is a URL, not the TV's address: this check asks the address itself on "
f"{PORTS[0][0]} and {PORTS[1][0]}, so pass it alone, without http:// or https://.")
- port = HOST_PORT.match(host)
- if port:
- address = host.rsplit(":", 1)[0]
- return (f"{host} carries a port, and this check asks the address itself on {PORTS[0][0]} and "
- f"{PORTS[1][0]}, so pass the TV's address alone: {address}.\n"
- f"TV_HOST is where that form comes from - tools/tv.py takes IP:port for its ADB "
- f"connection, and that port is the ADB port, not a JointSpace one.")
- return None
+ address, rest = split_address(host)
+ if rest is None:
+ if ":" in address and not address.startswith("["):
+ return (f"{host} is an IPv6 address written without brackets, so the URLs this asks are not "
+ f"ones: pass it with brackets, the way tools/tv.py takes one: [{host}].")
+ return None
+ if not rest:
+ return (f"{host} ends in a colon with nothing after it, so it is not an address: pass the TV's "
+ f"address alone: {address}.")
+ if not rest.isdigit():
+ return (f"what follows the colon in {host} is {rest!r}, not a port number, so this is not an "
+ f"address the check can ask: pass the TV's address alone: {address}.")
+ return (f"{host} carries a port, and this check asks the address itself on {PORTS[0][0]} and "
+ f"{PORTS[1][0]}, so pass the TV's address alone: {address}.\n"
+ f"TV_HOST is where that form comes from - tools/tv.py takes IP:port for its ADB "
+ f"connection, and that port is the ADB port, not a JointSpace one.")
def main(argv):