def activate_scene(scene_name): """ Activate a specific scene if the lights are already on """ b = Bridge(bridge_ip, bridge_username) scenes = b.scenes() for scene_id, scene in scenes.items(): if scene['name'] == scene_name: # Variable to store the current light state current_lightstates = {} status = b.scenes[scene_id](http_method='get') lightstates = status['lightstates'] # Check the current status of the lights # If every lights involved in this scene # are already off, don't change anything # It might means it is the day everything_is_off = True for light_id, light in lightstates.items(): light_status = b.lights[light_id](http_method='get') # Save the status of this light buble current_lightstates[light_id] = light_status['state'] if light_status['state']['on'] == True: everything_is_off = False if everything_is_off: # Save the current state (with everything off) save_current_state(current_lightstates) return False for light_id, light in lightstates.items(): if light['on'] == True: b.lights[light_id].state(on=True, bri=light['bri'], ct=light['ct']) else: b.lights[light_id].state(on=False) # Let's save the current state of the lights involved in this scene save_current_state(current_lightstates) return True
class HueController(BaseController): def init(self, *args, **kwargs): try: with open('.hueusername') as f: bridge_data = json.loads(f.read()) except: self.log("Bridge not authorised, need to press the button!", logging.WARN) bridge_data = json.loads( requests.get('https://www.meethue.com/api/nupnp').text)[0] bridge_data['username'] = create_new_username( bridge_data['internalipaddress']) with open('.hueusername', 'w') as f: f.write(json.dumps(bridge_data)) self.bridge = Bridge(bridge_data['internalipaddress'], bridge_data['username']) self.log("Successfully connected to Hue Bridge {}".format( bridge_data['internalipaddress'])) def print_all(self): self.log("Lights:") for room_id, room in self.bridge.groups().items(): self.log("{} [ID: {}] - {}:".format(room['type'], room_id, room['name'])) for light_id in room['lights']: light = self.bridge.lights()[light_id] self.log(" - [ID: {}]: {} ({})".format(light_id, light['name'], light['type'])) self.log(" ") self.log("Scenes:") for scene in self.bridge.scenes().values(): self.log(" - {}".format(scene['name'])) def set_light(self, id, *args, **kwargs): self.log("Setting light {}: {}".format(id, kwargs)) self.bridge.lights[id].state(**kwargs) def set_room(self, id, *args, **kwargs): self.log("Setting room {}: {}".format(id, kwargs)) self.bridge.groups[id].state(**kwargs) def adjust_light_brightness(self, id, *args, **kwargs): current_amount = self.bridge.lights[id]['brightness'] if kwargs['direction'] == 'up': new_amount = current_amount + kwargs.get('amount', 16) else: new_amount = current_amount - kwargs.get('amount', 16) self.set_light(id, **{'brightness': new_amount}) def set_scene(self, id, *args, **kwargs): scene_id = [ k for k, v in self.bridge.scenes().items() if v['name'] == kwargs['scene'] ] self.bridge.groups[id].state({'scene': scene_id}) def perform(self, action): kwargs = { k: v for k, v in action.items() if k not in ['action', 'id', 'type'] } id, act = action['id'], action['action'] if act == 'set_light': self.set_light(id, **kwargs) elif act == 'set_room': self.set_room(id, **kwargs) elif act == 'adjust_brightness': pass elif act == 'set_scene': self.set_scene(id, **kwargs) @classmethod def help(cls): return """