aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xbin/udt-bulb35
1 files changed, 35 insertions, 0 deletions
diff --git a/bin/udt-bulb b/bin/udt-bulb
index b2eddf5..eee17bc 100755
--- a/bin/udt-bulb
+++ b/bin/udt-bulb
@@ -30,6 +30,7 @@ import secrets
import shutil
import subprocess
import sys
+import time
from pathlib import Path
CONFIG = Path.home() / ".config/udt/tradfri.json"
@@ -65,6 +66,22 @@ def coap(method, url, ident, key, payload=None):
return out.strip()
+def wait_for_gateway(url, auth, seconds, now=time.monotonic, nap=time.sleep):
+ """True once a get on url answers, False if it has not by `seconds`.
+
+ For `on`, which runs straight after a resume and at login: the wired link
+ came up 4 s after the resume finished, and a request sent before that
+ fails at once, with nothing to try again. `off` never waits, since it runs
+ as the session ends.
+ """
+ end = now() + seconds
+ while coap("get", url, *auth) is None:
+ if now() >= end:
+ return False
+ nap(2)
+ return True
+
+
def light_states(colour):
"""3311 states for #rrggbb, sent in order."""
h, s, _ = colorsys.rgb_to_hsv(*(int(colour[i:i + 2], 16) / 255 for i in (1, 3, 5)))
@@ -102,6 +119,20 @@ def selftest():
# A pastel keeps its hue and loses its paleness to the floor.
colour = light_states("#8080ff")[0]
assert (colour["5707"], colour["5708"]) == (round(HUE_MAX * 2 / 3), HUE_MAX)
+
+ # The wait for the gateway retries until it answers, and gives up at the
+ # deadline. coap and the clock are swapped for fakes.
+ global coap
+ real, answers, naps = coap, [None, None, "{}"], []
+ coap = lambda *a: answers.pop(0)
+ clock = iter(range(100))
+ try:
+ assert wait_for_gateway("u", ("i", "k"), 30, lambda: next(clock), naps.append)
+ assert len(naps) == 2, naps
+ coap = lambda *a: None
+ assert not wait_for_gateway("u", ("i", "k"), 3, lambda: next(clock), naps.append)
+ finally:
+ coap = real
print("ok")
@@ -132,6 +163,10 @@ def main():
else:
return
auth = cfg["identity"], cfg["key"]
+ first = f"coaps://{cfg['host']}:5684/{DEVICES}/{cfg['bulbs'][0]}"
+ if args == ["on"] and not wait_for_gateway(first, auth, 30):
+ print("udt-bulb: the gateway did not answer for 30 s", file=sys.stderr)
+ return
for dev in cfg["bulbs"]:
url = f"coaps://{cfg['host']}:5684/{DEVICES}/{dev}"
if not args: