Skip to content

refactor: extract firmware check logic into a dedicated helper method to reduce duplication#527

Merged
firstof9 merged 2 commits intomainfrom
suggested-fixes
Mar 4, 2026
Merged

refactor: extract firmware check logic into a dedicated helper method to reduce duplication#527
firstof9 merged 2 commits intomainfrom
suggested-fixes

Conversation

@firstof9
Copy link
Owner

@firstof9 firstof9 commented Mar 4, 2026

Summary by CodeRabbit

  • Refactor
    • Redesigned firmware check internals to reduce duplication and centralize response handling, improving maintainability.
  • Bug Fixes
    • Improved error resilience for firmware update checks: handles malformed or unexpected API responses and additional network/timeout errors to avoid false failures and provide more reliable update status.

@coderabbitai
Copy link

coderabbitai bot commented Mar 4, 2026

📝 Walkthrough

Walkthrough

Refactors OpenEVSE.firmware_check to extract session-based HTTP logic into a new private helper _firmware_check_with_session, consolidates JSON parsing/response construction, and expands exception handling to include ContentTypeError and aiohttp.ClientConnectorError.

Changes

Cohort / File(s) Summary
Core refactor
openevsehttp/__main__.py
Adds _firmware_check_with_session(session, url, method) and routes both session and no-session code paths through it; centralizes request/response parsing and expands exception handling to include aiohttp.ContentTypeError and aiohttp.ClientConnectorError.
Tests
tests/test_main.py
Adds handling for JSONDecodeError and non-dict JSON responses in firmware-check tests to validate resilience of the new parsing behavior.

Sequence Diagram(s)

sequenceDiagram
    participant OpenEVSE as OpenEVSE
    participant Session as aiohttp.ClientSession
    participant GitHub as GitHub API

    OpenEVSE->>Session: _firmware_check_with_session(url, method)
    Session->>GitHub: HTTP GET /releases/latest
    GitHub-->>Session: HTTP 200 + JSON payload
    Session-->>OpenEVSE: parsed JSON (dict) or error
    alt parsed dict
        OpenEVSE->>OpenEVSE: build {latest_version, release_notes, release_url}
    else JSON decode / type error / connector error
        OpenEVSE->>OpenEVSE: return None or {}
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • firstof9/python-openevse-http#493 — Modifies firmware_check response construction (release notes/summary) in the same file.
  • firstof9/python-openevse-http#524 — Refactors session-based HTTP handling in openevsehttp/__main__.py similarly by extracting per-session request logic into a helper.

Suggested labels

code quality

Poem

A rabbit nibbles at tangled threads of code,
Pulls out a helper, makes the pathways flow,
Fewer copies, fewer sighs — what a treat!
Tests hum softly, errors handled neat,
Hoppity-hop, the firmware check runs slow no more. 🐇✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main refactoring change: extracting firmware check logic into a dedicated helper method (_firmware_check_with_session) to reduce code duplication, which is the primary focus of the changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch suggested-fixes

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@firstof9 firstof9 marked this pull request as ready for review March 4, 2026 15:23
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@openevsehttp/__main__.py`:
- Around line 624-629: Wrap the JSON parsing and key access in a defensive
try/except: when calling json.loads(message) (variable message) catch
json.JSONDecodeError and handle by logging and returning an empty/neutral
response; likewise either use message.get("tag_name"), message.get("body"),
message.get("html_url") or catch KeyError around the dict accesses that populate
response["latest_version"], response["release_notes"], response["release_url"]
so unexpected response shapes don't raise—make the helper return a safe response
(e.g., empty dict or keys set to None) that the existing firmware_check
try/except and the pattern used in _process_request_with_session can handle.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 885d9bd7-d68a-4e4a-bc78-c5b94f4c07bc

📥 Commits

Reviewing files that changed from the base of the PR and between f7cc8db and 0dc931f.

📒 Files selected for processing (1)
  • openevsehttp/__main__.py

@firstof9
Copy link
Owner Author

firstof9 commented Mar 4, 2026

@coderabbitai review

@coderabbitai
Copy link

coderabbitai bot commented Mar 4, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
openevsehttp/__main__.py (1)

625-629: Preserve traceback for JSON parse failures.

At Line 628, using _LOGGER.exception(...) instead of _LOGGER.error(...) keeps stack context for easier debugging.

Proposed tweak
             try:
                 message = json.loads(message)
             except json.JSONDecodeError:
-                _LOGGER.error("Failed to parse JSON response: %s", message)
+                _LOGGER.exception("Failed to parse JSON response: %s", message)
                 return None
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openevsehttp/__main__.py` around lines 625 - 629, The except block catching
json.JSONDecodeError around the json.loads(...) call should log the failure with
the traceback; replace the call to _LOGGER.error(...) with
_LOGGER.exception(...) so the exception stack context is preserved while still
including the original message content, i.e., update the JSON decoding error
handling in the try/except that surrounds json.loads(message) to call
_LOGGER.exception("Failed to parse JSON response: %s", message) before returning
None.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@openevsehttp/__main__.py`:
- Around line 625-629: The except block catching json.JSONDecodeError around the
json.loads(...) call should log the failure with the traceback; replace the call
to _LOGGER.error(...) with _LOGGER.exception(...) so the exception stack context
is preserved while still including the original message content, i.e., update
the JSON decoding error handling in the try/except that surrounds
json.loads(message) to call _LOGGER.exception("Failed to parse JSON response:
%s", message) before returning None.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f580b12b-645e-48b5-b1c6-6b68681af49b

📥 Commits

Reviewing files that changed from the base of the PR and between 0dc931f and 70758d4.

📒 Files selected for processing (2)
  • openevsehttp/__main__.py
  • tests/test_main.py

@firstof9 firstof9 merged commit 03bb355 into main Mar 4, 2026
6 checks passed
@firstof9 firstof9 deleted the suggested-fixes branch March 4, 2026 16:22
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