Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/batcontrol/mqtt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,13 +731,15 @@ def send_mqtt_discovery_for_mode(self) -> None:
val_templ = (
"{% if value == '-1' %}Charge from Grid"
"{% elif value == '0' %}Avoid Discharge"
"{% elif value == '8' %}Limit Battery Charge"
"{% elif value == '10' %}Discharge Allowed"
"{% else %}Unknown"
"{% endif %}"
)
cmd_templ = (
"{% if value == 'Charge from Grid' %}-1"
"{% elif value == 'Avoid Discharge' %}0"
"{% elif value == 'Limit Battery Charge' %}8"
"{% elif value == 'Discharge Allowed' %}10"
"{% else %}-1"
"{% endif %}"
Expand All @@ -754,6 +756,7 @@ def send_mqtt_discovery_for_mode(self) -> None:
options=[
"Charge from Grid",
"Avoid Discharge",
"Limit Battery Charge",
"Discharge Allowed"],
value_template=val_templ,
command_template=cmd_templ)
Comment on lines 756 to 762
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

send_mqtt_discovery_for_mode passes a list for options, but publish_mqtt_discovery_message currently annotates options as str (and treats it generically). To avoid confusion and improve static analysis, update the options parameter type to a sequence/list of strings (or None) and keep the formatting consistent (e.g., trailing comma / closing bracket on its own line).

Copilot uses AI. Check for mistakes.
Expand Down
27 changes: 27 additions & 0 deletions tests/batcontrol/test_mqtt_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,33 @@ def test_str_convert_with_plain_string(self):
assert received == ['true']


class TestModeDiscovery:
"""Mode discovery should expose the full externally supported mode model."""

Comment on lines +92 to +94
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

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

The new discovery-related tests make this module-level scope broader than the current file docstring (which says it focuses on _handle_message bytes decoding). Consider updating the top-level docstring so it reflects both the message-handling and discovery regression coverage.

Copilot uses AI. Check for mistakes.
def test_mode_discovery_includes_limit_battery_charge_mode(self):
api = MagicMock(spec=MqttApi)
api.base_topic = 'batcontrol'
api.publish_mqtt_discovery_message = MagicMock()
api.send_mqtt_discovery_for_mode = (
MqttApi.send_mqtt_discovery_for_mode.__get__(api, MqttApi)
)

api.send_mqtt_discovery_for_mode()

options = api.publish_mqtt_discovery_message.call_args.kwargs['options']
value_template = api.publish_mqtt_discovery_message.call_args.kwargs['value_template']
command_template = api.publish_mqtt_discovery_message.call_args.kwargs['command_template']

assert options == [
'Charge from Grid',
'Avoid Discharge',
'Limit Battery Charge',
'Discharge Allowed',
]
assert "{% elif value == '8' %}Limit Battery Charge" in value_template
assert "{% elif value == 'Limit Battery Charge' %}8" in command_template


class TestPeakShavingEnabledApi:
"""Regression test: peak_shaving/enabled must correctly parse the bytes payload."""

Expand Down