Switched from properties to getter/setter functions

This commit is contained in:
Julian Müller (ChaoticByte) 2023-07-25 20:27:35 +02:00
parent ed75cd2951
commit c927edcd87
2 changed files with 181 additions and 225 deletions

View file

@ -1,38 +1,38 @@
# Table of Contents # Table of Contents
* [yahuelib](#yahuelib) * [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) * [yahuelib.controller](#yahuelib.controller)
* [LightController](#yahuelib.controller.LightController) * [LightController](#yahuelib.controller.LightController)
* [reachable](#yahuelib.controller.LightController.reachable) * [check\_reachable](#yahuelib.controller.LightController.check_reachable)
* [on](#yahuelib.controller.LightController.on) * [check\_on](#yahuelib.controller.LightController.check_on)
* [on](#yahuelib.controller.LightController.on) * [set\_on](#yahuelib.controller.LightController.set_on)
* [brightness](#yahuelib.controller.LightController.brightness) * [get\_brightness](#yahuelib.controller.LightController.get_brightness)
* [brightness](#yahuelib.controller.LightController.brightness) * [set\_brightness](#yahuelib.controller.LightController.set_brightness)
* [hue](#yahuelib.controller.LightController.hue) * [get\_hue](#yahuelib.controller.LightController.get_hue)
* [hue](#yahuelib.controller.LightController.hue) * [set\_hue](#yahuelib.controller.LightController.set_hue)
* [saturation](#yahuelib.controller.LightController.saturation) * [get\_saturation](#yahuelib.controller.LightController.get_saturation)
* [saturation](#yahuelib.controller.LightController.saturation) * [set\_saturation](#yahuelib.controller.LightController.set_saturation)
* [color\_temperature](#yahuelib.controller.LightController.color_temperature) * [get\_color\_temperature](#yahuelib.controller.LightController.get_color_temperature)
* [color\_temperature](#yahuelib.controller.LightController.color_temperature) * [set\_color\_temperature](#yahuelib.controller.LightController.set_color_temperature)
* [alert](#yahuelib.controller.LightController.alert) * [alert](#yahuelib.controller.LightController.alert)
* [alert\_long](#yahuelib.controller.LightController.alert_long) * [alert\_long](#yahuelib.controller.LightController.alert_long)
* [GroupController](#yahuelib.controller.GroupController) * [GroupController](#yahuelib.controller.GroupController)
* [any\_on](#yahuelib.controller.GroupController.any_on) * [check\_any\_on](#yahuelib.controller.GroupController.check_any_on)
* [all\_on](#yahuelib.controller.GroupController.all_on) * [check\_all\_on](#yahuelib.controller.GroupController.check_all_on)
* [all\_on](#yahuelib.controller.GroupController.all_on) * [set\_all\_on](#yahuelib.controller.GroupController.set_all_on)
* [brightness](#yahuelib.controller.GroupController.brightness) * [get\_brightness](#yahuelib.controller.GroupController.get_brightness)
* [brightness](#yahuelib.controller.GroupController.brightness) * [set\_brightness](#yahuelib.controller.GroupController.set_brightness)
* [hue](#yahuelib.controller.GroupController.hue) * [get\_hue](#yahuelib.controller.GroupController.get_hue)
* [hue](#yahuelib.controller.GroupController.hue) * [set\_hue](#yahuelib.controller.GroupController.set_hue)
* [saturation](#yahuelib.controller.GroupController.saturation) * [get\_saturation](#yahuelib.controller.GroupController.get_saturation)
* [saturation](#yahuelib.controller.GroupController.saturation) * [set\_saturation](#yahuelib.controller.GroupController.set_saturation)
* [color\_temperature](#yahuelib.controller.GroupController.color_temperature) * [get\_color\_temperature](#yahuelib.controller.GroupController.get_color_temperature)
* [color\_temperature](#yahuelib.controller.GroupController.color_temperature) * [set\_color\_temperature](#yahuelib.controller.GroupController.set_color_temperature)
* [alert](#yahuelib.controller.GroupController.alert) * [alert](#yahuelib.controller.GroupController.alert)
* [alert\_long](#yahuelib.controller.GroupController.alert_long) * [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) * [yahuelib.exceptions](#yahuelib.exceptions)
* [LightOrGroupNotFound](#yahuelib.exceptions.LightOrGroupNotFound) * [LightOrGroupNotFound](#yahuelib.exceptions.LightOrGroupNotFound)
* [APIError](#yahuelib.exceptions.APIError) * [APIError](#yahuelib.exceptions.APIError)
@ -41,30 +41,6 @@
# yahuelib # yahuelib
<a id="yahuelib.utils"></a>
# yahuelib.utils
<a id="yahuelib.utils.rgb_to_hsv"></a>
#### 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)`
<a id="yahuelib.utils.kelvin_to_mired"></a>
#### kelvin\_to\_mired
```python
def kelvin_to_mired(kelvin: int)
```
Convert the color temperature from Kelvin to Mired
<a id="yahuelib.controller"></a> <a id="yahuelib.controller"></a>
# yahuelib.controller # 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. Use the class method `.from_name(name:str, ...)` to use the name of a light instead of the number.
<a id="yahuelib.controller.LightController.reachable"></a> <a id="yahuelib.controller.LightController.check_reachable"></a>
#### reachable #### check\_reachable
```python ```python
@property def check_reachable() -> bool
def reachable() -> bool
``` ```
Check if the light is reachable using `LightController.reachable` Check if the light is reachable
<a id="yahuelib.controller.LightController.on"></a> <a id="yahuelib.controller.LightController.check_on"></a>
#### on #### check\_on
```python ```python
@property def check_on() -> bool
def on() -> bool
``` ```
Check if the light is on using `LightController.on` Check if the light is on
<a id="yahuelib.controller.LightController.on"></a> <a id="yahuelib.controller.LightController.set_on"></a>
#### on #### set\_on
```python ```python
@on.setter def set_on(on: bool)
def on(on: bool)
``` ```
Turn the light on/off using `LightController.on = ...` Turn the light on/off
<a id="yahuelib.controller.LightController.brightness"></a> <a id="yahuelib.controller.LightController.get_brightness"></a>
#### brightness #### get\_brightness
```python ```python
@property def get_brightness() -> int
def brightness() -> int
``` ```
Get the brightness using `LightController.brightness` Get the brightness
<a id="yahuelib.controller.LightController.brightness"></a> <a id="yahuelib.controller.LightController.set_brightness"></a>
#### brightness #### set\_brightness
```python ```python
@brightness.setter def set_brightness(brightness: float)
def brightness(brightness: float)
``` ```
Set the brightness using `LightController.brightness = ...` Set the brightness
<a id="yahuelib.controller.LightController.hue"></a> <a id="yahuelib.controller.LightController.get_hue"></a>
#### hue #### get\_hue
```python ```python
@property def get_hue() -> int
def hue() -> int
``` ```
Get the hue using `LightController.hue` Get the hue
<a id="yahuelib.controller.LightController.hue"></a> <a id="yahuelib.controller.LightController.set_hue"></a>
#### hue #### set\_hue
```python ```python
@hue.setter def set_hue(hue: float)
def hue(hue: float)
``` ```
Set the hue using `LightController.hue = ...` Set the hue
<a id="yahuelib.controller.LightController.saturation"></a> <a id="yahuelib.controller.LightController.get_saturation"></a>
#### saturation #### get\_saturation
```python ```python
@property def get_saturation() -> int
def saturation() -> int
``` ```
Get the saturation using `LightController.saturation` Get the saturation
<a id="yahuelib.controller.LightController.saturation"></a> <a id="yahuelib.controller.LightController.set_saturation"></a>
#### saturation #### set\_saturation
```python ```python
@saturation.setter def set_saturation(saturation: float)
def saturation(saturation: float)
``` ```
Set the saturation using `LightController.saturation = ...` Set the saturation
<a id="yahuelib.controller.LightController.color_temperature"></a> <a id="yahuelib.controller.LightController.get_color_temperature"></a>
#### color\_temperature #### get\_color\_temperature
```python ```python
@property def get_color_temperature()
def color_temperature()
``` ```
Get the white color temperature in Mired using `LightController.color_temperature` Get the white color temperature in Mired
<a id="yahuelib.controller.LightController.color_temperature"></a> <a id="yahuelib.controller.LightController.set_color_temperature"></a>
#### color\_temperature #### set\_color\_temperature
```python ```python
@color_temperature.setter def set_color_temperature(mired: int)
def 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`)
<a id="yahuelib.controller.LightController.alert"></a> <a id="yahuelib.controller.LightController.alert"></a>
@ -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. Use the class method `.from_name(name:str, ...)` to use the name of a group instead of the number.
<a id="yahuelib.controller.GroupController.any_on"></a> <a id="yahuelib.controller.GroupController.check_any_on"></a>
#### any\_on #### check\_any\_on
```python ```python
@property def check_any_on() -> bool
def 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
<a id="yahuelib.controller.GroupController.all_on"></a> <a id="yahuelib.controller.GroupController.check_all_on"></a>
#### all\_on #### check\_all\_on
```python ```python
@property def check_all_on() -> bool
def 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
<a id="yahuelib.controller.GroupController.all_on"></a> <a id="yahuelib.controller.GroupController.set_all_on"></a>
#### all\_on #### set\_all\_on
```python ```python
@all_on.setter def set_all_on(on: bool)
def all_on(on: bool)
``` ```
Turn on/off all lights in this group using `GroupController.all_on = ...` Turn on/off all lights in this group
<a id="yahuelib.controller.GroupController.brightness"></a> <a id="yahuelib.controller.GroupController.get_brightness"></a>
#### brightness #### get\_brightness
```python ```python
@property def get_brightness() -> int
def brightness() -> int
``` ```
Get the last set brightness in this group using `GroupController.brightness` Get the last set brightness in this group
<a id="yahuelib.controller.GroupController.brightness"></a> <a id="yahuelib.controller.GroupController.set_brightness"></a>
#### brightness #### set\_brightness
```python ```python
@brightness.setter def set_brightness(brightness: float)
def brightness(brightness: float)
``` ```
Set the brightness of all lights in this group using `GroupController.brightness = ...` Set the brightness of all lights in this group
<a id="yahuelib.controller.GroupController.hue"></a> <a id="yahuelib.controller.GroupController.get_hue"></a>
#### hue #### get\_hue
```python ```python
@property def get_hue() -> int
def hue() -> int
``` ```
Get the last set hue in this group using `GroupController.hue` Get the last set hue in this group
<a id="yahuelib.controller.GroupController.hue"></a> <a id="yahuelib.controller.GroupController.set_hue"></a>
#### hue #### set\_hue
```python ```python
@hue.setter def set_hue(hue: float)
def hue(hue: float)
``` ```
Set the hue of all lights in this group using `GroupController.hue = ...` Set the hue of all lights in this group
<a id="yahuelib.controller.GroupController.saturation"></a> <a id="yahuelib.controller.GroupController.get_saturation"></a>
#### saturation #### get\_saturation
```python ```python
@property def get_saturation() -> int
def saturation() -> int
``` ```
Get the last set saturation in this group using `GroupController.saturation` Get the last set saturation in this group
<a id="yahuelib.controller.GroupController.saturation"></a> <a id="yahuelib.controller.GroupController.set_saturation"></a>
#### saturation #### set\_saturation
```python ```python
@saturation.setter def set_saturation(saturation: float)
def saturation(saturation: float)
``` ```
Set the saturation of all lights in this group using `GroupController.saturation = ...` Set the saturation of all lights in this group
<a id="yahuelib.controller.GroupController.color_temperature"></a> <a id="yahuelib.controller.GroupController.get_color_temperature"></a>
#### color\_temperature #### get\_color\_temperature
```python ```python
@property def get_color_temperature()
def color_temperature()
``` ```
Get the last set white color temperature in Mired using `GroupController.color_temperature` Get the last set white color temperature in Mired
<a id="yahuelib.controller.GroupController.color_temperature"></a> <a id="yahuelib.controller.GroupController.set_color_temperature"></a>
#### color\_temperature #### set\_color\_temperature
```python ```python
@color_temperature.setter def set_color_temperature(mired: int)
def 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
<a id="yahuelib.controller.GroupController.alert"></a> <a id="yahuelib.controller.GroupController.alert"></a>
@ -387,6 +341,30 @@ def alert_long()
Flash all lights in the group for 10 seconds. Flash all lights in the group for 10 seconds.
<a id="yahuelib.utils"></a>
# yahuelib.utils
<a id="yahuelib.utils.rgb_to_hsv"></a>
#### 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)`
<a id="yahuelib.utils.kelvin_to_mired"></a>
#### kelvin\_to\_mired
```python
def kelvin_to_mired(kelvin: int)
```
Convert the color temperature from Kelvin to Mired
<a id="yahuelib.exceptions"></a> <a id="yahuelib.exceptions"></a>
# yahuelib.exceptions # yahuelib.exceptions

View file

@ -76,72 +76,61 @@ class LightController(_BaseController):
_api_endpoint_all = "https://{bridge_ip_address}/api/{bridge_api_user}/lights" _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}" _api_endpoint_specific = "https://{bridge_ip_address}/api/{bridge_api_user}/lights/{number}"
@property def check_reachable(self) -> bool:
def reachable(self) -> bool: '''Check if the light is reachable'''
'''Check if the light is reachable using `LightController.reachable`'''
data = self._api_request() data = self._api_request()
return data["state"]["reachable"] return data["state"]["reachable"]
@property def check_on(self) -> bool:
def on(self) -> bool: '''Check if the light is on'''
'''Check if the light is on using `LightController.on`'''
data = self._api_request() data = self._api_request()
return data["state"]["on"] return data["state"]["on"]
@on.setter def set_on(self, on:bool):
def on(self, on:bool): '''Turn the light on/off'''
'''Turn the light on/off using `LightController.on = ...`'''
assert type(on) == bool assert type(on) == bool
self._api_request("PUT", "/state", {"on": on}) self._api_request("PUT", "/state", {"on": on})
@property def get_brightness(self) -> int:
def brightness(self) -> int: '''Get the brightness'''
'''Get the brightness using `LightController.brightness`'''
data = self._api_request() data = self._api_request()
return data["state"]["bri"] return data["state"]["bri"]
@brightness.setter def set_brightness(self, brightness:float):
def brightness(self, brightness:float): '''Set the brightness'''
'''Set the brightness using `LightController.brightness = ...`'''
assert type(brightness) == float or type(brightness) == int assert type(brightness) == float or type(brightness) == int
bri_ = min(max(int(brightness * 254), 0), 254) bri_ = min(max(int(brightness * 254), 0), 254)
self._api_request("PUT", "/state", {"bri": bri_}) self._api_request("PUT", "/state", {"bri": bri_})
@property def get_hue(self) -> int:
def hue(self) -> int: '''Get the hue'''
'''Get the hue using `LightController.hue`'''
data = self._api_request() data = self._api_request()
return data["state"]["hue"] return data["state"]["hue"]
@hue.setter def set_hue(self, hue:float):
def hue(self, hue:float): '''Set the hue'''
'''Set the hue using `LightController.hue = ...`'''
assert type(hue) == float or type(hue) == int assert type(hue) == float or type(hue) == int
hue_ = min(max(int(hue * 65535), 0), 65535) hue_ = min(max(int(hue * 65535), 0), 65535)
self._api_request("PUT", "/state", {"hue": hue_}) self._api_request("PUT", "/state", {"hue": hue_})
@property def get_saturation(self) -> int:
def saturation(self) -> int: '''Get the saturation'''
'''Get the saturation using `LightController.saturation`'''
data = self._api_request() data = self._api_request()
return data["state"]["sat"] return data["state"]["sat"]
@saturation.setter def set_saturation(self, saturation:float):
def saturation(self, saturation:float): '''Set the saturation'''
'''Set the saturation using `LightController.saturation = ...`'''
assert type(saturation) == float or type(saturation) == int assert type(saturation) == float or type(saturation) == int
sat_ = min(max(int(saturation * 254), 0), 254) sat_ = min(max(int(saturation * 254), 0), 254)
self._api_request("PUT", "/state", {"sat": sat_}) self._api_request("PUT", "/state", {"sat": sat_})
@property def get_color_temperature(self):
def color_temperature(self): '''Get the white color temperature in Mired'''
'''Get the white color temperature in Mired using `LightController.color_temperature`'''
data = self._api_request() data = self._api_request()
return data["state"]["ct"] return data["state"]["ct"]
@color_temperature.setter def set_color_temperature(self, mired:int):
def color_temperature(self, mired:int): '''Set the white color temperature in Mired (`154` - `500`)'''
'''Set the white color temperature in Mired (`154` - `500`) using `LightController.color_temperature = ...`'''
assert type(mired) == int assert type(mired) == int
ct_ = min(max(mired, 154), 500) ct_ = min(max(mired, 154), 500)
self._api_request("PUT", "/state", {"ct": ct_}) 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_all = "https://{bridge_ip_address}/api/{bridge_api_user}/groups"
_api_endpoint_specific = "https://{bridge_ip_address}/api/{bridge_api_user}/groups/{number}" _api_endpoint_specific = "https://{bridge_ip_address}/api/{bridge_api_user}/groups/{number}"
@property def check_any_on(self) -> bool:
def any_on(self) -> bool: '''Check if any light in this group is on'''
'''Check if any light in this group is on using `GroupController.any_on`'''
data = self._api_request() data = self._api_request()
return data["state"]["any_on"] return data["state"]["any_on"]
@property def check_all_on(self) -> bool:
def all_on(self) -> bool: '''Check if all lights in this group are on'''
'''Check if all lights in this group are on using `GroupController.all_on`'''
data = self._api_request() data = self._api_request()
return data["state"]["all_on"] return data["state"]["all_on"]
@all_on.setter def set_all_on(self, on:bool):
def all_on(self, on:bool): '''Turn on/off all lights in this group'''
'''Turn on/off all lights in this group using `GroupController.all_on = ...`'''
assert type(on) == bool assert type(on) == bool
self._api_request("PUT", "/action", {"on": on}) self._api_request("PUT", "/action", {"on": on})
@property def get_brightness(self) -> int:
def brightness(self) -> int: '''Get the last set brightness in this group'''
'''Get the last set brightness in this group using `GroupController.brightness`'''
data = self._api_request() data = self._api_request()
return data["action"]["bri"] return data["action"]["bri"]
@brightness.setter def set_brightness(self, brightness:float):
def brightness(self, brightness:float): '''Set the brightness of all lights in this group'''
'''Set the brightness of all lights in this group using `GroupController.brightness = ...`'''
assert type(brightness) == float or type(brightness) == int assert type(brightness) == float or type(brightness) == int
bri_ = min(max(int(brightness * 254), 0), 254) bri_ = min(max(int(brightness * 254), 0), 254)
self._api_request("PUT", "/action", {"bri": bri_}) self._api_request("PUT", "/action", {"bri": bri_})
@property def get_hue(self) -> int:
def hue(self) -> int: '''Get the last set hue in this group'''
'''Get the last set hue in this group using `GroupController.hue`'''
data = self._api_request() data = self._api_request()
return data["action"]["hue"] return data["action"]["hue"]
@hue.setter def set_hue(self, hue:float):
def hue(self, hue:float): '''Set the hue of all lights in this group'''
'''Set the hue of all lights in this group using `GroupController.hue = ...`'''
assert type(hue) == float or type(hue) == int assert type(hue) == float or type(hue) == int
hue_ = min(max(int(hue * 65535), 0), 65535) hue_ = min(max(int(hue * 65535), 0), 65535)
self._api_request("PUT", "/action", {"hue": hue_}) self._api_request("PUT", "/action", {"hue": hue_})
@property def get_saturation(self) -> int:
def saturation(self) -> int: '''Get the last set saturation in this group'''
'''Get the last set saturation in this group using `GroupController.saturation`'''
data = self._api_request() data = self._api_request()
return data["action"]["sat"] return data["action"]["sat"]
@saturation.setter def set_saturation(self, saturation:float):
def saturation(self, saturation:float): '''Set the saturation of all lights in this group'''
'''Set the saturation of all lights in this group using `GroupController.saturation = ...`'''
assert type(saturation) == float or type(saturation) == int assert type(saturation) == float or type(saturation) == int
sat_ = min(max(int(saturation * 254), 0), 254) sat_ = min(max(int(saturation * 254), 0), 254)
self._api_request("PUT", "/action", {"sat": sat_}) self._api_request("PUT", "/action", {"sat": sat_})
@property def get_color_temperature(self):
def color_temperature(self): '''Get the last set white color temperature in Mired'''
'''Get the last set white color temperature in Mired using `GroupController.color_temperature`'''
data = self._api_request() data = self._api_request()
return data["action"]["ct"] return data["action"]["ct"]
@color_temperature.setter def set_color_temperature(self, mired:int):
def color_temperature(self, mired:int): '''Set the white color temperature in Mired (`154` - `500`) for all lights in this group'''
'''Set the white color temperature in Mired (`154` - `500`) for all lights in this group using `GroupController.color_temperature = ...`'''
assert type(mired) == int assert type(mired) == int
ct_ = min(max(mired, 154), 500) ct_ = min(max(mired, 154), 500)
self._api_request("PUT", "/action", {"ct": ct_}) self._api_request("PUT", "/action", {"ct": ct_})