Skip to content

Commit

Permalink
feat: allow setting all values for grouped lights in one api call (#1)
Browse files Browse the repository at this point in the history
* feat: allow setting all properties at once

* tests

---------

Co-authored-by: eugene_yao <eyy2102@gmail.com>
  • Loading branch information
fidesachates and eugene-yao-zocdoc authored Apr 16, 2024
1 parent 7f53a84 commit 2777895
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/python_hue_v2/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ def get_grouped_lights(self) -> List[dict]:
def get_grouped_light(self, grouped_light_id: str) -> dict:
return self._get_by_id(self._grouped_light_category, grouped_light_id)

def set_grouped_light_service(self, grouped_light_id: str, property_name: str, property_value: dict) -> dict:
def set_grouped_light_service(self, grouped_light_id: str, properties: dict) -> dict:
return self._put_by_id(self._grouped_light_category, grouped_light_id,
properties={property_name: property_value})
properties=properties)

def get_devices(self):
return self._get(self._device_category)
Expand Down
16 changes: 12 additions & 4 deletions src/python_hue_v2/grouped_light.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __init__(self, bridge: Bridge, grouped_light_id: str):
def _get(self):
return self.bridge.get_grouped_light(self.grouped_light_id)

def _set(self, property_name: str, property_value: dict) -> dict:
def _set(self, properties: dict) -> dict:
"""_set is equal to HTTP PUT
Arguments:
Expand All @@ -36,7 +36,7 @@ def _set(self, property_name: str, property_value: dict) -> dict:
Returns:
Response data, ResourceIdentifierPut, should be a dict for one id, not list
"""
return self.bridge.set_grouped_light_service(self.grouped_light_id, property_name, property_value)
return self.bridge.set_grouped_light_service(self.grouped_light_id, properties)

@property
def data_dict(self) -> dict:
Expand All @@ -48,7 +48,15 @@ def on(self) -> bool:

@on.setter
def on(self, value: bool):
self._set('on', {'on': value})
self._set({'on': {'on': value}})

def set_state(self, value: bool, brightness: float = None, duration_ms: int = None):
properties = {'on': {'on': value}}
if duration_ms:
properties['dynamics'] = {'duration': duration_ms}
if brightness:
properties['dimming'] = {'brightness': brightness}
self._set(properties)

@property
def type(self) -> str:
Expand All @@ -60,7 +68,7 @@ def brightness(self) -> float:

@brightness.setter
def brightness(self, value: float):
self._set('dimming', {'brightness': value})
self._set({'dimming': {'brightness': value}})

@property
def owner(self) -> Owner:
Expand Down
59 changes: 59 additions & 0 deletions tests/test_hue_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import time
from unittest import TestCase

from python_hue_v2 import Hue, BridgeFinder

finder = BridgeFinder()
time.sleep(1)
test_hostname = finder.get_bridge_server_lists()[0]
test_key = '7K-IbBzEV3wZoXkTlSh6HyLTALLFsYrxCjIcW1o9'
hue = Hue(test_hostname, test_key)


class IntegrationTests(TestCase):
def test_set_on_grouped_light(self):
grouped_lights = hue.grouped_lights
grouped_light = grouped_lights[0]

test_cases = [
True,
False,
]

for is_on in test_cases:
with self.subTest(is_on=is_on):
grouped_light.on = is_on
time.sleep(1)

def test_set_brightness_grouped_light(self):
grouped_lights = hue.grouped_lights
grouped_light = grouped_lights[0]
grouped_light.on = True

test_cases = [
30,
50,
100
]

for brightness in test_cases:
with self.subTest(brightness=brightness):
grouped_light.brightness = brightness
time.sleep(1)

def test_set_state_grouped_light(self):
grouped_lights = hue.grouped_lights
grouped_light = grouped_lights[0]

test_cases = [
(True, 100, 6000),
(False, 50, 3000),
(True, 100, None),
(False, None, None),
(True, None, None),
]

for is_on, brightness, duration in test_cases:
with self.subTest(is_on=is_on, brightness=brightness, duration=duration):
grouped_light.set_state(is_on, brightness, duration)
time.sleep(duration / 1000 if duration else 1)

0 comments on commit 2777895

Please sign in to comment.