Add get-state command

This commit is contained in:
Torjus Håkestad 2024-08-22 21:31:43 +02:00
parent fdf7e5c642
commit 455b3c5f3c
2 changed files with 26 additions and 1 deletions

View File

@ -6,6 +6,7 @@ from typing_extensions import Annotated
import typer import typer
import json import json
import paho.mqtt.publish as pub import paho.mqtt.publish as pub
import paho.mqtt.subscribe as sub
class LightState(str, Enum): class LightState(str, Enum):
@ -185,6 +186,30 @@ def set_brightness(
payload = json.dumps({"brightness": brightness}) payload = json.dumps({"brightness": brightness})
pub.single(topic, payload, hostname=get_mqtt_broker()) 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() @app.command()
def version(): def version():

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "huecli" name = "huecli"
version = "0.1.4" version = "0.1.5"
description = "Set Philips Hue lights using MQTT" description = "Set Philips Hue lights using MQTT"
authors = ["Torjus Håkestad <torjus@uio.no>"] authors = ["Torjus Håkestad <torjus@uio.no>"]
license = "MIT" license = "MIT"