Skip to content

Fix crash when charge/discharge time string is malformed#3930

Merged
springfall2008 merged 4 commits into
mainfrom
copilot/fix-inverter-crash-invalid-time
May 16, 2026
Merged

Fix crash when charge/discharge time string is malformed#3930
springfall2008 merged 4 commits into
mainfrom
copilot/fix-inverter-crash-invalid-time

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 16, 2026

time_string_to_stamp() raised an unhandled ValueError when the inverter returned an out-of-range time like '14:70:00', crashing update_status() entirely instead of recovering gracefully.

Changes

  • utils.pytime_string_to_stamp(): wrap datetime.strptime in try/except ValueError; return None on failure with a warning print. The existing None-guards in inverter.py already set safe defaults (00:00:00) and log a warning, so no further changes are needed there.
# Before
return datetime.strptime(time_string, "%H:%M:%S")  # raises ValueError on e.g. '14:70:00'

# After
try:
    return datetime.strptime(time_string, "%H:%M:%S")
except ValueError:
    print("WARN: time_string_to_stamp: invalid time string '{}', returning None".format(time_string))
    return None
  • fetch.py: removed the now-redundant try/except (ValueError, TypeError) blocks around time_string_to_stamp() calls in the energy rates loader, replacing them with None checks. Since time_string_to_stamp() now handles ValueError internally and returns None, the old exception handlers would never have triggered. The error logging and continue behaviour are preserved exactly.

  • tests/test_inverter.py: add test_charge_window_invalid_format_time and test_discharge_window_invalid_format_time covering the out-of-range minutes case ('14:70:00'), complementing the existing 'unknown' tests.

- Wrap datetime.strptime in time_string_to_stamp (utils.py) with try/except ValueError so invalid time strings like '14:70:00' return None instead of crashing
- Add test_charge_window_invalid_format_time and test_discharge_window_invalid_format_time tests to cover this case in test_inverter.py

Agent-Logs-Url: https://github.com/springfall2008/batpred/sessions/a86d98eb-f679-4432-9da4-686d12309bdf

Co-authored-by: springfall2008 <48591903+springfall2008@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix inverter.py crash on invalid discharge time Fix crash in inverter.py when charge/discharge time string is malformed May 16, 2026
Copilot AI requested a review from springfall2008 May 16, 2026 13:56
@springfall2008
Copy link
Copy Markdown
Owner

@copilot what about the other call sits of time_string_to_stamp in fetch.py, don't we need to remove the try/except around those and check for None instead?

Now that time_string_to_stamp() handles ValueError internally and returns
None, the try/except (ValueError, TypeError) blocks in fetch.py are
redundant. Replace them with None checks to keep the same error logging
and rate-skip behaviour.

Agent-Logs-Url: https://github.com/springfall2008/batpred/sessions/eb3f6ef7-2369-4d94-a670-180c93ae4bcb

Co-authored-by: springfall2008 <48591903+springfall2008@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 16, 2026

@copilot what about the other call sits of time_string_to_stamp in fetch.py, don't we need to remove the try/except around those and check for None instead?

Done in b4661df. The two try/except (ValueError, TypeError) blocks around time_string_to_stamp() calls in fetch.py have been replaced with None checks — since the function now handles ValueError internally and returns None, those exception handlers would never have fired. The log message and continue behaviour are preserved exactly.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/springfall2008/batpred/contents/apps/predbat
    • Triggering command: /home/REDACTED/work/batpred/batpred/coverage/venv/bin/python3 python3 ../apps/predbat/unit_test.py --test inverter --test basic_rates (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Copilot AI changed the title Fix crash in inverter.py when charge/discharge time string is malformed Fix crash when charge/discharge time string is malformed May 16, 2026
@springfall2008 springfall2008 marked this pull request as ready for review May 16, 2026 14:06
Copilot AI review requested due to automatic review settings May 16, 2026 14:06
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR hardens Predbat’s time parsing so malformed inverter-provided schedule strings (e.g. 14:70:00) don’t crash inverter status updates, and it aligns rate-loading code to treat bad times as “missing” rather than exceptional.

Changes:

  • Update time_string_to_stamp() to catch malformed time strings and return None.
  • Simplify fetch.basic_rates() by removing redundant exception handling and relying on None checks.
  • Add inverter tests covering out-of-range minute values for charge/discharge schedule times.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
apps/predbat/utils.py Wraps datetime.strptime() to avoid crashing on invalid time strings (but currently only catches ValueError).
apps/predbat/fetch.py Replaces try/except around time_string_to_stamp() with None checks when parsing rate windows.
apps/predbat/tests/test_inverter.py Adds regression tests for invalid time values (14:70:00) in charge/discharge windows and wires them into the inverter test runner.
Comments suppressed due to low confidence (1)

apps/predbat/utils.py:784

  • The warning uses print(), which bypasses Predbat's normal logging (self.log() / record_status) and can become noisy because many callers already log a warning when time_string_to_stamp() returns None (so this can double-log). Prefer returning None without printing, or route the warning through the project's logging mechanism if available.
        return datetime.strptime(time_string, "%H:%M:%S")
    except ValueError:
        print("WARN: time_string_to_stamp: invalid time string '{}', returning None".format(time_string))
        return None

Comment thread apps/predbat/utils.py Outdated
Comment thread apps/predbat/fetch.py
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@springfall2008 springfall2008 merged commit 0c0d269 into main May 16, 2026
1 check passed
@springfall2008 springfall2008 deleted the copilot/fix-inverter-crash-invalid-time branch May 16, 2026 14:15
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.

Bug: If discharge time is invalid inverter.py crashes

3 participants