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"