Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 10 additions & 9 deletions homeassistant/components/youtube/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

from .api import AsyncConfigEntryAuth
from .const import CONF_CHANNELS
from .coordinator import YouTubeConfigEntry, YouTubeDataUpdateCoordinator

PLATFORMS = [Platform.SENSOR]
Expand All @@ -39,7 +40,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: YouTubeConfigEntry) -> b

await coordinator.async_config_entry_first_refresh()

await delete_devices(hass, entry, coordinator)
await delete_devices(hass, entry)

entry.runtime_data = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand All @@ -52,15 +53,15 @@ async def async_unload_entry(hass: HomeAssistant, entry: YouTubeConfigEntry) ->
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)


async def delete_devices(
hass: HomeAssistant,
entry: YouTubeConfigEntry,
coordinator: YouTubeDataUpdateCoordinator,
) -> None:
"""Delete all devices created by integration."""
channel_ids = list(coordinator.data)
async def delete_devices(hass: HomeAssistant, entry: YouTubeConfigEntry) -> None:
"""Delete devices for channels removed from the entry options."""
channel_ids = set(entry.options[CONF_CHANNELS])
device_registry = dr.async_get(hass)
dev_entries = dr.async_entries_for_config_entry(device_registry, entry.entry_id)
for dev_entry in dev_entries:
if any(identifier[1] in channel_ids for identifier in dev_entry.identifiers):
# Identifiers are (DOMAIN, f"{entry_id}_{channel_id}").
if not any(
identifier[1].removeprefix(f"{entry.entry_id}_") in channel_ids
for identifier in dev_entry.identifiers
):
device_registry.async_remove_device(dev_entry.id)
62 changes: 62 additions & 0 deletions tests/components/youtube/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
ImplementationUnavailableError,
)

from . import MockYouTube
from .conftest import GOOGLE_TOKEN_URI, ComponentSetup

from tests.test_util.aiohttp import AiohttpClientMocker
Expand Down Expand Up @@ -139,6 +140,67 @@ async def test_device_info(
assert device.name == "Google for Developers"


async def test_delete_devices_for_removed_channels(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
setup_integration: ComponentSetup,
) -> None:
"""Test that devices for channels removed from the options are deleted."""
await setup_integration()

entry = hass.config_entries.async_entries(DOMAIN)[0]
channel_id = entry.options[CONF_CHANNELS][0]
device = device_registry.async_get_device_by_identifier(
(DOMAIN, f"{entry.entry_id}_{channel_id}"), entry.entry_id
)
assert device is not None

hass.config_entries.async_update_entry(entry, options={CONF_CHANNELS: []})
with patch(
"homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource",
return_value=MockYouTube(hass, channel_fixture="get_no_channel.json"),
):
await hass.config_entries.async_reload(entry.entry_id)
await hass.async_block_till_done()

assert (
device_registry.async_get_device_by_identifier(
(DOMAIN, f"{entry.entry_id}_{channel_id}"), entry.entry_id
)
is None
)


async def test_keep_device_for_channel_missing_from_api(
hass: HomeAssistant,
device_registry: dr.DeviceRegistry,
setup_integration: ComponentSetup,
) -> None:
"""Test that the device is kept when the API omits a configured channel."""
await setup_integration()

entry = hass.config_entries.async_entries(DOMAIN)[0]
channel_id = entry.options[CONF_CHANNELS][0]
device = device_registry.async_get_device_by_identifier(
(DOMAIN, f"{entry.entry_id}_{channel_id}"), entry.entry_id
)
assert device is not None

with patch(
"homeassistant.components.youtube.api.AsyncConfigEntryAuth.get_resource",
return_value=MockYouTube(hass, channel_fixture="get_no_channel.json"),
):
await hass.config_entries.async_reload(entry.entry_id)
await hass.async_block_till_done()

assert (
device_registry.async_get_device_by_identifier(
(DOMAIN, f"{entry.entry_id}_{channel_id}"), entry.entry_id
)
is not None
)


async def test_oauth_implementation_not_available(
hass: HomeAssistant, setup_integration: ComponentSetup
) -> None:
Expand Down
Loading