bundles/infobeamer-monitor: some more improvements in status display

This commit is contained in:
Franzi 2024-11-28 08:27:36 +01:00
parent 8e23747400
commit 128a61706e
Signed by: kunsi
GPG key ID: 12E3D2136B818350

View file

@ -25,7 +25,8 @@ logging.basicConfig(
)
LOG = logging.getLogger("main")
MLOG = logging.getLogger("mqtt")
TZ = ZoneInfo("Europe/Berlin")
DUMP_TIME = "0900"
state = None
@ -68,13 +69,12 @@ def mqtt_dump_state(device):
device=device,
)
def is_dump_time():
return datetime.now(TZ).strftime("%H%M") == DUMP_TIME
mqtt_out("Monitor starting up")
while True:
try:
online_devices = set()
available_credits = None
try:
r = get(
"https://info-beamer.com/api/v1/device/list",
@ -83,9 +83,9 @@ while True:
r.raise_for_status()
ib_state = r.json()["devices"]
except RequestException as e:
LOG.exception("Could not get data from info-beamer")
LOG.exception("Could not get device data from info-beamer")
mqtt_out(
f"Could not get data from info-beamer: {e!r}",
f"Could not get device data from info-beamer: {e!r}",
level="WARN",
)
else:
@ -98,7 +98,8 @@ while True:
continue
new_state[did] = device
must_dump_state = False
# force information output for every online device at 09:00 CE(S)T
must_dump_state = is_dump_time()
if state is not None:
if did not in state:
@ -161,14 +162,6 @@ while True:
else:
LOG.info("adding device {} to empty state".format(device["id"]))
if device["is_online"]:
online_devices.add(
"{} ({})".format(
device["id"],
device["description"],
)
)
state = new_state
try:
@ -179,13 +172,16 @@ while True:
r.raise_for_status()
ib_account = r.json()
except RequestException as e:
LOG.exception("Could not get data from info-beamer")
LOG.exception("Could not get account data from info-beamer")
mqtt_out(
f"Could not get data from info-beamer: {e!r}",
f"Could not get account data from info-beamer: {e!r}",
level="WARN",
)
else:
available_credits = ib_account["balance"]
if is_dump_time():
mqtt_out(f"Available Credits: {available_credits}")
if available_credits < 50:
mqtt_out(
f"balance has dropped below 50 credits! (available: {available_credits})",
@ -211,15 +207,6 @@ while True:
level="WARN",
)
if datetime.now(ZoneInfo("Europe/Berlin")).strftime("%H%M") == "0900":
if available_credits is not None:
mqtt_out(f"Available Credits: {available_credits}")
if online_devices:
mqtt_out(
"Online Devices: {}".format(", ".join(sorted(online_devices)))
)
sleep(60)
except KeyboardInterrupt:
break