-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Automatic cleanup of entity and device registry in AVM FRITZ!SmartHome #114601
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
Merged
mib1185
merged 8 commits into
home-assistant:dev
from
mib1185:fritzbox/automatic-cleanup-of-entity-and-device-registry
Apr 20, 2024
+165
−75
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
88a49cb
cleanup obsolet entity and device registry entries
mib1185 1c6e2be
move coordinator tests into own file
mib1185 45efbc0
move first coordinator fetch after unique_id migrations
mib1185 d75d58f
adjust existing tests
mib1185 9b3cdf8
add auto cleanup test
mib1185 47dd1ab
Merge branch 'dev' into fritzbox/automatic-cleanup-of-entity-and-devi…
mib1185 b4decb1
move first orphan check into async setup method
mib1185 efa950a
pre calculate available identifiers
mib1185 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| """Tests for the AVM Fritz!Box integration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from datetime import timedelta | ||
| from unittest.mock import Mock | ||
|
|
||
| from pyfritzhome import LoginError | ||
| from requests.exceptions import ConnectionError, HTTPError | ||
|
|
||
| from homeassistant.components.fritzbox.const import DOMAIN as FB_DOMAIN | ||
| from homeassistant.config_entries import ConfigEntryState | ||
| from homeassistant.const import CONF_DEVICES | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import device_registry as dr, entity_registry as er | ||
| from homeassistant.util.dt import utcnow | ||
|
|
||
| from . import FritzDeviceCoverMock, FritzDeviceSwitchMock | ||
| from .const import MOCK_CONFIG | ||
|
|
||
| from tests.common import MockConfigEntry, async_fire_time_changed | ||
|
|
||
|
|
||
| async def test_coordinator_update_after_reboot( | ||
| hass: HomeAssistant, fritz: Mock | ||
| ) -> None: | ||
| """Test coordinator after reboot.""" | ||
| entry = MockConfigEntry( | ||
| domain=FB_DOMAIN, | ||
| data=MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], | ||
| unique_id="any", | ||
| ) | ||
| entry.add_to_hass(hass) | ||
| fritz().update_devices.side_effect = [HTTPError(), ""] | ||
|
|
||
| assert await hass.config_entries.async_setup(entry.entry_id) | ||
| assert fritz().update_devices.call_count == 2 | ||
| assert fritz().update_templates.call_count == 1 | ||
| assert fritz().get_devices.call_count == 1 | ||
| assert fritz().get_templates.call_count == 1 | ||
| assert fritz().login.call_count == 2 | ||
|
|
||
|
|
||
| async def test_coordinator_update_after_password_change( | ||
| hass: HomeAssistant, fritz: Mock | ||
| ) -> None: | ||
| """Test coordinator after password change.""" | ||
| entry = MockConfigEntry( | ||
| domain=FB_DOMAIN, | ||
| data=MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], | ||
| unique_id="any", | ||
| ) | ||
| entry.add_to_hass(hass) | ||
| fritz().update_devices.side_effect = HTTPError() | ||
| fritz().login.side_effect = ["", LoginError("some_user")] | ||
|
|
||
| assert not await hass.config_entries.async_setup(entry.entry_id) | ||
| assert fritz().update_devices.call_count == 1 | ||
| assert fritz().get_devices.call_count == 0 | ||
| assert fritz().get_templates.call_count == 0 | ||
| assert fritz().login.call_count == 2 | ||
|
|
||
|
|
||
| async def test_coordinator_update_when_unreachable( | ||
| hass: HomeAssistant, fritz: Mock | ||
| ) -> None: | ||
| """Test coordinator after reboot.""" | ||
| entry = MockConfigEntry( | ||
| domain=FB_DOMAIN, | ||
| data=MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], | ||
| unique_id="any", | ||
| ) | ||
| entry.add_to_hass(hass) | ||
| fritz().update_devices.side_effect = [ConnectionError(), ""] | ||
|
|
||
| assert not await hass.config_entries.async_setup(entry.entry_id) | ||
| assert entry.state is ConfigEntryState.SETUP_RETRY | ||
|
|
||
|
|
||
| async def test_coordinator_automatic_registry_cleanup( | ||
| hass: HomeAssistant, | ||
| fritz: Mock, | ||
| device_registry: dr.DeviceRegistry, | ||
| entity_registry: er.EntityRegistry, | ||
| ) -> None: | ||
| """Test automatic registry cleanup.""" | ||
| fritz().get_devices.return_value = [ | ||
| FritzDeviceSwitchMock(ain="fake ain switch", name="fake_switch"), | ||
| FritzDeviceCoverMock(ain="fake ain cover", name="fake_cover"), | ||
| ] | ||
| entry = MockConfigEntry( | ||
| domain=FB_DOMAIN, | ||
| data=MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], | ||
| unique_id="any", | ||
| ) | ||
| entry.add_to_hass(hass) | ||
| await hass.config_entries.async_setup(entry.entry_id) | ||
| await hass.async_block_till_done(wait_background_tasks=True) | ||
|
|
||
| assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 11 | ||
| assert len(dr.async_entries_for_config_entry(device_registry, entry.entry_id)) == 2 | ||
|
|
||
| fritz().get_devices.return_value = [ | ||
| FritzDeviceSwitchMock(ain="fake ain switch", name="fake_switch") | ||
| ] | ||
|
|
||
| async_fire_time_changed(hass, utcnow() + timedelta(seconds=35)) | ||
| await hass.async_block_till_done(wait_background_tasks=True) | ||
|
|
||
| assert len(er.async_entries_for_config_entry(entity_registry, entry.entry_id)) == 8 | ||
| assert len(dr.async_entries_for_config_entry(device_registry, entry.entry_id)) == 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.