Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow discovering Broadlink device at a specific IP #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cdzombak
Copy link

@cdzombak cdzombak commented Sep 25, 2024

Summary by Sourcery

Introduce functionality to discover Broadlink RM blasters at a specific IP address, enhancing the discovery process for locked or otherwise undiscoverable devices. Update the documentation to reflect these changes and provide additional user guidance.

New Features:

  • Add the ability to discover a Broadlink RM blaster at a specific IP address using the /discoverblasters?at_ip=<value> endpoint.

Enhancements:

  • Update the get_new_blasters function to accept an optional at_ip parameter for targeted blaster discovery.

Documentation:

  • Update the README to document the new /discoverblasters?at_ip=<value> endpoint and provide additional guidance on unlocking blasters for control.

Copy link
Contributor

sourcery-ai bot commented Sep 25, 2024

Reviewer's Guide by Sourcery

This pull request adds functionality to discover a Broadlink device at a specific IP address. It modifies the get_new_blasters function in blaster_db.py to accept an optional at_ip parameter, updates the /discoverblasters endpoint in app.py to handle this new parameter, and updates the README.md with information about the new feature and a note about locked blasters.

Sequence Diagram

sequenceDiagram
    participant C as Client
    participant A as API
    participant B as Blaster DB
    participant D as Broadlink Device
    C->>A: GET /discoverblasters?at_ip=<value>
    A->>B: get_new_blasters(at_ip=<value>)
    alt at_ip provided
        B->>D: broadlink.hello(at_ip)
        D-->>B: Device info
    else at_ip not provided
        B->>D: discover_blasters()
        D-->>B: List of devices
    end
    B-->>A: Discovered device(s)
    A-->>C: JSON response with discovered device(s)
Loading

File-Level Changes

Change Details Files
Added ability to discover a Broadlink device at a specific IP address
  • Modified get_new_blasters function to accept an optional at_ip parameter
  • Added logic to discover a device at a specific IP if at_ip is provided
  • Updated the discovery process to use the new at_ip parameter if available
app/db_helpers/blaster_db.py
Updated API endpoint to support discovering a device at a specific IP
  • Modified the DiscoverRESTResource class to pass the at_ip parameter to get_new_blasters
app/app.py
Updated documentation in README.md
  • Added information about the new /discoverblasters?at_ip=<value> endpoint
  • Included a note about locked blasters and how to unlock them in the Broadlink app
README.md

Tips and commands
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @cdzombak - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 2 issues found
  • 🟡 Security: 1 issue found
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟡 Documentation: 1 issue found

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@@ -142,10 +142,18 @@ def discover_blasters(timeout):
]


def get_new_blasters(timeout=DISCOVERY_TIMEOUT):
def get_new_blasters(at_ip=None, timeout=DISCOVERY_TIMEOUT):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider adding error handling for broadlink.hello(at_ip) call

The function should handle potential exceptions or errors that may occur when calling broadlink.hello(at_ip). This will improve the robustness of the code.

def get_new_blasters(at_ip=None, timeout=DISCOVERY_TIMEOUT):
    try:
        cnt = 0
        discovered = []
        if at_ip:
            broadlink.hello(at_ip)
    except Exception as e:
        logging.error(f"Error discovering blasters: {e}")
        return []

@@ -142,10 +142,18 @@ def discover_blasters(timeout):
]


def get_new_blasters(timeout=DISCOVERY_TIMEOUT):
def get_new_blasters(at_ip=None, timeout=DISCOVERY_TIMEOUT):
cnt = 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Remove unused variable 'cnt'

The 'cnt' variable is initialized but never used. Consider removing it to improve code clarity.

Suggested change
cnt = 0
def get_new_blasters(at_ip=None, timeout=DISCOVERY_TIMEOUT):
discovered = []

@@ -86,7 +86,7 @@ def process_response(self, req, resp, resource, req_succeeded):

class DiscoverRESTResource(object):
def on_get(self, req, resp):
resp.body = json.dumps(blaster_db.get_new_blasters())
resp.body = json.dumps(blaster_db.get_new_blasters(at_ip=req.get_param("at_ip", required=False)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚨 suggestion (security): Consider adding input validation for the 'at_ip' parameter

While the 'at_ip' parameter is optional, it's important to validate it as a valid IP address when provided. This will enhance the security and reliability of the API.

from ipaddress import ip_address

def validate_ip(ip):
    try:
        ip_address(ip)
        return True
    except ValueError:
        return False

at_ip = req.get_param("at_ip", required=False)
if at_ip and not validate_ip(at_ip):
    raise ValueError("Invalid IP address provided")
resp.body = json.dumps(blaster_db.get_new_blasters(at_ip=at_ip))

@@ -84,6 +85,7 @@ Endpoint | HTTP Method | Description
2. The database files are in SQLite3 format and can be hand edited if needed. I use [SQLiteStudio](https://sqlitestudio.pl/index.rvt).
3. To reset all settings/data, simply stop the container/app, delete the two .db files, and restart.
4. This was tested on an RM3 Mini but should theoretically support any RM device that [python-broadlink](https://github.com/mjg59/python-broadlink) does.
5. Blasters must be unlocked (accessible via WLAN) to be controlled via this program, otherwise you may get `Authentication failed` errors (see [mjg59/python-broadlink#719](https://github.com/mjg59/python-broadlink/issues/719)). To unlock a blaster in the Broadlink app: select the device, tap "..." in the top tap, click "Property", then find the "Lock device" setting and turn it off.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (documentation): Fix typo: 'top tap' should be 'top right'

Suggested change
5. Blasters must be unlocked (accessible via WLAN) to be controlled via this program, otherwise you may get `Authentication failed` errors (see [mjg59/python-broadlink#719](https://github.com/mjg59/python-broadlink/issues/719)). To unlock a blaster in the Broadlink app: select the device, tap "..." in the top tap, click "Property", then find the "Lock device" setting and turn it off.
5. Blasters must be unlocked (accessible via WLAN) to be controlled via this program, otherwise you may get `Authentication failed` errors (see [mjg59/python-broadlink#719](https://github.com/mjg59/python-broadlink/issues/719)). To unlock a blaster in the Broadlink app: select the device, tap "..." in the top right, click "Property", then find the "Lock device" setting and turn it off.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant