Skip to content

Update upslite_plugin_1_3.py#7

Open
milanojs wants to merge 3 commits intoitsdarklikehell:masterfrom
milanojs:patch-1
Open

Update upslite_plugin_1_3.py#7
milanojs wants to merge 3 commits intoitsdarklikehell:masterfrom
milanojs:patch-1

Conversation

@milanojs
Copy link
Copy Markdown

@milanojs milanojs commented Mar 29, 2025

Enhance the code and fix the "startup" on the i2get interface so the battery % can be shown in the screen

Summary by Sourcery

Enhance the UPSLite plugin for Pwnagotchi to improve battery status reading and error handling for the UPS Lite v1.3 interface

New Features:

  • Added QuickStart method to initialize battery chip
  • Implemented more comprehensive error checking in battery methods

Bug Fixes:

  • Fixed battery capacity reading calculation
  • Added error handling for I2C communication failures
  • Improved charging status detection

Enhancements:

  • Improved error handling and logging for battery status reading
  • More robust initialization of UPS interface
  • Added detailed logging for plugin operations

Enhance the code and fix the "startup" on the i2get interface so the battery % can be shown in the screen
@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Mar 29, 2025

Reviewer's Guide by Sourcery

This pull request enhances the UPSLite plugin by improving error handling, implementing a QuickStart command for the CW2015 chip, improving capacity and charging status readings, and improving UI updates and error handling. It also adds plugin metadata.

Sequence diagram for UPSLite QuickStart

sequenceDiagram
  participant P as Pwnagotchi
  participant U as UPSLite Plugin
  participant UPS
  participant SMB as SMBus

  P->>U: on_loaded()
  U->>UPS: __init__()
  UPS->>UPS: _quick_start()
  UPS->>SMB: write_byte_data(CW2015_ADDRESS, CW2015_REG_MODE, CW2015_QUICKSTART_VAL)
  SMB-->>UPS: OK
  UPS->>UPS: time.sleep(0.1)
  UPS-->>U: UPS object
  U-->>P: Plugin loaded
Loading

Sequence diagram for UPSLite UI Update

sequenceDiagram
  participant P as Pwnagotchi
  participant U as UPSLite Plugin
  participant UPS
  participant SMB as SMBus
  participant GPIO

  P->>U: on_ui_update()
  U->>UPS: capacity()
  UPS->>SMB: read_word_data(CW2015_ADDRESS, CW2015_REG_SOC)
  SMB-->>UPS: capacity_data
  UPS-->>U: capacity
  U->>UPS: charging()
  UPS->>GPIO: input(GPIO_PIN_CHARGING)
  GPIO-->>UPS: charging_status
  UPS-->>U: charging
  U->>P: ui.set("ups", "%2i%%%s" % (int(round(capacity)), charging))
Loading

Updated class diagram for UPS

classDiagram
  class UPS {
    -bus
    +__init__()
    +_quick_start()
    +voltage()
    +capacity()
    +charging()
  }

  note for UPS "Initializes SMBus and GPIO, implements QuickStart, and reads voltage, capacity, and charging status."
Loading

File-Level Changes

Change Details Files
Improved error handling and logging for I2C and GPIO operations.
  • Added try-except blocks to catch potential IOError and other exceptions during I2C reads and writes.
  • Used logging.debug for potentially frequent I2C errors and logging.error for unexpected errors.
  • Implemented error handling for GPIO setup and read operations, providing informative error messages.
  • Return a default value on error, so the program doesn't crash.
upslite_plugin_1_3.py
Implemented QuickStart command to initialize the CW2015 chip.
  • Added _quick_start method to send the QuickStart command to the CW2015 chip.
  • Included a short delay after sending the QuickStart command to allow the chip to process it.
  • Added error handling for the QuickStart command to catch potential IOError exceptions.
  • The quick start command is called when the UPS class is initialized.
upslite_plugin_1_3.py
Improved capacity reading and handling.
  • Used explicit float division when calculating capacity to ensure accurate results.
  • Clamped the capacity value between 0 and 100 to prevent weird readings.
  • Return a default value on error, so the program doesn't crash.
upslite_plugin_1_3.py
Improved charging status reading and handling.
  • The GPIO is initialized only once, when the UPS class is initialized.
  • Return a default value on error, so the program doesn't crash.
upslite_plugin_1_3.py
Improved UI update and error handling.
  • Added a check to ensure the UPS object was initialized successfully before attempting to update the UI.
  • Added error handling for UI updates to catch potential exceptions.
  • Format the capacity to an integer percentage.
  • Display a default value if the UPS object failed to initialize.
upslite_plugin_1_3.py
Added plugin metadata.
  • Added author and version to the plugin metadata.
  • Added a description to the plugin metadata.
  • Added a help message to the plugin metadata.
upslite_plugin_1_3.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@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 @milanojs - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a try-except block around the plugin's on_ui_update method to gracefully handle potential errors during UI updates.
  • The multiple assignments to __author__, __version__, __license__, and __description__ should be consolidated.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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 and I'll use the feedback to improve your reviews.

Comment thread upslite_plugin_1_3.py Outdated
Comment on lines +71 to +80
# Use write_byte_data for single byte register, or write_word_data if required by specific smbus implementation/chip behavior
# The manufacturer script used write_word_data, let's stick to that for consistency maybe?
# bus.write_word_data(CW2015_ADDRESS, CW2015_REG_MODE, CW2015_QUICKSTART_VAL)
# Let's try write_byte_data first as MODE is a single register 0x0A
self._bus.write_byte_data(CW2015_ADDRESS, CW2015_REG_MODE, CW2015_QUICKSTART_VAL)
log.info("UPSLite: CW2015 QuickStart command sent (wrote 0x%02X to 0x%02X)", CW2015_QUICKSTART_VAL, CW2015_REG_MODE)
time.sleep(0.1) # Give chip a moment
except IOError as e:
log.error("UPSLite: Error sending QuickStart command: %s", e)
except Exception as e:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue: Duplicate/conflicting plugin metadata definitions.

Both the author and version (as well as description and license) are defined twice with differing values. Consolidating these definitions into a single consistent block will reduce confusion and prevent accidental mismatches.

Comment thread upslite_plugin_1_3.py
review the on update ui functions by recommendations give for sourcery
create the decorator and enhance code in general
@milanojs milanojs closed this Mar 29, 2025
@milanojs milanojs reopened this Mar 29, 2025
@codacy-production
Copy link
Copy Markdown

Codacy's Analysis Summary

0 new issue (≤ 0 issue)
0 new security issue
19 complexity
-9 duplications

Review Pull Request in Codacy →

AI Reviewer available: add the codacy-review label to get contextual insights without leaving GitHub.

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