Skip to content

Refactor button platform to use indevolt-api 1.4.2#169063

Merged
abmantis merged 3 commits into
home-assistant:devfrom
Xirt:refactor-indevolt-button-realtime
Apr 24, 2026
Merged

Refactor button platform to use indevolt-api 1.4.2#169063
abmantis merged 3 commits into
home-assistant:devfrom
Xirt:refactor-indevolt-button-realtime

Conversation

@Xirt
Copy link
Copy Markdown
Contributor

@Xirt Xirt commented Apr 24, 2026

Proposed change

Updated the existing integration to utilize the updated Indevolt API library. This prevent having changes to the buttons platform when the PR for actions (#163578) is merged. It is part of a bigger refactoring to move global magic constants into the API library. Changes:

  • Leveraged new function to stop() the battery
  • Altered exception handling to align on the new approach
  • Adjusted test cases accordingly

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:
  • Link to developer documentation pull request:
  • Link to frontend pull request:

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

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

Refactors the Indevolt button implementation to use the updated indevolt-api (v1.4.2) real-time action methods, aligning button behavior and tests with the new API surface.

Changes:

  • Switch button press handling from writing the real-time action key to calling the API’s stop() real-time action.
  • Replace coordinator real-time action execution with charge() / discharge() / stop() calls.
  • Update Indevolt button tests and test fixtures to match the new API behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
homeassistant/components/indevolt/coordinator.py Replaces old real-time action push logic with IndevoltRealtimeAction and API action methods.
homeassistant/components/indevolt/button.py Updates button press to invoke coordinator real-time STOP action.
homeassistant/components/indevolt/const.py Removes the no-longer-used real-time action write key constant.
tests/components/indevolt/test_button.py Updates tests to assert mode switch + stop() behavior instead of writing action data.
tests/components/indevolt/conftest.py Extends the IndevoltAPI mock with stop/charge/discharge return values.
Comments suppressed due to low confidence (2)

tests/components/indevolt/test_button.py:122

  • Add/restore a test case covering stop() raising a timeout/connection exception (previously covered via set_data TimeOutException) so the new real-time action path’s error mapping stays verified.
    # Simulate stop() returning False (e.g. device rejected the command)
    mock_indevolt.stop.return_value = False

    # Mock call to pause (dis)charging
    with pytest.raises(HomeAssistantError):
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {ATTR_ENTITY_ID: ENTITY_ID_GEN2},
            blocking=True,
        )

homeassistant/components/indevolt/coordinator.py:169

  • Wrap the Indevolt API real-time action calls in exception handling (or reuse async_push_data) so TimeOutException/connection errors are translated into a HomeAssistantError with the intended translation key instead of bubbling raw exceptions.
        await self.async_switch_energy_mode(REALTIME_ACTION_MODE, refresh=False)

        match action_code:
            case IndevoltRealtimeAction.CHARGE:
                success = await self.api.charge(power, target_soc)
            case IndevoltRealtimeAction.DISCHARGE:
                success = await self.api.discharge(power, target_soc)
            case _:
                success = await self.api.stop()

        if not success:
            raise HomeAssistantError(
                translation_domain=DOMAIN,
                translation_key="failed_to_execute_realtime_action",
            )

Comment thread tests/components/indevolt/test_button.py Outdated
@Xirt Xirt marked this pull request as ready for review April 24, 2026 14:27
Copilot AI review requested due to automatic review settings April 24, 2026 14:27
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

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

Comment thread homeassistant/components/indevolt/coordinator.py
Comment thread homeassistant/components/indevolt/const.py Outdated
@Xirt Xirt mentioned this pull request Apr 24, 2026
21 tasks
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 24, 2026 14:34
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

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

Comments suppressed due to low confidence (1)

tests/components/indevolt/test_button.py:121

  • Add coverage for stop() raising TimeOutException/connection errors (in addition to returning False) once coordinator wraps these into HomeAssistantError, since the previous timeout test was removed and this is user-facing error behavior.
@pytest.mark.parametrize("generation", [2], indirect=True)
async def test_button_press_standby_rejected_command(
    hass: HomeAssistant,
    mock_indevolt: AsyncMock,
    mock_config_entry: MockConfigEntry,
) -> None:
    """Test pressing standby raises HomeAssistantError when the device rejects the command."""
    with patch("homeassistant.components.indevolt.PLATFORMS", [Platform.BUTTON]):
        await setup_integration(hass, mock_config_entry)

    # Simulate stop() returning False (device rejected the command)
    mock_indevolt.stop.return_value = False

    # Mock call to pause (dis)charging
    with pytest.raises(HomeAssistantError):
        await hass.services.async_call(
            BUTTON_DOMAIN,
            SERVICE_PRESS,
            {ATTR_ENTITY_ID: ENTITY_ID_GEN2},
            blocking=True,
        )

) from err
match action_code:
case IndevoltRealtimeAction.STOP:
success = await self.api.stop()
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

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

Ensure async_realtime_action always defines success by handling all IndevoltRealtimeAction values (e.g., CHARGE/DISCHARGE) or adding a default case that raises a clear error; otherwise non-STOP actions will crash with an unbound local error.

Suggested change
success = await self.api.stop()
success = await self.api.stop()
case _:
raise HomeAssistantError(
f"Unsupported real-time action: {action_code}"
)

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is currently only one caller of this function which only uses IndevoltRealtimeAction.STOP - there are thus no uncovered edge cases. Other cases will be added in subsequent PRs in which we can be exhaustive.

Comment thread homeassistant/components/indevolt/coordinator.py
Copy link
Copy Markdown
Member

@abmantis abmantis left a comment

Choose a reason for hiding this comment

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

The new api is looking nice! Thanks for the effort!

@abmantis abmantis merged commit ca70abe into home-assistant:dev Apr 24, 2026
36 of 37 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants