Fix usage of internal get and set methods

This commit is contained in:
Evan Fiordeliso 2023-01-30 11:58:02 -05:00
parent 93949ec0e9
commit 5820f837a5
3 changed files with 57 additions and 69 deletions

View File

@ -59,8 +59,9 @@ class Flickerstrip:
return False return False
async def __get_request(self, path: str, async def __get_request(self, path: str,
params: Optional[Mapping[str, str]] = None params: Optional[Mapping[str, str]] = None,
) -> aiohttp.ClientResponse: return_body: bool = False,
) -> bool | bytes:
"""Send a GET request to the strip. """Send a GET request to the strip.
Args: Args:
@ -74,13 +75,16 @@ class Flickerstrip:
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(f"http://{self.host}:{self.port}/{path}", async with session.get(f"http://{self.host}:{self.port}/{path}",
params=params) as response: params=params) as response:
return response if return_body:
return await response.read()
else:
return await self.__handle_response(response)
async def __post_request(self, path, async def __post_request(self, path,
params: Optional[Mapping[str, str]] = None, params: Optional[Mapping[str, str]] = None,
json: Any = None, json: Any = None,
data: Any = None data: Any = None
) -> aiohttp.ClientResponse: ) -> bool:
"""Send a POST request to the strip. """Send a POST request to the strip.
Args: Args:
@ -98,54 +102,49 @@ class Flickerstrip:
async with session.post(f"http://{self.host}:{self.port}/{path}", async with session.post(f"http://{self.host}:{self.port}/{path}",
params=params, json=json, data=data params=params, json=json, data=data
) as response: ) as response:
return response return await self.__handle_response(response)
async def force_update(self): async def force_update(self) -> bool:
"""Force updates the status of the strip. """Force updates the status of the strip.
Returns: Returns:
bool: If the request was succesful bool: If the request was succesful
""" """
resp = await self.__get_request("status") return bool(await self.__get_request("status"))
return await self.__handle_response(resp)
async def power_on(self): async def power_on(self) -> bool:
"""Trigger the strip to turn the lights on. """Trigger the strip to turn the lights on.
Returns: Returns:
bool: If the request was succesful bool: If the request was succesful
""" """
resp = await self.__get_request("power/on") return bool(await self.__get_request("power/on"))
return await self.__handle_response(resp)
async def power_off(self): async def power_off(self) -> bool:
"""Trigger the strip to turn the lights off. """Trigger the strip to turn the lights off.
Returns: Returns:
bool: If the request was succesful bool: If the request was succesful
""" """
resp = await self.__get_request("power/off") return bool(await self.__get_request("power/off"))
return await self.__handle_response(resp)
async def power_toggle(self): async def power_toggle(self) -> bool:
"""Trigger the strip to toggle the power status. """Trigger the strip to toggle the power status.
Returns: Returns:
bool: If the request was succesful bool: If the request was succesful
""" """
resp = await self.__get_request("power/toggle") return bool(await self.__get_request("power/toggle"))
return await self.__handle_response(resp)
async def next_pattern(self): async def next_pattern(self) -> bool:
"""Trigger the strip to switch to the next pattern in memory. """Trigger the strip to switch to the next pattern in memory.
Returns: Returns:
bool: If the request was succesful bool: If the request was succesful
""" """
resp = await self.__get_request("pattern/next") return bool(await self.__get_request("pattern/next"))
return await self.__handle_response(resp)
async def freeze_frame(self, frame: int): async def freeze_frame(self, frame: int) -> bool:
"""Freeze the animation at the specified frame. """Freeze the animation at the specified frame.
Args: Args:
@ -155,8 +154,7 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
params = {"value": str(frame)} params = {"value": str(frame)}
resp = await self.__get_request("pattern/frame", params=params) return bool(await self.__get_request("pattern/frame", params=params))
return await self.__handle_response(resp)
async def download_pattern(self, id: int) -> PatternBuilder | None: async def download_pattern(self, id: int) -> PatternBuilder | None:
"""Download a pattern by its id """Download a pattern by its id
@ -175,14 +173,12 @@ class Flickerstrip:
return None return None
params = {"id": str(id)} params = {"id": str(id)}
resp = await self.__get_request("pattern/download", params=params) data = bytes(await self.__get_request("pattern/download",
if resp.status != 200: params=params, return_body=True))
return None
data = await resp.read()
return PatternBuilder.from_binary_string(data, meta) return PatternBuilder.from_binary_string(data, meta)
async def select_pattern(self, index: int): async def select_pattern(self, index: int) -> bool:
"""Change the current pattern to the pattern at the specified index. """Change the current pattern to the pattern at the specified index.
Args: Args:
@ -192,13 +188,12 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
params = {"index": str(index)} params = {"index": str(index)}
resp = await self.__get_request("pattern/select", params=params) return bool(await self.__get_request("pattern/select", params=params))
return await self.__handle_response(resp)
async def delete_pattern(self, async def delete_pattern(self,
pattern: Optional[PatternMeta] = None, pattern: Optional[PatternMeta] = None,
index: Optional[int] = None, index: Optional[int] = None,
id: Optional[int] = None): id: Optional[int] = None) -> bool:
"""Delete a pattern by its meta, index, or id """Delete a pattern by its meta, index, or id
Args: Args:
@ -216,17 +211,20 @@ class Flickerstrip:
return await self.delete_pattern(id=pattern.id) return await self.delete_pattern(id=pattern.id)
elif index is not None: elif index is not None:
params = {"index": str(index)} params = {"index": str(index)}
resp = await self.__get_request("pattern/forget", params=params) return bool(await self.__get_request("pattern/forget",
return await self.__handle_response(resp) params=params))
elif id is not None: elif id is not None:
params = {"id": str(id)} params = {"id": str(id)}
resp = await self.__get_request("pattern/forget", params=params) return bool(await self.__get_request("pattern/forget",
return await self.__handle_response(resp) params=params))
else: else:
raise TypeError( raise TypeError(
"Deleting a pattern requires one of the three args.") "Deleting a pattern requires one of the three args.")
async def create_pattern(self, pattern: PatternBuilder, preview=True): async def create_pattern(self,
pattern: PatternBuilder,
preview=True
) -> bool:
"""Create a pattern on the flickerstrip. """Create a pattern on the flickerstrip.
Args: Args:
@ -245,11 +243,10 @@ class Flickerstrip:
"preview": str(1 if preview else 0) "preview": str(1 if preview else 0)
} }
data = pattern.to_binary_string() data = pattern.to_binary_string()
resp = await self.__post_request("pattern/create", return await self.__post_request("pattern/create",
params=params, data=data) params=params, data=data)
return await self.__handle_response(resp)
async def set_color(self, r: int, g: int, b: int): async def set_color(self, r: int, g: int, b: int) -> bool:
"""Sets the strip to a solid color (given as an RGB value). """Sets the strip to a solid color (given as an RGB value).
Args: Args:
@ -264,7 +261,7 @@ class Flickerstrip:
pattern.add_pixel(r, g, b) pattern.add_pixel(r, g, b)
return await self.create_pattern(pattern) return await self.create_pattern(pattern)
async def set_brightness(self, value: int): async def set_brightness(self, value: int) -> bool:
"""Set the brightness of the flickerstrip. """Set the brightness of the flickerstrip.
Args: Args:
@ -274,10 +271,9 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
params = {"value": str(value)} params = {"value": str(value)}
resp = await self.__get_request("brightness", params=params) return bool(await self.__get_request("brightness", params=params))
return await self.__handle_response(resp)
async def set_name(self, value: str): async def set_name(self, value: str) -> bool:
"""Set the name of the flickerstrip. """Set the name of the flickerstrip.
Args: Args:
@ -287,10 +283,9 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
json = {"name": value} json = {"name": value}
resp = await self.__post_request("config/name", json=json) return bool(await self.__post_request("config/name", json=json))
return await self.__handle_response(resp)
async def set_group(self, value: str): async def set_group(self, value: str) -> bool:
"""Set the group of the flickerstrip. """Set the group of the flickerstrip.
Args: Args:
@ -300,10 +295,9 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
json = {"name": value} json = {"name": value}
resp = await self.__post_request("config/group", json=json) return bool(await self.__post_request("config/group", json=json))
return await self.__handle_response(resp)
async def set_cycle(self, value): async def set_cycle(self, value: int) -> bool:
"""Set the cycle timer of the flickerstrip. """Set the cycle timer of the flickerstrip.
Args: Args:
@ -313,11 +307,10 @@ class Flickerstrip:
Returns: Returns:
bool: If the request was succesful bool: If the request was succesful
""" """
params = {"value": value} params = {"value": str(value)}
resp = await self.__get_request("config/cycle", params=params) return bool(await self.__get_request("config/cycle", params=params))
return await self.__handle_response(resp)
async def set_fade_duration(self, value: int): async def set_fade_duration(self, value: int) -> bool:
"""Set the fade timer of the flickerstrip. """Set the fade timer of the flickerstrip.
Args: Args:
@ -328,10 +321,9 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
params = {"value": str(value)} params = {"value": str(value)}
resp = await self.__get_request("config/fade", params=params) return bool(await self.__get_request("config/fade", params=params))
return await self.__handle_response(resp)
async def set_strip_length(self, value: int): async def set_strip_length(self, value: int) -> bool:
"""Set the length of the flickerstrip. """Set the length of the flickerstrip.
Args: Args:
@ -341,10 +333,9 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
params = {"value": str(value)} params = {"value": str(value)}
resp = await self.__get_request("config/length", params=params) return bool(await self.__get_request("config/length", params=params))
return await self.__handle_response(resp)
async def set_strip_start(self, value: int): async def set_strip_start(self, value: int) -> bool:
"""Set the start pixel of the strip. """Set the start pixel of the strip.
This will make the flickerstrip ignore all pixels before This will make the flickerstrip ignore all pixels before
@ -357,10 +348,9 @@ class Flickerstrip:
bool: If the request was successful. bool: If the request was successful.
""" """
params = {"value": str(value)} params = {"value": str(value)}
resp = await self.__get_request("config/start", params=params) return bool(await self.__get_request("config/start", params=params))
return await self.__handle_response(resp)
async def set_strip_end(self, value: int): async def set_strip_end(self, value: int) -> bool:
"""Set the end pixel of the strip. """Set the end pixel of the strip.
This will make the flickerstrip ignore all pixels after This will make the flickerstrip ignore all pixels after
@ -373,10 +363,9 @@ class Flickerstrip:
bool: If the request was successful. bool: If the request was successful.
""" """
params = {"value": str(value)} params = {"value": str(value)}
resp = await self.__get_request("config/end", params=params) return bool(await self.__get_request("config/end", params=params))
return await self.__handle_response(resp)
async def set_reversed(self, value: bool): async def set_reversed(self, value: bool) -> bool:
"""Set the reversed state of the flickerstrip. """Set the reversed state of the flickerstrip.
Args: Args:
@ -387,5 +376,4 @@ class Flickerstrip:
bool: If the request was succesful bool: If the request was succesful
""" """
params = {"value": str(1 if value else 0)} params = {"value": str(1 if value else 0)}
resp = await self.__get_request("config/reversed", params=params) return bool(await self.__get_request("config/reversed", params=params))
return await self.__handle_response(resp)

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "flickerstrip-py" name = "flickerstrip-py"
version = "0.2.2" version = "0.2.3"
description = "" description = ""
authors = ["Evan Fiordeliso <evan.fiordeliso@gmail.com>"] authors = ["Evan Fiordeliso <evan.fiordeliso@gmail.com>"]

View File

@ -3,7 +3,7 @@ from setuptools import find_packages, setup
setup( setup(
name='flickerstrip-py', name='flickerstrip-py',
packages=find_packages(), packages=find_packages(),
version='0.2.2', version='0.2.3',
description='A python library for interracting with a flickerstrip.', description='A python library for interracting with a flickerstrip.',
author='Evan Fiordeliso <evan.fiordeliso@gmail.com>', author='Evan Fiordeliso <evan.fiordeliso@gmail.com>',
license='MIT', license='MIT',