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

@ -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_})