Skip to content

Commit

Permalink
Fix 'Go Back' bug in the menu
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdelis committed Dec 22, 2024
1 parent 472b32b commit f780348
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ To get started with Broadlink Reader, clone the repository from GitHub:
```
git clone https://github.com/JexSrs/broadlink-reader.git
cd broadlink-reader
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
```

## Usage
Expand Down
5 changes: 2 additions & 3 deletions bdlk.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import base64
from time import sleep
from broadlink import Device

import broadlink
import base64

from broadlink import Device
from colorama import Fore


Expand Down
33 changes: 25 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import ipaddress
import argparse
import json
import os

from broadlink import Device
from colorama import Fore, init
from utils import get_action_color, print_keys

import bdlk
import json
import os
import argparse
from utils import print_keys

init(autoreset=True)

Expand All @@ -22,9 +21,14 @@ def navigate_actions(device: Device, actions):

current_actions = actions
path = []
display_path = []

while True:
print("\nChoose an action by index to register:")
if display_path:
print(Fore.LIGHTBLUE_EX + f'\nCurrent path: {' > '.join(display_path)}')
else:
print(Fore.LIGHTBLUE_EX + '\nHome Page')
print("Choose an action by index to register:")
print(Fore.WHITE + "-1: " + ("Go back" if path else "Exit"))
action_keys = print_keys(current_actions)

Expand All @@ -39,11 +43,22 @@ def navigate_actions(device: Device, actions):
if path:
# Go back one level
current_actions = path.pop()

# Fix "Go back" bug
for action in reversed(display_path):
if action in current_actions:
if not isinstance(current_actions[action], dict) and path:
current_actions = path.pop()
display_path.pop()

display_path.pop()
else:
break
elif 0 <= choice < len(action_keys):
# Save current level
selected_action = action_keys[choice]
if selected_action not in display_path:
display_path.append(selected_action)
path.append(current_actions)

# Go deeper into the next level
Expand Down Expand Up @@ -77,8 +92,10 @@ def main():
parser.add_argument('--ip', required=True, help='IP Address of the Broadlink device')
parser.add_argument('--port', type=int, default=80, help='Port of the Broadlink device (default: 80)')
parser.add_argument('--timeout', type=int, default=10, help='Timeout for device connection (default: 10)')
parser.add_argument('--file', type=str, default='commands.json', help='Output file for saving learned commands (default: commands.json)')
parser.add_argument('--read', type=int, default=2, help='Time to wait for the user to send the command (default: 2 seconds)')
parser.add_argument('--file', type=str, default='commands.json',
help='Output file for saving learned commands (default: commands.json)')
parser.add_argument('--read', type=int, default=2,
help='Time to wait for the user to send the command (default: 2 seconds)')
args = parser.parse_args()

read_timeout = args.read
Expand Down
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
broadlink==0.19.0
cffi==1.17.1
colorama==0.4.6
cryptography==44.0.0
pycparser==2.22

0 comments on commit f780348

Please sign in to comment.