power: add support for hue devices

Signed-off-by: Oliver Tetz <olli.codes.some.lines@gmail.com>
This commit is contained in:
lie_ink 2022-07-16 19:39:22 +02:00 committed by Eric Callahan
parent 3080885257
commit 4d803ec9e5
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
2 changed files with 53 additions and 3 deletions

View File

@ -446,7 +446,7 @@ The following configuration options are available for all power device types:
type: type:
# The type of device. Can be either gpio, klipper_device, rf, # The type of device. Can be either gpio, klipper_device, rf,
# tplink_smartplug, tasmota, shelly, homeseer, homeassistant, loxonev1, # tplink_smartplug, tasmota, shelly, homeseer, homeassistant, loxonev1,
# smartthings, or mqtt. # smartthings, mqtt or hue.
# This parameter must be provided. # This parameter must be provided.
off_when_shutdown: False off_when_shutdown: False
# If set to True the device will be powered off when Klipper enters # 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 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 #### Toggling device state from Klipper
It is possible to toggle device power from the Klippy host, this can be done It is possible to toggle device power from the Klippy host, this can be done
@ -1544,7 +1566,7 @@ gcode:
brightness=brightness, brightness=brightness,
intensity=intensity, intensity=intensity,
speed=speed)} speed=speed)}
[gcode_macro WLED_OFF] [gcode_macro WLED_OFF]
description: Turn WLED strip off description: Turn WLED strip off
gcode: gcode:

View File

@ -52,7 +52,8 @@ class PrinterPower:
"loxonev1": Loxonev1, "loxonev1": Loxonev1,
"rf": RFDevice, "rf": RFDevice,
"mqtt": MQTTDevice, "mqtt": MQTTDevice,
"smartthings": SmartThings "smartthings": SmartThings,
"hue": HueDevice
} }
for section in prefix_sections: for section in prefix_sections:
@ -1287,6 +1288,33 @@ class MQTTDevice(PowerDevice):
f"device to state '{state}'", 500) 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 # The power component has multiple configuration sections
def load_component(config: ConfigHelper) -> PrinterPower: def load_component(config: ConfigHelper) -> PrinterPower:
return PrinterPower(config) return PrinterPower(config)