def start_server(self): try: http_server = HTTPServer(WSGIContainer(self.app)) http_server.listen(self.port) sleep(1) if not self.needs_update and not self.error and not self.new_user: # Autostart check if not self.args.silent: webbrowser.open(self.url) else: config = utility.get_config_dict() auto_start = config['autostart'] if auto_start: sb_controller.start() # New User / Error / Needs Update - skip autostart else: webbrowser.open(self.url) IOLoop.instance().start() # Handle port collision except socket.error: self.port += 1 self.start_server()
def get_lights_data(hue_ip, username): config = utility.get_config_dict() all_lights = [int(i) for i in config['all_lights'].split(',')] active_bulbs = [int(i) for i in config['active'].split(',')] lights = [] for counter, light in enumerate(all_lights): result = get_light(hue_ip, username, light) if type(result) is dict: # Skip unavailable lights state = result['state']['on'] light_name = result['name'] model_id = result['modelid'] bri = result['state']['bri'] # Setting defaults for non-color bulbs try: colormode = result['state']['colormode'] except KeyError: colormode = None try: xy = result['state']['xy'] except KeyError: xy = [] active = light if int(light) in active_bulbs else 0 light_data = [ light, state, light_name, active, model_id, bri, xy, colormode ] lights.append(light_data) return lights
def startup_checks(self): # Check For DLL error if params.BUILD == 'win': if not utility.dll_check(): self.url = self.base_url + 'dll-error' self.error = True return # Check if config file has been created yet if os.path.isfile(utility.get_config_path()): # Check to see if config needs to be updated if not utility.config_check(): self.url = self.base_url + 'update-config' self.needs_update = True return else: presets.update_presets_if_necessary() config = utility.get_config_dict() lights_initial_state = json.dumps(utility.get_hue_initial_state(config['ip'], config['username'])) # Init Screen object with some first-run defaults utility.write_config('App State', 'running', False) utility.write_config('Light Settings', 'default', lights_initial_state) sb_controller.init() self.url = self.base_url return else: # Config file doesn't exist, open New User interface self.url = self.base_url + 'new-user' self.new_user = True return
def save_new_preset(): json_to_write = utility.get_config_dict() fa_icons = utility.get_fa_class_names() icon = random.choice(fa_icons) if os.path.isfile(utility.get_json_filepath()): with open(utility.get_json_filepath()) as data_file: presets = json.load(data_file) preset_number = 0 for key in presets: new_preset_number = int(key[key.find('_') + 1:]) if new_preset_number > preset_number: preset_number = new_preset_number preset_number = str(preset_number + 1) new_key = 'preset_%s' % preset_number presets[new_key] = json_to_write presets[new_key]['preset_name'] = 'Preset %s' % preset_number presets[new_key]['preset_number'] = int(preset_number) presets[new_key]['icon_class'] = icon else: preset_name = 'preset_1' preset_number = 1 json_to_write['preset_name'] = 'Preset 1' json_to_write['preset_number'] = preset_number json_to_write['icon_class'] = icon presets = {preset_name: json_to_write} # Write/Rewrite presets.json with new section with open(utility.get_json_filepath(), 'w') as data_file: json.dump(presets, data_file) # print '\nSaved new Preset!' return preset_number
def get_index_data(): config_dict = utility.get_config_dict() hue_ip = config_dict['ip'] username = config_dict['username'] auto_start = config_dict['autostart'] current_preset = config_dict['current_preset'] update = config_dict['update'] update_buffer = config_dict['update_buffer'] max_bri = config_dict['max_bri'] min_bri = config_dict['min_bri'] bulb_settings = json.loads(config_dict['bulb_settings']) zones = ast.literal_eval(config_dict['zones']) zone_state = config_dict['zone_state'] display_index = config_dict['display_index'] sat = config_dict['sat'] party_mode = config_dict['party_mode'] state = config_dict['app_state'] lights = hue_interface.get_lights_data(hue_ip, username) for light in lights: light.append(int(bulb_settings[unicode(light[0])]['max_bri'])) light.append(int(bulb_settings[unicode(light[0])]['min_bri'])) presets = utility.get_all_presets() icon_size = 10 if len(lights) > 3: icon_size = 4 data = { 'auto_start_state': auto_start, 'update': update, 'update_buffer': update_buffer, 'max_bri': max_bri, 'min_bri': min_bri, 'default': config_dict['default'], 'lights': lights, 'lights_number': len(lights), 'icon_size': icon_size, 'username': username, 'party_mode': party_mode, 'zones': zones, 'zone_state': zone_state, 'display_index': display_index, 'sat': sat, 'presets': presets, 'current_preset': current_preset, 'state': state, } return data
def get_light_diagnostic_data(hue_ip, username): bridge = Bridge(device={'ip': hue_ip}, user={'name': username}) config = utility.get_config_dict() all_lights = [int(i) for i in config['all_lights'].split(',')] lights = {} for counter, light in enumerate(all_lights): resource = { 'which': light } result = bridge.light.get(resource) lights[light] = result return lights
def start_screenbloom(): config = utility.get_config_dict() state = config['app_state'] sb_controller.get_screen_object().bulb_state = 'on' hue_interface.lights_on_off('On') if state: message = 'ScreenBloom already running' else: sb_controller.re_initialize() sb_controller.start() message = 'ScreenBloom Started!' data = {'message': message} return data
def update_preset(preset_number, preset_name, icon): with open(utility.get_json_filepath()) as data_file: presets = json.load(data_file) preset_to_edit = None for preset in presets: if int(preset_number) == presets[preset]['preset_number']: preset_to_edit = preset preset_number = presets[preset_to_edit]['preset_number'] presets[preset_to_edit] = utility.get_config_dict() presets[preset_to_edit]['preset_name'] = preset_name presets[preset_to_edit]['icon_class'] = icon presets[preset_to_edit]['preset_number'] = preset_number with open(utility.get_json_filepath(), 'w') as f: json.dump(presets, f)
def start_screenbloom(): config = utility.get_config_dict() state = config['color_mode_enabled'] sb_controller.get_screen_object().bulb_state = 'on' hue_interface.lights_on_off('On') if state: message = 'ScreenBloom already running' else: utility.write_config('Configuration', 'color_mode_enabled', True) sb_controller.re_initialize() sb_controller.start() message = 'Color Mode Started!' data = {'message': message} return data
def start_screenbloom(): config = utility.get_config_dict() state = config['app_state'] sb_controller.get_screen_object().bulb_state = 'on' hue_interface.lights_on_off('On') if state: message = 'ScreenBloom already running' else: sb_controller.re_initialize() sb_controller.start() message = 'ScreenBloom Started!' data = { 'message': message } return data
def initialize(): config_dict = utility.get_config_dict() ip = config_dict['ip'] username = config_dict['username'] max_bri = config_dict['max_bri'] min_bri = config_dict['min_bri'] active_lights = [int(i) for i in config_dict['active'].split(',')] all_lights = [int(i) for i in config_dict['all_lights'].split(',')] # Check selected bulbs vs all known bulbs bulb_list = [] for counter, bulb in enumerate(all_lights): if active_lights[counter]: bulb_list.append(active_lights[counter]) else: bulb_list.append(0) bulb_settings = json.loads(config_dict['bulb_settings']) update = config_dict['update'] update_buffer = config_dict['update_buffer'] default = config_dict['default'] zones = ast.literal_eval(config_dict['zones']) zone_state = bool(config_dict['zone_state']) party_mode = bool(config_dict['party_mode']) display_index = config_dict['display_index'] sat = config_dict['sat'] bbox = None if params.BUILD == 'win': from desktopmagic.screengrab_win32 import getDisplayRects bbox = getDisplayRects()[int(display_index)] return ip, username, bulb_list, bulb_settings, default, [], \ update, update_buffer, max_bri, min_bri, zones, zone_state, \ display_index, party_mode, sat, bbox
def initialize(): config_dict = utility.get_config_dict() ip = config_dict['ip'] username = config_dict['username'] bridge = Bridge(device={'ip': ip}, user={'name': username}) max_bri = config_dict['max_bri'] min_bri = config_dict['min_bri'] active_lights = [int(i) for i in config_dict['active'].split(',')] all_lights = [int(i) for i in config_dict['all_lights'].split(',')] # Check selected bulbs vs all known bulbs bulb_list = [] for counter, bulb in enumerate(all_lights): if active_lights[counter]: bulb_list.append(active_lights[counter]) else: bulb_list.append(0) bulb_settings = json.loads(config_dict['bulb_settings']) update = config_dict['update'] update_buffer = config_dict['update_buffer'] default = [int(i) for i in config_dict['default'].split(',')] black_rgb = [int(i) for i in config_dict['black_rgb'].split(',')] zones = ast.literal_eval(config_dict['zones']) zone_state = bool(config_dict['zone_state']) party_mode = bool(config_dict['party_mode']) display_index = config_dict['display_index'] color_mode_enabled = config_dict['color_mode_enabled'] color_mode = config_dict['color_mode'] return bridge, ip, username, bulb_list, bulb_settings, default, default, \ update, update_buffer, max_bri, min_bri, zones, zone_state, color_mode, \ black_rgb, display_index, party_mode, color_mode_enabled
def start_server(app, startup_thread): try: http_server = HTTPServer(WSGIContainer(app)) http_server.listen(startup_thread.port) if not startup_thread.args.silent: startup_thread.start() else: config = utility.get_config_dict() auto_start = config['autostart'] sb_controller.start() if auto_start: view_logic.start_screenbloom() IOLoop.instance().start() # Handle port collision except socket.error: startup_thread.port += 1 start_server(app, startup_thread)
def initialize(): config_dict = utility.get_config_dict() ip = config_dict['ip'] username = config_dict['username'] bridge = Bridge(device={'ip': ip}, user={'name': username}) max_bri = config_dict['max_bri'] min_bri = config_dict['min_bri'] active_lights = [int(i) for i in config_dict['active'].split(',')] all_lights = [int(i) for i in config_dict['all_lights'].split(',')] # Check selected bulbs vs all known bulbs bulb_list = [] for counter, bulb in enumerate(all_lights): if active_lights[counter]: bulb_list.append(active_lights[counter]) else: bulb_list.append(0) bulb_settings = json.loads(config_dict['bulb_settings']) update = config_dict['update'] update_buffer = config_dict['update_buffer'] default = config_dict['default'] zones = ast.literal_eval(config_dict['zones']) zone_state = bool(config_dict['zone_state']) party_mode = bool(config_dict['party_mode']) display_index = config_dict['display_index'] sat = config_dict['sat'] return bridge, ip, username, bulb_list, bulb_settings, default, [], \ update, update_buffer, max_bri, min_bri, zones, zone_state, \ display_index, party_mode, sat
def get_lights_data(hue_ip, username): bridge = Bridge(device={'ip': hue_ip}, user={'name': username}) config = utility.get_config_dict() all_lights = [int(i) for i in config['all_lights'].split(',')] active_bulbs = [int(i) for i in config['active'].split(',')] lights = [] for counter, light in enumerate(all_lights): resource = { 'which': light } result = bridge.light.get(resource) if type(result['resource']) is dict: # Skip unavailable lights state = result['resource']['state']['on'] light_name = result['resource']['name'] model_id = result['resource']['modelid'] bri = result['resource']['state']['bri'] # Setting defaults for non-color bulbs try: colormode = result['resource']['state']['colormode'] except KeyError: colormode = None try: xy = result['resource']['state']['xy'] except KeyError: xy = [] active = light if int(light) in active_bulbs else 0 light_data = [light, state, light_name, active, model_id, bri, xy, colormode] lights.append(light_data) return lights
def save_new_preset(): json_to_write = utility.get_config_dict() fa_icons = utility.get_fa_class_names() icon = random.choice(fa_icons) if os.path.isfile(utility.get_json_filepath()): with open(utility.get_json_filepath()) as data_file: presets = json.load(data_file) preset_number = 0 for key in presets: new_preset_number = int(key[key.find('_') + 1:]) if new_preset_number > preset_number: preset_number = new_preset_number preset_number = str(preset_number + 1) new_key = 'preset_%s' % preset_number presets[new_key] = json_to_write presets[new_key]['preset_name'] = 'Preset %s' % preset_number presets[new_key]['preset_number'] = int(preset_number) presets[new_key]['icon_class'] = icon else: preset_name = 'preset_1' preset_number = 1 json_to_write['preset_name'] = 'Preset 1' json_to_write['preset_number'] = preset_number json_to_write['icon_class'] = icon presets = { preset_name: json_to_write } # Write/Rewrite presets.json with new section with open(utility.get_json_filepath(), 'w') as data_file: json.dump(presets, data_file) # print '\nSaved new Preset!' return preset_number
def get_lights_data(hue_ip, username): bridge = Bridge(device={'ip': hue_ip}, user={'name': username}) config = utility.get_config_dict() all_lights = [int(i) for i in config['all_lights'].split(',')] active_bulbs = [int(i) for i in config['active'].split(',')] lights = [] for counter, light in enumerate(all_lights): resource = {'which': light} result = bridge.light.get(resource) # Skip unavailable lights if type(result['resource']) is dict: state = result['resource']['state']['on'] light_name = result['resource']['name'] model_id = result['resource']['modelid'] active = light if int(light) in active_bulbs else 0 light_data = [light, state, light_name, active, model_id] lights.append(light_data) return lights
def update_presets_if_necessary(): needs_update = False config = utility.get_config_dict() all_lights = hue_interface.get_lights_list(config['ip'], config['username']) all_lights_list = [int(i) for i in all_lights] all_lights_str = ','.join([str(i) for i in all_lights_list]) # Return if presets file does not exist yet try: with open(utility.get_json_filepath()) as data_file: presets = json.load(data_file) except IOError: return current_light_settings = utility.get_current_light_settings() presets_to_write = {} for preset_name in presets: # Check each preset for key errors (new values needing defaults) preset = presets[preset_name] bulbs = json.loads(preset['bulb_settings']) # Check if active bulbs needs to be updated active_bulbs = preset['active'] active_bulbs_list = active_bulbs.split(',') new_active_bulbs = [] for index, bulb_id in enumerate(all_lights_list): try: bulb = int(active_bulbs_list[index]) new_active_bulbs.append(bulb) except IndexError: needs_update = True new_active_bulbs.append(0) active_bulbs = ','.join([str(i) for i in new_active_bulbs]) # Add new bulb to current_light_settings if necessary for bulb_id in current_light_settings: try: bulb = bulbs[bulb_id] except KeyError: # Add new bulb with default values needs_update = True bulb = current_light_settings[bulb_id] bulb['max_bri'] = 254 bulb['min_bri'] = 1 bulbs[bulb_id] = bulb for bulb_id in bulbs: bulb = bulbs[bulb_id] bulb_current_settings = current_light_settings[str(bulb_id)] # Check bulbs for missing key->value pairs here try: model_id = bulb['model_id'] except KeyError: needs_update = True bulb['model_id'] = bulb_current_settings['model_id'] try: gamut = bulb['gamut'] except KeyError: needs_update = True bulb['gamut'] = bulb_current_settings['gamut'] try: name = bulb['name'] except KeyError: needs_update = True bulb['name'] = bulb_current_settings['name'] # Version 2.2 Updates ################################################# try: sat = preset['sat'] except KeyError: needs_update = True sat = 1.0 if needs_update: preset['bulb_settings'] = json.dumps(bulbs) preset['active'] = active_bulbs preset['all_lights'] = all_lights_str preset['sat'] = sat presets_to_write[preset_name] = preset if needs_update: # print 'Updating presets...' with open(utility.get_json_filepath(), 'w') as f: json.dump(presets_to_write, f) current_preset = config['current_preset'] if current_preset: for key in presets: preset = presets[key] name = preset['preset_name'] if name == current_preset: preset_number = key[key.find('_') + 1:] # print 'Applying %s...' % str(key) apply_preset(preset_number)
def get_index_data(): config_dict = utility.get_config_dict() hue_ip = config_dict['ip'] username = config_dict['username'] auto_start = config_dict['autostart'] current_preset = config_dict['current_preset'] color_mode_enabled = config_dict['color_mode_enabled'] update = config_dict['update'] update_buffer = config_dict['update_buffer'] max_bri = config_dict['max_bri'] min_bri = config_dict['min_bri'] bulb_settings = json.loads(config_dict['bulb_settings']) default_color = config_dict['default'].split(',') default_color = Color(rgb=(int(default_color[0]) / 255.0, int(default_color[1]) / 255.0, int(default_color[2]) / 255.0)).hex black = config_dict['black_rgb'].split(',') zones = ast.literal_eval(config_dict['zones']) zone_state = config_dict['zone_state'] color_mode = config_dict['color_mode'] display_index = config_dict['display_index'] party_mode = config_dict['party_mode'] lights = hue_interface.get_lights_data(hue_ip, username) for light in lights: light.append(int(bulb_settings[unicode(light[0])]['max_bri'])) light.append(int(bulb_settings[unicode(light[0])]['min_bri'])) filepath = utility.get_json_filepath() presets = [] if os.path.isfile(filepath): with open(filepath) as data_file: presets = json.load(data_file) icon_size = 10 if len(lights) > 3: icon_size = 4 data = { 'auto_start_state': auto_start, 'color_mode_enabled': color_mode_enabled, 'update': update, 'update_buffer': update_buffer, 'max_bri': max_bri, 'min_bri': min_bri, 'default': config_dict['default'], 'default_color': default_color, 'black_rgb': black, 'lights': lights, 'lights_number': len(lights), 'icon_size': icon_size, 'username': username, 'party_mode': party_mode, 'zones': zones, 'zone_state': zone_state, 'display_index': display_index, 'presets': presets, 'current_preset': current_preset, 'color_mode': color_mode } return data