From e4dc13bad120b39e4b56231ebdb513007bf1e899 Mon Sep 17 00:00:00 2001 From: Graham Home Date: Sat, 7 Aug 2021 21:42:22 -0400 Subject: [PATCH 1/2] Add set_states method --- pifx/client.py | 2 +- pifx/core.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pifx/client.py b/pifx/client.py index 1cf0334..ce8e67a 100644 --- a/pifx/client.py +++ b/pifx/client.py @@ -36,7 +36,7 @@ def perform_request( if json_body: res = self._s.request( - method=method, url=http_endpoint, json=data, headers=self.headers) + method=method, url=http_endpoint, json=json_body, headers=self.headers) else: res = self._s.request( method=method, url=http_endpoint, data=data, headers=self.headers) diff --git a/pifx/core.py b/pifx/core.py index fb36ef0..eac2e63 100644 --- a/pifx/core.py +++ b/pifx/core.py @@ -72,6 +72,41 @@ def set_state(self, selector='all', method='put', endpoint='lights/{}/state', endpoint_args=[selector], argument_tuples=argument_tuples) + def set_states(self, states=[{'selector': 'all', + 'power': None, 'color': None, 'brightness': None, 'duration': None}]): + """Given a list of states, set the state of one or more lights. + States may contain selector, power, color, brightness and duration parameters + as detailed below. + Selectors can be based on id, scene_id, group_id, label, etc. + Returns list of lightbulb statuses if successful. + See http://api.developer.lifx.com/v1/docs/selectors + + + selector: required String + The selector to limit which lights will run the effect. + + power: String + e.g "on" or "off" + + color: String + e.g #ff0000 or "red" + Color to set selected bulbs. + Hex color code, color name, saturation percentage, hue, RGB, etc. + See http://api.developer.lifx.com/v1/docs/colors + + brightness: Double + e.g 0.5 + Set brightness level from 0 to 1 + + duration: Double + e.g 10 + Setting transition time, in seconds, from 0.0 to + 3155760000.0 (100 years). + """ + json_body = {"states": states} + return self.client.perform_request( + method='put', endpoint='lights/states', json_body=json_body) + def state_delta(self, selector='all', power=None, duration=1.0, infrared=None, hue=None, saturation=None, brightness=None, kelvin=None): From 7a9b8f969aae10e1cc76cf50149e889ba24e342d Mon Sep 17 00:00:00 2001 From: Graham Home Date: Sun, 8 Aug 2021 10:17:23 -0400 Subject: [PATCH 2/2] Remove mutable argument, add usage example --- pifx/core.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pifx/core.py b/pifx/core.py index eac2e63..6af2c89 100644 --- a/pifx/core.py +++ b/pifx/core.py @@ -72,10 +72,9 @@ def set_state(self, selector='all', method='put', endpoint='lights/{}/state', endpoint_args=[selector], argument_tuples=argument_tuples) - def set_states(self, states=[{'selector': 'all', - 'power': None, 'color': None, 'brightness': None, 'duration': None}]): - """Given a list of states, set the state of one or more lights. - States may contain selector, power, color, brightness and duration parameters + def set_states(self, states=None): + """Given a collection of "state" dictionaries, set the state of one or more lights. + State dictionaries may contain selector, power, color, brightness and duration parameters as detailed below. Selectors can be based on id, scene_id, group_id, label, etc. Returns list of lightbulb statuses if successful. @@ -102,7 +101,12 @@ def set_states(self, states=[{'selector': 'all', e.g 10 Setting transition time, in seconds, from 0.0 to 3155760000.0 (100 years). + + Example invocation: + pifx.set_states([{'selector': 'id:XXXYYYZZZZ', 'color': 'red'}, {'selector': 'id:AAABBBCCCCC', 'brightness': 0.8}]) """ + if states is None: + states = [] json_body = {"states": states} return self.client.perform_request( method='put', endpoint='lights/states', json_body=json_body)