Skip to content

Instantly share code, notes, and snippets.

@theodric
Created January 1, 2025 13:47
Show Gist options
  • Save theodric/9082b3389c240c084fc4fa78ef6c338e to your computer and use it in GitHub Desktop.
Save theodric/9082b3389c240c084fc4fa78ef6c338e to your computer and use it in GitHub Desktop.
Python script to toggle the state of a Philips Hue smart socket using "openhue" (https://www.openhue.io/cli/openhue-cli). I use this in combination with the plasmoid "Run Command" (https://himdek.com/Run-Command-Widget-for-Plasma/) to turn my ThinkPad charging brick on and off.
#!/usr/bin/env python3
import os
import subprocess
# Define the commands
cmd_on = "openhue set light 626d5ac8-dd60-4eaa-9fa7-f0d0eb31ec15 --on"
cmd_off = "openhue set light 626d5ac8-dd60-4eaa-9fa7-f0d0eb31ec15 --off"
get_cmd = "openhue get light 626d5ac8-dd60-4eaa-9fa7-f0d0eb31ec15"
# Function to get the current status of the light
def get_light_status():
result = subprocess.run(get_cmd, shell=True, capture_output=True, text=True)
if result.returncode != 0:
raise Exception("Failed to get light status")
output = result.stdout
if "[on]" in output:
return "on"
elif "[ ]" in output:
return "off"
else:
raise Exception("Unknown light status")
# Main function to toggle the command
def toggle_command():
current_status = get_light_status()
if current_status == "on":
command = cmd_off
else:
command = cmd_on
# Execute the command
subprocess.run(command, shell=True)
# Run the toggle command function
if __name__ == "__main__":
toggle_command()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment