flickerstrip-py/flickerstrip_py/flickerstrip.py

254 lines
7.5 KiB
Python

import requests
from status import Status
from pattern import PatternMeta
class Flickerstrip:
def __init__(self, ip_address):
"""The initializer for the Flickerstrip class.
Args:
ip_address (string): The ip address of the flickerstrip.
"""
self.ip_address = ip_address
self.status = None
def __check_response(self, response):
"""Check if the request was succesful.
Args:
response (requests.Response): The response from the request.
Returns:
boolean: If the request was successful
"""
data = response.json()
respType = data["type"]
if respType == "error":
message = data["message"]
print(f"ERROR: Request failed with message \"{message}\".")
return False
elif respType == "OK":
return True
elif respType == "status":
self.status = Status.from_json(data)
return True
else:
print(f"ERROR: Unrecognized response type: {respType}.")
return False
def __get_request(self, path, params=None):
"""Send a GET request to the strip.
Args:
path (string): The path for the URL to request.
params (dictionary, optional): The query string params for the
request. Defaults to None.
Returns:
boolean: If the request was successful
"""
resp = requests.get(f"http://{self.ip_address}/{path}", params=params)
return self.__check_response(resp)
def __post_request(self, path, json=None, data=None):
"""Send a POST request to the strip.
Args:
path (string): The path for the URL to request.
json (dictionary, optional): The JSON encodable body for the
request. Defaults to None.
data (string, optional): The raw data body. Defaults to None.
Returns:
boolean: If the request was successful
"""
resp = requests.post(f"http://{self.ip_address}/{path}",
json=json, data=data)
return self.__check_response(resp)
def force_update(self):
"""Force updates the status of the strip.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("status")
def power_on(self):
"""Trigger the strip to turn the lights on.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("power/on")
def power_off(self):
"""Trigger the strip to turn the lights off.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("power/off")
def power_toggle(self):
"""Trigger the strip to toggle the power status.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("power/toggle")
def next_pattern(self):
"""Trigger the strip to switch to the next pattern in memory.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("pattern/next")
def freeze_frame(self, frame):
"""Freeze the animation at the specified frame.
Args:
frame (int): The frame number to freeze on.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("pattern/frame", {"value": frame})
def select_pattern(self, index):
"""Change the current pattern to the pattern at the specified index.
Args:
index (int): The index of the pattern.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("pattern/select", {"index": index})
def delete_pattern(self, pattern=None, index=None, id=None):
if pattern is not None:
if not isinstance(pattern, PatternMeta):
raise TypeError(
"Expected a PatternMeta object for the pattern arg.")
return self.delete_pattern(id=pattern.id)
elif index is not None:
return self.__get_request("pattern/forget", {"index": index})
elif id is not None:
return self.__get_request("pattern/forget", {"id": id})
else:
raise TypeError(
"Deleting a pattern requires one of the three args.")
def set_brightness(self, value):
"""Set the brightness of the flickerstrip.
Args:
value (int): The brightness level as an int from 0 to 100.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("brightness", {"value": value})
def set_name(self, value):
"""Set the name of the flickerstrip.
Args:
value (string): The new name.
Returns:
boolean: If the request was succesful
"""
return self.__post_request("config/name", {"name": value})
def set_group(self, value):
"""Set the group of the flickerstrip.
Args:
value (string): The new group.
Returns:
boolean: If the request was succesful
"""
return self.__post_request("config/group", {"name": value})
def set_cycle(self, value):
"""Set the cycle timer of the flickerstrip.
Args:
value (int): How long to cycle to next pattern.
Value is handled in seconds.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("config/cycle", {"value": value})
def set_fade_duration(self, value):
"""Set the fade timer of the flickerstrip.
Args:
value (int): How long to fade to next pattern.
Value is handled in seconds.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("config/fade", {"value": value})
def set_strip_length(self, value):
"""Set the length of the flickerstrip.
Args:
value (int): The length of the strip in pixels.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("config/length", {"value": value})
def set_strip_start(self, value):
"""Set the start pixel of the strip.
This will make the flickerstrip ignore all pixels before
the given number, set to 0 for the first pixel.
Args:
value (int): The new first pixel of the strip.
Returns:
boolean: If the request was successful.
"""
return self.__get_request("config/start", {"value": value})
def set_strip_end(self, value):
"""Set the end pixel of the strip.
This will make the flickerstrip ignore all pixels after
the given number, set to -1 for the last pixel.
Args:
value (int): The new last pixel of the strip.
Returns:
boolean: If the request was successful.
"""
return self.__get_request("config/end", {"value": value})
def set_reversed(self, value):
"""Set the reversed state of the flickerstrip.
Args:
value (boolean): If the flickerstrip should animate
patterns in reverse.
Returns:
boolean: If the request was succesful
"""
return self.__get_request("config/reversed",
{"value": 1 if value else 0})