From 4d803ec9e59501a4274220b090dcd9daee6bd9fd Mon Sep 17 00:00:00 2001 From: lie_ink Date: Sat, 16 Jul 2022 19:39:22 +0200 Subject: [PATCH] power: add support for hue devices Signed-off-by: Oliver Tetz --- docs/configuration.md | 26 ++++++++++++++++++++++++-- moonraker/components/power.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 06df2fc..5f7c7bb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -446,7 +446,7 @@ The following configuration options are available for all power device types: type: # The type of device. Can be either gpio, klipper_device, rf, # tplink_smartplug, tasmota, shelly, homeseer, homeassistant, loxonev1, -# smartthings, or mqtt. +# smartthings, mqtt or hue. # This parameter must be provided. off_when_shutdown: False # If set to True the device will be powered off when Klipper enters @@ -1031,6 +1031,28 @@ token: smartthings-bearer-token device: smartthings-device-id ``` +#### Hue Device Configuration + +The following options are available for `hue` device types: + +```ini +# moonraker.conf + +address: +# A valid ip address or hostname of the Philips Hue Bridge. This +# parameter must be provided. +user: +# The api key used for request authorization. This option accepts +# Jinja2 Templates, see the [secrets] section for details. +# An explanation how to get the api key can be found here: +# https://developers.meethue.com/develop/get-started-2/#so-lets-get-started +device_id: +# The device id of the light/socket you want to control. +# An explanation on how you could get the device id, can be found here: +# https://developers.meethue.com/develop/get-started-2/#turning-a-light-on-and-off + +``` + #### Toggling device state from Klipper It is possible to toggle device power from the Klippy host, this can be done @@ -1544,7 +1566,7 @@ gcode: brightness=brightness, intensity=intensity, speed=speed)} - + [gcode_macro WLED_OFF] description: Turn WLED strip off gcode: diff --git a/moonraker/components/power.py b/moonraker/components/power.py index a9830a4..ef53ede 100644 --- a/moonraker/components/power.py +++ b/moonraker/components/power.py @@ -52,7 +52,8 @@ class PrinterPower: "loxonev1": Loxonev1, "rf": RFDevice, "mqtt": MQTTDevice, - "smartthings": SmartThings + "smartthings": SmartThings, + "hue": HueDevice } for section in prefix_sections: @@ -1287,6 +1288,33 @@ class MQTTDevice(PowerDevice): f"device to state '{state}'", 500) +class HueDevice(HTTPDevice): + + def __init__(self, config: ConfigHelper) -> None: + super().__init__(config) + self.device_id = config.get("device_id") + + async def _send_power_request(self, state: str) -> str: + new_state = True if state == "on" else False + url = f"http://{self.addr}/api" \ + f"/{self.user}/lights" \ + f"/{self.device_id}/state" + url = self.client.escape_url(url) + ret = await self.client.request("PUT", + url, + body={"on": new_state}) + return "on" if \ + ret.json()[0].get("success")[f"/lights/{self.device_id}/state/on"] \ + else "off" + + async def _send_status_request(self) -> str: + ret = await self.client.request("GET", + f"http://{self.addr}/api/" + f"{self.user}/lights/" + f"{self.device_id}") + return "on" if ret.json().get("state")["on"] else "off" + + # The power component has multiple configuration sections def load_component(config: ConfigHelper) -> PrinterPower: return PrinterPower(config)