From 455b3c5f3c67844b4309e6ec03af2b74f2b4b09e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torjus=20H=C3=A5kestad?= Date: Thu, 22 Aug 2024 21:31:43 +0200 Subject: [PATCH] Add get-state command --- huecli/__main__.py | 25 +++++++++++++++++++++++++ pyproject.toml | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/huecli/__main__.py b/huecli/__main__.py index f3883e9..5cf058d 100644 --- a/huecli/__main__.py +++ b/huecli/__main__.py @@ -6,6 +6,7 @@ from typing_extensions import Annotated import typer import json import paho.mqtt.publish as pub +import paho.mqtt.subscribe as sub class LightState(str, Enum): @@ -185,6 +186,30 @@ def set_brightness( payload = json.dumps({"brightness": brightness}) pub.single(topic, payload, hostname=get_mqtt_broker()) +@app.command() +@app.command("s", hidden=True) +def get_state( + id: Annotated[ + LightID, typer.Argument(help="ID of light.", autocompletion=complete_id) + ], + as_json: Annotated[ + bool, typer.Option("--json", help="Print state as json."), + ] = False + ): + """ + Get the state of ID + """ + str_id = NAME_TO_ID[id] + topic = f"zigbee2mqtt/{str_id}/get" + resp_topic = f"zigbee2mqtt/{str_id}" + payload = json.dumps({"state": ""}) + pub.single(topic, payload, hostname=get_mqtt_broker()) + resp = sub.simple(resp_topic, hostname=get_mqtt_broker()) + data = json.loads(resp.payload.decode("utf-8")) + if as_json: + print(json.dumps(data)) + else: + print(f"{data['state']}") @app.command() def version(): diff --git a/pyproject.toml b/pyproject.toml index 265c07d..4dca7b7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "huecli" -version = "0.1.4" +version = "0.1.5" description = "Set Philips Hue lights using MQTT" authors = ["Torjus HÃ¥kestad "] license = "MIT"