Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove auth token from device when removing HomeWizard entry #138596

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
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
16 changes: 16 additions & 0 deletions homeassistant/components/homewizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""The Homewizard integration."""

from contextlib import suppress

from homewizard_energy import (
HomeWizardEnergy,
HomeWizardEnergyV1,
Expand Down Expand Up @@ -74,6 +76,20 @@ async def async_unload_entry(hass: HomeAssistant, entry: HomeWizardConfigEntry)
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


async def async_remove_entry(hass: HomeAssistant, entry: HomeWizardConfigEntry) -> None:
"""Try removing a token."""
if token := entry.data.get(CONF_TOKEN):
api = HomeWizardEnergyV2(
entry.data[CONF_IP_ADDRESS],
token=token,
clientsession=async_get_clientsession(hass),
)

# Ignore any error, because device may be unreachable
with suppress(Exception):
Copy link
Member

Choose a reason for hiding this comment

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

does it make sense here to treat all exceptions the same (if the answer is "yes" also fine). I was just thinking, if the device is reachable but we still cannot remove it if we should take action

Copy link
Member

Choose a reason for hiding this comment

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

Isn't this described in the PR description?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but I wasn’t sure if we shouldn’t consider some/retry if makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMO this is just some cleanup which is not really required. It is fine if it fails, because the user can factory reset the device to clear all tokens if they get into issues.

I think this is just a bit better than doing nothing, any other solution with error handling will just cause issues and confusion.

Copy link
Member

Choose a reason for hiding this comment

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

fine by me, but I'd prefer catching and logging a warning then, to give the user a chance to notice something went wrong and is left over in the device (together with "you can factory reset to get rid of it")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be nice indeed 👍

await api.delete_token()


async def async_check_v2_support_and_create_issue(
hass: HomeAssistant, entry: HomeWizardConfigEntry
) -> None:
Expand Down
3 changes: 3 additions & 0 deletions tests/components/homewizard/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ def mock_homewizardenergy_v2(
load_json_object_fixture("v2/generic/token.json", DOMAIN)
).token

# Authorization flow is used during configuration flow
client.delete_token.return_value = None

yield client


Expand Down
31 changes: 30 additions & 1 deletion tests/components/homewizard/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import weakref

from freezegun.api import FrozenDateTimeFactory
from homewizard_energy.errors import DisabledError, UnauthorizedError
from homewizard_energy.errors import DisabledError, RequestError, UnauthorizedError
import pytest

from homeassistant.components.homewizard.const import DOMAIN
Expand Down Expand Up @@ -210,3 +210,32 @@ async def test_disablederror_reloads_integration(
flow = flows[0]
assert flow.get("step_id") == "reauth_enable_api"
assert flow.get("handler") == DOMAIN


async def test_remove_entry_device_reachable(
hass: HomeAssistant,
mock_config_entry_v2: MockConfigEntry,
mock_homewizardenergy_v2: MagicMock,
) -> None:
"""Test removing entry when device is reachable."""
mock_config_entry_v2.add_to_hass(hass)
await hass.config_entries.async_remove(mock_config_entry_v2.entry_id)
await hass.async_block_till_done()

assert mock_homewizardenergy_v2.delete_token.called


@pytest.mark.parametrize("exception", [RequestError, TimeoutError, UnauthorizedError])
async def test_remove_entry_device_not_reachable(
hass: HomeAssistant,
mock_config_entry_v2: MockConfigEntry,
mock_homewizardenergy_v2: MagicMock,
exception: Exception,
) -> None:
"""Test removing entry when device is not reachable."""
mock_homewizardenergy_v2.delete_token.side_effect = exception
mock_config_entry_v2.add_to_hass(hass)
await hass.config_entries.async_remove(mock_config_entry_v2.entry_id)
await hass.async_block_till_done()

assert mock_homewizardenergy_v2.delete_token.called