Conversation
There was a problem hiding this comment.
Pull request overview
Fixes a runtime TypeError in the default battery control logic by ensuring the logic factory passes an integer time resolution (minutes) into DefaultLogic, avoiding string/int arithmetic issues when configuration values come in as strings (e.g., via Home Assistant).
Changes:
- Update logic factory API to accept an explicit
time_resolution_minutesargument and pass it toDefaultLogic. - Update core loop to call the logic factory with
self.time_resolutioninstead of readingtime_resolution_minutesdirectly inside the factory.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/batcontrol/logic/logic.py |
Adjusts create_logic to take time_resolution_minutes explicitly and use it for DefaultLogic(interval_minutes=...). |
src/batcontrol/core.py |
Passes the validated/normalized self.time_resolution into the logic factory during each run. |
src/batcontrol/logic/logic.py
Outdated
| print_class_message = True | ||
| @staticmethod | ||
| def create_logic(config: dict, timezone) -> LogicInterface: | ||
| def create_logic(time_resolution_minutes: int,config: dict, timezone) -> LogicInterface: |
| this_logic_run = LogicFactory.create_logic(self.time_resolution, | ||
| self.config, | ||
| self.timezone) |
|
@copilot open a new pull request to apply changes based on the comments in this thread |
…_minutes (#306) * Initial plan * Fix PEP8 spacing in logic.py and add string time_resolution tests Co-authored-by: MaStr <1036501+MaStr@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: MaStr <1036501+MaStr@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Fixes a runtime TypeError caused by interval_minutes being treated as a string in the control logic, by ensuring the logic factory receives the already-coerced integer time resolution from Batcontrol.
Changes:
- Update logic factory API to take
time_resolution_minutesexplicitly and pass it through toDefaultLogic. - Update
Batcontrol.run()to call the logic factory withself.time_resolution(int). - Add tests covering string
time_resolution_minuteshandling and basic logic factory behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/batcontrol/logic/logic.py |
Changes Logic.create_logic signature and uses the passed resolution for DefaultLogic(interval_minutes=...). |
src/batcontrol/core.py |
Passes self.time_resolution into LogicFactory.create_logic(...) during run(). |
tests/batcontrol/test_core.py |
Adds regression tests for string time_resolution_minutes coercion and logic factory instantiation. |
| def create_logic(time_resolution_minutes: int, config: dict, timezone) -> LogicInterface: | ||
| """ Select and configure a logic class based on the given configuration """ | ||
| request_type = config.get('type', 'default').lower() | ||
| interval_minutes = config.get('time_resolution_minutes', 60) | ||
| logic = None | ||
| if request_type == 'default': | ||
| if Logic.print_class_message: | ||
| logger.info('Using default logic') | ||
| Logic.print_class_message = False | ||
| logic = DefaultLogic(timezone, interval_minutes=interval_minutes) | ||
| logic = DefaultLogic(timezone, interval_minutes=time_resolution_minutes) | ||
| if config.get('battery_control_expert', None) is not None: |
| this_logic_run = LogicFactory.create_logic(self.time_resolution, | ||
| self.config, | ||
| self.timezone) |
| @pytest.mark.parametrize('resolution_str', ['60', '15']) | ||
| def test_logic_factory_accepts_string_resolution_as_int(self, resolution_str): | ||
| """Logic factory must produce a valid logic instance when given an int resolution""" | ||
| logic = LogicFactory.create_logic( | ||
| int(resolution_str), | ||
| {'type': 'default'}, | ||
| datetime.timezone.utc | ||
| ) | ||
| assert logic is not None | ||
| assert logic.interval_minutes == int(resolution_str) |
Closed #302