diff --git a/flickerstrip_py/flickerstrip.py b/flickerstrip_py/flickerstrip.py index c3e554e..a1698f3 100644 --- a/flickerstrip_py/flickerstrip.py +++ b/flickerstrip_py/flickerstrip.py @@ -59,8 +59,9 @@ class Flickerstrip: return False async def __get_request(self, path: str, - params: Optional[Mapping[str, str]] = None - ) -> aiohttp.ClientResponse: + params: Optional[Mapping[str, str]] = None, + return_body: bool = False, + ) -> bool | bytes: """Send a GET request to the strip. Args: @@ -74,13 +75,16 @@ class Flickerstrip: async with aiohttp.ClientSession() as session: async with session.get(f"http://{self.host}:{self.port}/{path}", 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, params: Optional[Mapping[str, str]] = None, json: Any = None, data: Any = None - ) -> aiohttp.ClientResponse: + ) -> bool: """Send a POST request to the strip. Args: @@ -98,54 +102,49 @@ class Flickerstrip: async with session.post(f"http://{self.host}:{self.port}/{path}", params=params, json=json, data=data ) 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. Returns: bool: If the request was succesful """ - resp = await self.__get_request("status") - return await self.__handle_response(resp) + return bool(await self.__get_request("status")) - async def power_on(self): + async def power_on(self) -> bool: """Trigger the strip to turn the lights on. Returns: bool: If the request was succesful """ - resp = await self.__get_request("power/on") - return await self.__handle_response(resp) + return bool(await self.__get_request("power/on")) - async def power_off(self): + async def power_off(self) -> bool: """Trigger the strip to turn the lights off. Returns: bool: If the request was succesful """ - resp = await self.__get_request("power/off") - return await self.__handle_response(resp) + return bool(await self.__get_request("power/off")) - async def power_toggle(self): + async def power_toggle(self) -> bool: """Trigger the strip to toggle the power status. Returns: bool: If the request was succesful """ - resp = await self.__get_request("power/toggle") - return await self.__handle_response(resp) + return bool(await self.__get_request("power/toggle")) - async def next_pattern(self): + async def next_pattern(self) -> bool: """Trigger the strip to switch to the next pattern in memory. Returns: bool: If the request was succesful """ - resp = await self.__get_request("pattern/next") - return await self.__handle_response(resp) + return bool(await self.__get_request("pattern/next")) - async def freeze_frame(self, frame: int): + async def freeze_frame(self, frame: int) -> bool: """Freeze the animation at the specified frame. Args: @@ -155,8 +154,7 @@ class Flickerstrip: bool: If the request was succesful """ params = {"value": str(frame)} - resp = await self.__get_request("pattern/frame", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("pattern/frame", params=params)) async def download_pattern(self, id: int) -> PatternBuilder | None: """Download a pattern by its id @@ -175,14 +173,12 @@ class Flickerstrip: return None params = {"id": str(id)} - resp = await self.__get_request("pattern/download", params=params) - if resp.status != 200: - return None + data = bytes(await self.__get_request("pattern/download", + params=params, return_body=True)) - data = await resp.read() 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. Args: @@ -192,13 +188,12 @@ class Flickerstrip: bool: If the request was succesful """ params = {"index": str(index)} - resp = await self.__get_request("pattern/select", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("pattern/select", params=params)) async def delete_pattern(self, pattern: Optional[PatternMeta] = None, index: Optional[int] = None, - id: Optional[int] = None): + id: Optional[int] = None) -> bool: """Delete a pattern by its meta, index, or id Args: @@ -216,17 +211,20 @@ class Flickerstrip: return await self.delete_pattern(id=pattern.id) elif index is not None: params = {"index": str(index)} - resp = await self.__get_request("pattern/forget", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("pattern/forget", + params=params)) elif id is not None: params = {"id": str(id)} - resp = await self.__get_request("pattern/forget", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("pattern/forget", + params=params)) else: raise TypeError( "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. Args: @@ -245,11 +243,10 @@ class Flickerstrip: "preview": str(1 if preview else 0) } data = pattern.to_binary_string() - resp = await self.__post_request("pattern/create", + return await self.__post_request("pattern/create", 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). Args: @@ -264,7 +261,7 @@ class Flickerstrip: pattern.add_pixel(r, g, b) 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. Args: @@ -274,10 +271,9 @@ class Flickerstrip: bool: If the request was succesful """ params = {"value": str(value)} - resp = await self.__get_request("brightness", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("brightness", params=params)) - async def set_name(self, value: str): + async def set_name(self, value: str) -> bool: """Set the name of the flickerstrip. Args: @@ -287,10 +283,9 @@ class Flickerstrip: bool: If the request was succesful """ json = {"name": value} - resp = await self.__post_request("config/name", json=json) - return await self.__handle_response(resp) + return bool(await self.__post_request("config/name", json=json)) - async def set_group(self, value: str): + async def set_group(self, value: str) -> bool: """Set the group of the flickerstrip. Args: @@ -300,10 +295,9 @@ class Flickerstrip: bool: If the request was succesful """ json = {"name": value} - resp = await self.__post_request("config/group", json=json) - return await self.__handle_response(resp) + return bool(await self.__post_request("config/group", json=json)) - async def set_cycle(self, value): + async def set_cycle(self, value: int) -> bool: """Set the cycle timer of the flickerstrip. Args: @@ -313,11 +307,10 @@ class Flickerstrip: Returns: bool: If the request was succesful """ - params = {"value": value} - resp = await self.__get_request("config/cycle", params=params) - return await self.__handle_response(resp) + params = {"value": str(value)} + return bool(await self.__get_request("config/cycle", params=params)) - async def set_fade_duration(self, value: int): + async def set_fade_duration(self, value: int) -> bool: """Set the fade timer of the flickerstrip. Args: @@ -328,10 +321,9 @@ class Flickerstrip: bool: If the request was succesful """ params = {"value": str(value)} - resp = await self.__get_request("config/fade", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("config/fade", params=params)) - async def set_strip_length(self, value: int): + async def set_strip_length(self, value: int) -> bool: """Set the length of the flickerstrip. Args: @@ -341,10 +333,9 @@ class Flickerstrip: bool: If the request was succesful """ params = {"value": str(value)} - resp = await self.__get_request("config/length", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("config/length", params=params)) - async def set_strip_start(self, value: int): + async def set_strip_start(self, value: int) -> bool: """Set the start pixel of the strip. This will make the flickerstrip ignore all pixels before @@ -357,10 +348,9 @@ class Flickerstrip: bool: If the request was successful. """ params = {"value": str(value)} - resp = await self.__get_request("config/start", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("config/start", params=params)) - async def set_strip_end(self, value: int): + async def set_strip_end(self, value: int) -> bool: """Set the end pixel of the strip. This will make the flickerstrip ignore all pixels after @@ -373,10 +363,9 @@ class Flickerstrip: bool: If the request was successful. """ params = {"value": str(value)} - resp = await self.__get_request("config/end", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("config/end", params=params)) - async def set_reversed(self, value: bool): + async def set_reversed(self, value: bool) -> bool: """Set the reversed state of the flickerstrip. Args: @@ -387,5 +376,4 @@ class Flickerstrip: bool: If the request was succesful """ params = {"value": str(1 if value else 0)} - resp = await self.__get_request("config/reversed", params=params) - return await self.__handle_response(resp) + return bool(await self.__get_request("config/reversed", params=params)) diff --git a/pyproject.toml b/pyproject.toml index 6d15dd8..1f1ef8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "flickerstrip-py" -version = "0.2.2" +version = "0.2.3" description = "" authors = ["Evan Fiordeliso "] diff --git a/setup.py b/setup.py index 53086a3..ce50bbd 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import find_packages, setup setup( name='flickerstrip-py', packages=find_packages(), - version='0.2.2', + version='0.2.3', description='A python library for interracting with a flickerstrip.', author='Evan Fiordeliso ', license='MIT',