From c927edcd8759a4eba89ec9b4009109f2725c99e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20M=C3=BCller=20=28ChaoticByte=29?= Date: Tue, 25 Jul 2023 20:27:35 +0200 Subject: [PATCH] Switched from properties to getter/setter functions --- DOCUMENTATION.md | 296 +++++++++++++++++++---------------------- yahuelib/controller.py | 110 ++++++--------- 2 files changed, 181 insertions(+), 225 deletions(-) diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index d7dfd85..1e1fa21 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -1,38 +1,38 @@ # Table of Contents * [yahuelib](#yahuelib) -* [yahuelib.utils](#yahuelib.utils) - * [rgb\_to\_hsv](#yahuelib.utils.rgb_to_hsv) - * [kelvin\_to\_mired](#yahuelib.utils.kelvin_to_mired) * [yahuelib.controller](#yahuelib.controller) * [LightController](#yahuelib.controller.LightController) - * [reachable](#yahuelib.controller.LightController.reachable) - * [on](#yahuelib.controller.LightController.on) - * [on](#yahuelib.controller.LightController.on) - * [brightness](#yahuelib.controller.LightController.brightness) - * [brightness](#yahuelib.controller.LightController.brightness) - * [hue](#yahuelib.controller.LightController.hue) - * [hue](#yahuelib.controller.LightController.hue) - * [saturation](#yahuelib.controller.LightController.saturation) - * [saturation](#yahuelib.controller.LightController.saturation) - * [color\_temperature](#yahuelib.controller.LightController.color_temperature) - * [color\_temperature](#yahuelib.controller.LightController.color_temperature) + * [check\_reachable](#yahuelib.controller.LightController.check_reachable) + * [check\_on](#yahuelib.controller.LightController.check_on) + * [set\_on](#yahuelib.controller.LightController.set_on) + * [get\_brightness](#yahuelib.controller.LightController.get_brightness) + * [set\_brightness](#yahuelib.controller.LightController.set_brightness) + * [get\_hue](#yahuelib.controller.LightController.get_hue) + * [set\_hue](#yahuelib.controller.LightController.set_hue) + * [get\_saturation](#yahuelib.controller.LightController.get_saturation) + * [set\_saturation](#yahuelib.controller.LightController.set_saturation) + * [get\_color\_temperature](#yahuelib.controller.LightController.get_color_temperature) + * [set\_color\_temperature](#yahuelib.controller.LightController.set_color_temperature) * [alert](#yahuelib.controller.LightController.alert) * [alert\_long](#yahuelib.controller.LightController.alert_long) * [GroupController](#yahuelib.controller.GroupController) - * [any\_on](#yahuelib.controller.GroupController.any_on) - * [all\_on](#yahuelib.controller.GroupController.all_on) - * [all\_on](#yahuelib.controller.GroupController.all_on) - * [brightness](#yahuelib.controller.GroupController.brightness) - * [brightness](#yahuelib.controller.GroupController.brightness) - * [hue](#yahuelib.controller.GroupController.hue) - * [hue](#yahuelib.controller.GroupController.hue) - * [saturation](#yahuelib.controller.GroupController.saturation) - * [saturation](#yahuelib.controller.GroupController.saturation) - * [color\_temperature](#yahuelib.controller.GroupController.color_temperature) - * [color\_temperature](#yahuelib.controller.GroupController.color_temperature) + * [check\_any\_on](#yahuelib.controller.GroupController.check_any_on) + * [check\_all\_on](#yahuelib.controller.GroupController.check_all_on) + * [set\_all\_on](#yahuelib.controller.GroupController.set_all_on) + * [get\_brightness](#yahuelib.controller.GroupController.get_brightness) + * [set\_brightness](#yahuelib.controller.GroupController.set_brightness) + * [get\_hue](#yahuelib.controller.GroupController.get_hue) + * [set\_hue](#yahuelib.controller.GroupController.set_hue) + * [get\_saturation](#yahuelib.controller.GroupController.get_saturation) + * [set\_saturation](#yahuelib.controller.GroupController.set_saturation) + * [get\_color\_temperature](#yahuelib.controller.GroupController.get_color_temperature) + * [set\_color\_temperature](#yahuelib.controller.GroupController.set_color_temperature) * [alert](#yahuelib.controller.GroupController.alert) * [alert\_long](#yahuelib.controller.GroupController.alert_long) +* [yahuelib.utils](#yahuelib.utils) + * [rgb\_to\_hsv](#yahuelib.utils.rgb_to_hsv) + * [kelvin\_to\_mired](#yahuelib.utils.kelvin_to_mired) * [yahuelib.exceptions](#yahuelib.exceptions) * [LightOrGroupNotFound](#yahuelib.exceptions.LightOrGroupNotFound) * [APIError](#yahuelib.exceptions.APIError) @@ -41,30 +41,6 @@ # yahuelib - - -# yahuelib.utils - - - -#### rgb\_to\_hsv - -```python -def rgb_to_hsv(r: int, g: int, b: int) -> tuple -``` - -Convert RGB colors `(255, 220, 100)` to HSV `(0.129, 0.608, 1.0)` - - - -#### kelvin\_to\_mired - -```python -def kelvin_to_mired(kelvin: int) -``` - -Convert the color temperature from Kelvin to Mired - # yahuelib.controller @@ -87,126 +63,115 @@ Control a Philips Hue Light using the API of your Hue Bridge. Use the class method `.from_name(name:str, ...)` to use the name of a light instead of the number. - + -#### reachable +#### check\_reachable ```python -@property -def reachable() -> bool +def check_reachable() -> bool ``` -Check if the light is reachable using `LightController.reachable` +Check if the light is reachable - + -#### on +#### check\_on ```python -@property -def on() -> bool +def check_on() -> bool ``` -Check if the light is on using `LightController.on` +Check if the light is on - + -#### on +#### set\_on ```python -@on.setter -def on(on: bool) +def set_on(on: bool) ``` -Turn the light on/off using `LightController.on = ...` +Turn the light on/off - + -#### brightness +#### get\_brightness ```python -@property -def brightness() -> int +def get_brightness() -> int ``` -Get the brightness using `LightController.brightness` +Get the brightness - + -#### brightness +#### set\_brightness ```python -@brightness.setter -def brightness(brightness: float) +def set_brightness(brightness: float) ``` -Set the brightness using `LightController.brightness = ...` +Set the brightness - + -#### hue +#### get\_hue ```python -@property -def hue() -> int +def get_hue() -> int ``` -Get the hue using `LightController.hue` +Get the hue - + -#### hue +#### set\_hue ```python -@hue.setter -def hue(hue: float) +def set_hue(hue: float) ``` -Set the hue using `LightController.hue = ...` +Set the hue - + -#### saturation +#### get\_saturation ```python -@property -def saturation() -> int +def get_saturation() -> int ``` -Get the saturation using `LightController.saturation` +Get the saturation - + -#### saturation +#### set\_saturation ```python -@saturation.setter -def saturation(saturation: float) +def set_saturation(saturation: float) ``` -Set the saturation using `LightController.saturation = ...` +Set the saturation - + -#### color\_temperature +#### get\_color\_temperature ```python -@property -def color_temperature() +def get_color_temperature() ``` -Get the white color temperature in Mired using `LightController.color_temperature` +Get the white color temperature in Mired - + -#### color\_temperature +#### set\_color\_temperature ```python -@color_temperature.setter -def color_temperature(mired: int) +def set_color_temperature(mired: int) ``` -Set the white color temperature in Mired (`154` - `500`) using `LightController.color_temperature = ...` +Set the white color temperature in Mired (`154` - `500`) @@ -246,126 +211,115 @@ Control a Philips Hue Light Group (Room/Zone) using the API of your Hue Bridge. Use the class method `.from_name(name:str, ...)` to use the name of a group instead of the number. - + -#### any\_on +#### check\_any\_on ```python -@property -def any_on() -> bool +def check_any_on() -> bool ``` -Check if any light in this group is on using `GroupController.any_on` +Check if any light in this group is on - + -#### all\_on +#### check\_all\_on ```python -@property -def all_on() -> bool +def check_all_on() -> bool ``` -Check if all lights in this group are on using `GroupController.all_on` +Check if all lights in this group are on - + -#### all\_on +#### set\_all\_on ```python -@all_on.setter -def all_on(on: bool) +def set_all_on(on: bool) ``` -Turn on/off all lights in this group using `GroupController.all_on = ...` +Turn on/off all lights in this group - + -#### brightness +#### get\_brightness ```python -@property -def brightness() -> int +def get_brightness() -> int ``` -Get the last set brightness in this group using `GroupController.brightness` +Get the last set brightness in this group - + -#### brightness +#### set\_brightness ```python -@brightness.setter -def brightness(brightness: float) +def set_brightness(brightness: float) ``` -Set the brightness of all lights in this group using `GroupController.brightness = ...` +Set the brightness of all lights in this group - + -#### hue +#### get\_hue ```python -@property -def hue() -> int +def get_hue() -> int ``` -Get the last set hue in this group using `GroupController.hue` +Get the last set hue in this group - + -#### hue +#### set\_hue ```python -@hue.setter -def hue(hue: float) +def set_hue(hue: float) ``` -Set the hue of all lights in this group using `GroupController.hue = ...` +Set the hue of all lights in this group - + -#### saturation +#### get\_saturation ```python -@property -def saturation() -> int +def get_saturation() -> int ``` -Get the last set saturation in this group using `GroupController.saturation` +Get the last set saturation in this group - + -#### saturation +#### set\_saturation ```python -@saturation.setter -def saturation(saturation: float) +def set_saturation(saturation: float) ``` -Set the saturation of all lights in this group using `GroupController.saturation = ...` +Set the saturation of all lights in this group - + -#### color\_temperature +#### get\_color\_temperature ```python -@property -def color_temperature() +def get_color_temperature() ``` -Get the last set white color temperature in Mired using `GroupController.color_temperature` +Get the last set white color temperature in Mired - + -#### color\_temperature +#### set\_color\_temperature ```python -@color_temperature.setter -def color_temperature(mired: int) +def set_color_temperature(mired: int) ``` -Set the white color temperature in Mired (`154` - `500`) for all lights in this group using `GroupController.color_temperature = ...` +Set the white color temperature in Mired (`154` - `500`) for all lights in this group @@ -387,6 +341,30 @@ def alert_long() Flash all lights in the group for 10 seconds. + + +# yahuelib.utils + + + +#### rgb\_to\_hsv + +```python +def rgb_to_hsv(r: int, g: int, b: int) -> tuple +``` + +Convert RGB colors `(255, 220, 100)` to HSV `(0.129, 0.608, 1.0)` + + + +#### kelvin\_to\_mired + +```python +def kelvin_to_mired(kelvin: int) +``` + +Convert the color temperature from Kelvin to Mired + # yahuelib.exceptions diff --git a/yahuelib/controller.py b/yahuelib/controller.py index 49349c3..044014e 100644 --- a/yahuelib/controller.py +++ b/yahuelib/controller.py @@ -76,72 +76,61 @@ class LightController(_BaseController): _api_endpoint_all = "https://{bridge_ip_address}/api/{bridge_api_user}/lights" _api_endpoint_specific = "https://{bridge_ip_address}/api/{bridge_api_user}/lights/{number}" - @property - def reachable(self) -> bool: - '''Check if the light is reachable using `LightController.reachable`''' + def check_reachable(self) -> bool: + '''Check if the light is reachable''' data = self._api_request() return data["state"]["reachable"] - @property - def on(self) -> bool: - '''Check if the light is on using `LightController.on`''' + def check_on(self) -> bool: + '''Check if the light is on''' data = self._api_request() return data["state"]["on"] - @on.setter - def on(self, on:bool): - '''Turn the light on/off using `LightController.on = ...`''' + def set_on(self, on:bool): + '''Turn the light on/off''' assert type(on) == bool self._api_request("PUT", "/state", {"on": on}) - @property - def brightness(self) -> int: - '''Get the brightness using `LightController.brightness`''' + def get_brightness(self) -> int: + '''Get the brightness''' data = self._api_request() return data["state"]["bri"] - @brightness.setter - def brightness(self, brightness:float): - '''Set the brightness using `LightController.brightness = ...`''' + def set_brightness(self, brightness:float): + '''Set the brightness''' assert type(brightness) == float or type(brightness) == int bri_ = min(max(int(brightness * 254), 0), 254) self._api_request("PUT", "/state", {"bri": bri_}) - @property - def hue(self) -> int: - '''Get the hue using `LightController.hue`''' + def get_hue(self) -> int: + '''Get the hue''' data = self._api_request() return data["state"]["hue"] - @hue.setter - def hue(self, hue:float): - '''Set the hue using `LightController.hue = ...`''' + def set_hue(self, hue:float): + '''Set the hue''' assert type(hue) == float or type(hue) == int hue_ = min(max(int(hue * 65535), 0), 65535) self._api_request("PUT", "/state", {"hue": hue_}) - @property - def saturation(self) -> int: - '''Get the saturation using `LightController.saturation`''' + def get_saturation(self) -> int: + '''Get the saturation''' data = self._api_request() return data["state"]["sat"] - @saturation.setter - def saturation(self, saturation:float): - '''Set the saturation using `LightController.saturation = ...`''' + def set_saturation(self, saturation:float): + '''Set the saturation''' assert type(saturation) == float or type(saturation) == int sat_ = min(max(int(saturation * 254), 0), 254) self._api_request("PUT", "/state", {"sat": sat_}) - @property - def color_temperature(self): - '''Get the white color temperature in Mired using `LightController.color_temperature`''' + def get_color_temperature(self): + '''Get the white color temperature in Mired''' data = self._api_request() return data["state"]["ct"] - @color_temperature.setter - def color_temperature(self, mired:int): - '''Set the white color temperature in Mired (`154` - `500`) using `LightController.color_temperature = ...`''' + def set_color_temperature(self, mired:int): + '''Set the white color temperature in Mired (`154` - `500`)''' assert type(mired) == int ct_ = min(max(mired, 154), 500) self._api_request("PUT", "/state", {"ct": ct_}) @@ -169,72 +158,61 @@ class GroupController(_BaseController): _api_endpoint_all = "https://{bridge_ip_address}/api/{bridge_api_user}/groups" _api_endpoint_specific = "https://{bridge_ip_address}/api/{bridge_api_user}/groups/{number}" - @property - def any_on(self) -> bool: - '''Check if any light in this group is on using `GroupController.any_on`''' + def check_any_on(self) -> bool: + '''Check if any light in this group is on''' data = self._api_request() return data["state"]["any_on"] - @property - def all_on(self) -> bool: - '''Check if all lights in this group are on using `GroupController.all_on`''' + def check_all_on(self) -> bool: + '''Check if all lights in this group are on''' data = self._api_request() return data["state"]["all_on"] - @all_on.setter - def all_on(self, on:bool): - '''Turn on/off all lights in this group using `GroupController.all_on = ...`''' + def set_all_on(self, on:bool): + '''Turn on/off all lights in this group''' assert type(on) == bool self._api_request("PUT", "/action", {"on": on}) - @property - def brightness(self) -> int: - '''Get the last set brightness in this group using `GroupController.brightness`''' + def get_brightness(self) -> int: + '''Get the last set brightness in this group''' data = self._api_request() return data["action"]["bri"] - @brightness.setter - def brightness(self, brightness:float): - '''Set the brightness of all lights in this group using `GroupController.brightness = ...`''' + def set_brightness(self, brightness:float): + '''Set the brightness of all lights in this group''' assert type(brightness) == float or type(brightness) == int bri_ = min(max(int(brightness * 254), 0), 254) self._api_request("PUT", "/action", {"bri": bri_}) - @property - def hue(self) -> int: - '''Get the last set hue in this group using `GroupController.hue`''' + def get_hue(self) -> int: + '''Get the last set hue in this group''' data = self._api_request() return data["action"]["hue"] - @hue.setter - def hue(self, hue:float): - '''Set the hue of all lights in this group using `GroupController.hue = ...`''' + def set_hue(self, hue:float): + '''Set the hue of all lights in this group''' assert type(hue) == float or type(hue) == int hue_ = min(max(int(hue * 65535), 0), 65535) self._api_request("PUT", "/action", {"hue": hue_}) - @property - def saturation(self) -> int: - '''Get the last set saturation in this group using `GroupController.saturation`''' + def get_saturation(self) -> int: + '''Get the last set saturation in this group''' data = self._api_request() return data["action"]["sat"] - @saturation.setter - def saturation(self, saturation:float): - '''Set the saturation of all lights in this group using `GroupController.saturation = ...`''' + def set_saturation(self, saturation:float): + '''Set the saturation of all lights in this group''' assert type(saturation) == float or type(saturation) == int sat_ = min(max(int(saturation * 254), 0), 254) self._api_request("PUT", "/action", {"sat": sat_}) - @property - def color_temperature(self): - '''Get the last set white color temperature in Mired using `GroupController.color_temperature`''' + def get_color_temperature(self): + '''Get the last set white color temperature in Mired''' data = self._api_request() return data["action"]["ct"] - @color_temperature.setter - def color_temperature(self, mired:int): - '''Set the white color temperature in Mired (`154` - `500`) for all lights in this group using `GroupController.color_temperature = ...`''' + def set_color_temperature(self, mired:int): + '''Set the white color temperature in Mired (`154` - `500`) for all lights in this group''' assert type(mired) == int ct_ = min(max(mired, 154), 500) self._api_request("PUT", "/action", {"ct": ct_})