-
-
Save delfick/5d03f7c63f38e089545d752c4e1e5ee3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import colorsys | |
import requests | |
def hsb_to_hex(hue, saturation, brightness): | |
"""Convert hue/saturation/brightness to hex (#XXXXXX)""" | |
# First we convert our hsb to rgb | |
# Python has a handy standard library function for this | |
# Note that hue is a number between 0 and 360, but we want it between 0 and 1 | |
hue_percentage = hue / 360 | |
red, green, blue = colorsys.hsv_to_rgb(hue_percentage, saturation, brightness) | |
# Now we want red, green and blue as a number between 0 and 255 | |
# They're already a number between 0 and 1, so we can use that as a percentage of 255 | |
red = red * 255 | |
green = green * 255 | |
blue = blue * 255 | |
# Our numbers at this points are floats (i.e. they have a decimal) | |
# So 2.5 is a float, whereas 2 is an integer | |
# For the next step to work, we need to turn our floats into integers | |
red = int(red) | |
green = int(green) | |
blue = int(blue) | |
# Now we can make our hex string | |
# Which we do by printing our values as hex numbers | |
# We can do this with the ":03X" formatter | |
# The X says print the number as a hex string | |
# Note that because we use a capital X, the string will be in capitals | |
# If we said lowercase x then the string would be in lowercase | |
# as in "BA" instead of "ba" | |
# The 02 says pad the string so it's at least 2 characters | |
# So if it's only one character, say "F", then it'll return "0F" | |
return f"#{red:02X}{green:02X}{blue:02X}" | |
def get_result(url, token): | |
""" | |
return the result from making a request to LIFX servers as a python dictionary | |
""" | |
headers = {"Authorization": f"Bearer {token}"} | |
response = requests.get(url, headers=headers) | |
return response.json() | |
def print_devices(result): | |
""" | |
Print information about the devices in our result | |
""" | |
for light in result: | |
# It's possible that some data isn't in the result yet | |
# So we use the "get" function to get the data from the dictionary | |
# With a default value if it's not in the data | |
label = light.get("label", "") | |
power = light.get("power", "off") | |
connected = light.get("connected", False) | |
# Hue, saturation and brightness are in their own dictionary in the result | |
# Hue is the point on the color wheel we are at. So 0 is red, 100 is green, etc | |
# Saturation is how strong the color is. So 0 is white, 1 is full color | |
color = light.get("color", {}) | |
hue = color.get("hue", 0) | |
saturation = color.get("saturation", 0) | |
# Brightness isn't with hue and saturation for some bizarre reason, so we get that from the result itself | |
# Brightness is how light or dark the color is. So 0 is essentially off, and 1 requires sunglasses! | |
brightness = light.get("brightness", 0) | |
# So let's get our hex value | |
hex_color = hsb_to_hex(hue, saturation, brightness) | |
# We are connected if the connected variable is "truthy" | |
# In this case that happens if it's the boolean value "True" | |
if connected: | |
connected_string = "Connected" | |
else: | |
connected_string = "Disconnected" | |
print(f"Name: {label}") | |
print(f"Status: {connected_string}, {power}") | |
print(f"Brightness: {brightness * 100}%") | |
print(f"Color: {hex_color}") | |
print() | |
# So now we get our token | |
# You can hard code into into the script. | |
# Or you can get it from os.environ | |
# Or from sys.argv | |
# Or from the standard library argparse module | |
# Probably best to not learn too many things at once and put it into the script! | |
token = "my amazing token" | |
# Let's get our information from LIFX servers | |
result = get_result("https://api.lifx.com/v1/lights/all", token) | |
# And let's print the information | |
print_devices(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment