def md_action_changed(button_index: int, action: str) -> None: """ Handles changing the type of action Material Deck listens for. """ global _ui # pylint: disable=global-statement,invalid-name button_state = deck_api._button_state( # pylint: disable=protected-access _deck_id, deck_api.get_page(_deck_id), button_index) page = deck_api.get_page(_deck_id) if action: deck_info: dict = button_state.setdefault('material_deck', {}) init_data = deck_info.setdefault( 'init_data', { 'event': 'willAppear', 'payload': { 'settings': { 'displayName': True, 'displayIcon': True, 'displaySceneIcon': True, 'displaySceneName': True, }, }, }) init_data['action'] = action command = _create_command_payload(action, 'keyDown', button_index, page) deck_api.set_button_command( _deck_id, page, button_index, f"{CONFIG_PATH}/pipe_writer.sh '{_bridge_file}' '{json.dumps(command)}'" ) _write_message( json.dumps( _create_will_display_payload(deck_info, button_index, page))) elif 'material_deck' in button_state: previous_action = button_state['material_deck'].get('init_data', {}).get( 'action', '') del button_state['material_deck'] _reset_button(button_index, '', page) _write_message( json.dumps({ 'event': 'willDisappear', 'action': previous_action, 'payload': { 'coordinates': { 'column': button_index % DECK_COLUMNS, 'row': math.floor(button_index / DECK_COLUMNS), }, }, 'context': _to_context(button_index, page), 'device': _deck_id, })) deck_api.export_config(DECK_CONFIG_FILE) deck_gui.redraw_buttons(_ui)
def key_up_callback(deck_id: str, key: int, state: bool) -> None: """ Handles key up events by sending the necessary information to the output queue. """ if state: log.warning('Released callback called while the state boolean is true') button_state = deck_api._button_state(deck_id, deck_api.get_page(_deck_id), key) # pylint: disable=protected-access md_info = button_state.get('material_deck') if md_info: _write_message( json.dumps( _create_command_payload(md_info['init_data']['action'], 'keyUp', key, deck_api.get_page(_deck_id))))
def load_md_buttons(page: Optional[int] = None) -> None: """ Reads the buttons configured for use with MaterialDeck and sends willAppear events. """ if _deck_id not in deck_api.state: return if page is None: page = deck_api.get_page(_deck_id) button_states = deck_api.state[_deck_id]['buttons'] for button_index, button_info in button_states.get(page, {}).items(): if 'material_deck' in button_info: deck_info = button_info['material_deck'] if 'init_data' in deck_info: init_data = _create_will_display_payload( deck_info, button_index, page) command = _create_command_payload(init_data['action'], 'keyDown', button_index, page) deck_api.set_button_command( _deck_id, page, button_index, f"{CONFIG_PATH}/pipe_writer.sh '{_bridge_file}' '{json.dumps(command)}'" ) _write_message(json.dumps(init_data))
def get_md_action(button_index: int) -> str: """ Returns the md_action for the specified button. """ button_state = deck_api._button_state( # pylint: disable=protected-access _deck_id, deck_api.get_page(_deck_id), button_index) return button_state.get('material_deck', {}).get('init_data', {}).get('action', '') # type: ignore
def get_md_data(button_index: int) -> str: """ Returns the md_data for the specified button """ button_state = deck_api._button_state( # pylint: disable=protected-access _deck_id, deck_api.get_page(_deck_id), button_index) deck_info = button_state.get('material_deck') if deck_info: return json.dumps(deck_info.setdefault('action_settings', {})) return ''
def build_device(ui, _device_index=None) -> None: for page_id in range(ui.pages.count()): page = ui.pages.widget(page_id) page.setStyleSheet("background-color: black") build_buttons(ui, page) # Set the active page for this device ui.pages.setCurrentIndex(api.get_page(_deck_id(ui))) # Draw the buttons for the active page redraw_buttons(ui) sync(ui) _highlight_first_button(ui)
def md_data_changed(button_index: int, text: str) -> None: """ Handles when data is changed within the md data box. """ try: json.loads(text) except Exception: # pylint: disable=broad-except return page = deck_api.get_page(_deck_id) button_state = deck_api._button_state(_deck_id, page, button_index) # pylint: disable=protected-access deck_info = button_state.get('material_deck', {}) if deck_info: deck_info['action_settings'] = json.loads(text) _write_message( json.dumps( _create_will_display_payload(deck_info, button_index, page)))
def sync(ui) -> None: api.ensure_decks_connected() ui.brightness.setValue(api.get_brightness(_deck_id(ui))) ui.pages.setCurrentIndex(api.get_page(_deck_id(ui)))
def _set_page_override(deck_id: str, page: int) -> None: original_page = deck_api.get_page(deck_id) _original_set_page(deck_id, page) _on_page_changed(original_page, page)
def sync(ui) -> None: api.ensure_decks_connected() ui.pages.setCurrentIndex(api.get_page(_deck_id(ui)))
def handle_keypress(deck_id: str, key: int, state: bool) -> None: if state: if dimmers[deck_id].reset(): return keyboard = Controller() page = api.get_page(deck_id) command = api.get_button_command(deck_id, page, key) if command: try: Popen(shlex.split(command)) except Exception as error: print(f"The command '{command}' failed: {error}") keys = api.get_button_keys(deck_id, page, key) if keys: keys = keys.strip().replace(" ", "") for section in keys.split(","): # Since + and , are used to delimit our section and keys to press, # they need to be substituded with keywords. section_keys = [_replace_special_keys(key_name) for key_name in section.split("+")] # Translate string to enum, or just the string itself if not found section_keys = [getattr(Key, key_name.lower(), key_name) for key_name in section_keys] for key_name in section_keys: try: if key_name == "delay": time.sleep(0.5) else: keyboard.press(key_name) except Exception: print(f"Could not press key '{key_name}'") for key_name in section_keys: try: if key_name != "delay": keyboard.release(key_name) except Exception: print(f"Could not release key '{key_name}'") write = api.get_button_write(deck_id, page, key) if write: try: keyboard.type(write) except Exception as error: print(f"Could not complete the write command: {error}") brightness_change = api.get_button_change_brightness(deck_id, page, key) if brightness_change: try: api.change_brightness(deck_id, brightness_change) dimmers[deck_id].brightness = api.get_brightness(deck_id) dimmers[deck_id].reset() except Exception as error: print(f"Could not change brightness: {error}") switch_page = api.get_button_switch_page(deck_id, page, key) if switch_page: api.set_page(deck_id, switch_page - 1)
def handle_keypress(deck_id: str, key: int, state: bool) -> None: if state: if dimmers[deck_id].reset(): return keyboard = Controller() page = api.get_page(deck_id) command = api.get_button_command(deck_id, page, key) if command: try: Popen(shlex.split(command)) except Exception as error: print(f"The command '{command}' failed: {error}") keys = api.get_button_keys(deck_id, page, key) if keys: keys = keys.strip().replace(" ", "") for section in keys.split(","): # Since + and , are used to delimit our section and keys to press, # they need to be substituted with keywords. section_keys = [ _replace_special_keys(key_name) for key_name in section.split("+") ] # Translate string to enum, or just the string itself if not found section_keys = [ getattr(Key, key_name.lower(), key_name) for key_name in section_keys ] for key_name in section_keys: if isinstance(key_name, str) and key_name.startswith("delay"): sleep_time_arg = key_name.split("delay", 1)[1] if sleep_time_arg: try: sleep_time = float(sleep_time_arg) except Exception: print( f"Could not convert sleep time to float '{sleep_time_arg}'" ) sleep_time = 0 else: # default if not specified sleep_time = 0.5 if sleep_time: try: time.sleep(sleep_time) except Exception: print( f"Could not sleep with provided sleep time '{sleep_time}'" ) else: try: keyboard.press(key_name) except Exception: print(f"Could not press key '{key_name}'") for key_name in section_keys: if not (isinstance(key_name, str) and key_name.startswith("delay")): try: keyboard.release(key_name) except Exception: print(f"Could not release key '{key_name}'") write = api.get_button_write(deck_id, page, key) if write: try: keyboard.type(write) except Exception as error: print(f"Could not complete the write command: {error}") brightness_change = api.get_button_change_brightness( deck_id, page, key) if brightness_change: try: api.change_brightness(deck_id, brightness_change) dimmers[deck_id].brightness = api.get_brightness(deck_id) dimmers[deck_id].reset() except Exception as error: print(f"Could not change brightness: {error}") switch_page = api.get_button_switch_page(deck_id, page, key) target_device = api.get_target_device(deck_id, page, key) if switch_page: api.set_page(target_device, switch_page - 1)