-
-
Notifications
You must be signed in to change notification settings - Fork 37.5k
Add device cleanup to Vodafone Station #116024
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
chemelli74
merged 31 commits into
home-assistant:dev
from
chemelli74:chemelli74-vodafone-cleanup
Feb 3, 2025
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
b707158
add device cleanup
chemelli74 b7043cd
apply review comments
chemelli74 b0ee580
fix description
chemelli74 1f034ea
make cleanup automatic
chemelli74 c25ab95
.
chemelli74 9e6af46
rework approach based on IQS021 rule
chemelli74 1222a2d
add initial devices list from registry
chemelli74 472c787
use connections instead of identifiers
chemelli74 54394f6
apply review comment
chemelli74 dfe17bb
add some coordinator tests
chemelli74 82812bf
one more test
chemelli74 c057603
cleanup tests
chemelli74 d251388
allign tests
chemelli74 35d98bf
apply review comment
chemelli74 fbb5737
removed sensor test
chemelli74 f74a1e5
cleanup test
chemelli74 4f05265
Merge branch 'dev' into chemelli74-vodafone-cleanup
chemelli74 cbc8279
Merge branch 'dev' into chemelli74-vodafone-cleanup
chemelli74 36ac603
align test to latest code
chemelli74 976dff1
typo
chemelli74 e188e1e
fix after rebase
chemelli74 d7465b7
introduce generic helper
chemelli74 af4e854
apply some review comments
chemelli74 7c5c419
add comments to clarify design
chemelli74 c8b755d
apply latest review comment
chemelli74 bb6f225
ruff
chemelli74 8d356d5
improved coverage
chemelli74 73c94e8
more coverage
chemelli74 b0c8b53
100% helpers.py test coverage
chemelli74 3e02b8c
improve test
chemelli74 b91d7ba
Merge branch 'dev' into chemelli74-vodafone-cleanup
bdraco 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,72 @@ | ||
| """Vodafone Station helpers.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import device_registry as dr, entity_registry as er | ||
|
|
||
| from .const import _LOGGER | ||
|
|
||
|
|
||
| async def cleanup_device_tracker( | ||
| hass: HomeAssistant, config_entry: ConfigEntry, devices: dict[str, Any] | ||
| ) -> None: | ||
| """Cleanup stale device tracker.""" | ||
| entity_reg: er.EntityRegistry = er.async_get(hass) | ||
|
|
||
| entities_removed: bool = False | ||
|
|
||
| device_hosts_macs: set[str] = set() | ||
| device_hosts_names: set[str] = set() | ||
| for mac, device_info in devices.items(): | ||
| device_hosts_macs.add(mac) | ||
| device_hosts_names.add(device_info.device.name) | ||
|
|
||
| for entry in er.async_entries_for_config_entry(entity_reg, config_entry.entry_id): | ||
| if entry.domain != DEVICE_TRACKER_DOMAIN: | ||
| continue | ||
| entry_name = entry.name or entry.original_name | ||
| entry_host = entry_name.partition(" ")[0] if entry_name else None | ||
| entry_mac = entry.unique_id.partition("_")[0] | ||
|
|
||
| # Some devices, mainly routers, allow to change the hostname of the connected devices. | ||
| # This can lead to entities no longer aligned to the device UI | ||
| if ( | ||
| entry_host | ||
| and entry_host in device_hosts_names | ||
| and entry_mac in device_hosts_macs | ||
| ): | ||
| _LOGGER.debug( | ||
|
bdraco marked this conversation as resolved.
|
||
| "Skipping entity %s [mac=%s, host=%s]", | ||
| entry_name, | ||
| entry_mac, | ||
| entry_host, | ||
| ) | ||
| continue | ||
| # Entity is removed so that at the next coordinator update | ||
| # the correct one will be created | ||
| _LOGGER.info("Removing entity: %s", entry_name) | ||
| entity_reg.async_remove(entry.entity_id) | ||
|
chemelli74 marked this conversation as resolved.
|
||
| entities_removed = True | ||
|
|
||
| if entities_removed: | ||
| _async_remove_empty_devices(hass, entity_reg, config_entry) | ||
|
|
||
|
|
||
| def _async_remove_empty_devices( | ||
| hass: HomeAssistant, entity_reg: er.EntityRegistry, config_entry: ConfigEntry | ||
| ) -> None: | ||
| """Remove devices with no entities.""" | ||
|
bdraco marked this conversation as resolved.
|
||
|
|
||
| device_reg = dr.async_get(hass) | ||
| device_list = dr.async_entries_for_config_entry(device_reg, config_entry.entry_id) | ||
| for device_entry in device_list: | ||
| if not er.async_entries_for_device( | ||
| entity_reg, | ||
| device_entry.id, | ||
| include_disabled_entities=True, | ||
| ): | ||
| _LOGGER.info("Removing device: %s", device_entry.name) | ||
| device_reg.async_remove_device(device_entry.id) | ||
|
chemelli74 marked this conversation as resolved.
|
||
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 |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| """Common stuff for Vodafone Station tests.""" | ||
|
|
||
| DEVICE_1_HOST = "WifiDevice0" | ||
| DEVICE_1_MAC = "xx:xx:xx:xx:xx:xx" | ||
| DEVICE_2_HOST = "LanDevice1" | ||
| DEVICE_2_MAC = "yy:yy:yy:yy:yy:yy" |
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,68 @@ | ||
| """Define tests for the Vodafone Station coordinator.""" | ||
|
|
||
| import logging | ||
| from unittest.mock import AsyncMock | ||
|
|
||
| from aiovodafone import VodafoneStationDevice | ||
| from freezegun.api import FrozenDateTimeFactory | ||
| import pytest | ||
|
|
||
| from homeassistant.components.vodafone_station.const import DOMAIN, SCAN_INTERVAL | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import device_registry as dr | ||
|
|
||
| from . import setup_integration | ||
| from .const import DEVICE_1_HOST, DEVICE_1_MAC, DEVICE_2_HOST, DEVICE_2_MAC | ||
|
|
||
| from tests.common import MockConfigEntry, async_fire_time_changed | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("entity_registry_enabled_by_default") | ||
| async def test_coordinator_device_cleanup( | ||
| hass: HomeAssistant, | ||
| freezer: FrozenDateTimeFactory, | ||
| mock_vodafone_station_router: AsyncMock, | ||
| mock_config_entry: MockConfigEntry, | ||
| caplog: pytest.LogCaptureFixture, | ||
| device_registry: dr.DeviceRegistry, | ||
| ) -> None: | ||
| """Test Device cleanup on coordinator update.""" | ||
|
|
||
| caplog.set_level(logging.DEBUG) | ||
| await setup_integration(hass, mock_config_entry) | ||
|
|
||
| device = device_registry.async_get_or_create( | ||
| config_entry_id=mock_config_entry.entry_id, | ||
| identifiers={(DOMAIN, DEVICE_1_MAC)}, | ||
| name=DEVICE_1_HOST, | ||
| ) | ||
| assert device is not None | ||
|
|
||
| device_tracker = f"device_tracker.{DEVICE_1_HOST}" | ||
|
|
||
| state = hass.states.get(device_tracker) | ||
| assert state is not None | ||
|
|
||
| mock_vodafone_station_router.get_devices_data.return_value = { | ||
| DEVICE_2_MAC: VodafoneStationDevice( | ||
| connected=True, | ||
| connection_type="lan", | ||
| ip_address="192.168.1.11", | ||
| name=DEVICE_2_HOST, | ||
| mac=DEVICE_2_MAC, | ||
| type="desktop", | ||
| wifi="", | ||
| ), | ||
| } | ||
|
|
||
| freezer.tick(SCAN_INTERVAL) | ||
| async_fire_time_changed(hass) | ||
| await hass.async_block_till_done(wait_background_tasks=True) | ||
|
|
||
| state = hass.states.get(device_tracker) | ||
| assert state is None | ||
| assert f"Skipping entity {DEVICE_2_HOST}" in caplog.text | ||
|
|
||
| device = device_registry.async_get_device(identifiers={(DOMAIN, DEVICE_1_MAC)}) | ||
| assert device is None | ||
| assert f"Removing device: {DEVICE_1_HOST}" in caplog.text | ||
|
chemelli74 marked this conversation as resolved.
|
||
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.