Remove cleanup_registry from onewire (#60546)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-01 18:38:07 +01:00 committed by GitHub
parent 824b313705
commit 5c992ec2cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 1 additions and 85 deletions

View file

@ -1,11 +1,9 @@
"""The 1-Wire component."""
import asyncio
import logging
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from .const import DOMAIN, PLATFORMS
from .onewirehub import CannotConnect, OneWireHub
@ -25,34 +23,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.data[DOMAIN][entry.entry_id] = onewirehub
async def cleanup_registry(onewirehub: OneWireHub) -> None:
# Get registries
device_registry = dr.async_get(hass)
# Generate list of all device entries
registry_devices = list(
dr.async_entries_for_config_entry(device_registry, entry.entry_id)
)
# Remove devices that don't belong to any entity
for device in registry_devices:
if not onewirehub.has_device_in_cache(device):
_LOGGER.debug(
"Removing device `%s` because it is no longer available",
device.id,
)
device_registry.async_remove_device(device.id)
async def start_platforms(onewirehub: OneWireHub) -> None:
"""Start platforms and cleanup devices."""
# wait until all required platforms are ready
await asyncio.gather(
*(
hass.config_entries.async_forward_entry_setup(entry, platform)
for platform in PLATFORMS
)
)
await cleanup_registry(onewirehub)
hass.async_create_task(start_platforms(onewirehub))
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
return True

View file

@ -22,7 +22,6 @@ from homeassistant.const import (
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceEntry
from homeassistant.helpers.entity import DeviceInfo
from .const import (
@ -222,16 +221,6 @@ class OneWireHub:
assert isinstance(device_type, str)
return device_type
def has_device_in_cache(self, device: DeviceEntry) -> bool:
"""Check if device was present in the cache."""
if TYPE_CHECKING:
assert self.devices
for internal_device in self.devices:
for identifier in internal_device.device_info[ATTR_IDENTIFIERS]:
if identifier in device.identifiers:
return True
return False
class CannotConnect(HomeAssistantError):
"""Error to indicate we cannot connect."""

View file

@ -1,18 +1,11 @@
"""Tests for 1-Wire config flow."""
import logging
from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.onewire.const import DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
from . import setup_owproxy_mock_devices
from tests.common import mock_device_registry, mock_registry
@pytest.mark.usefixtures("owproxy_with_connerror")
@ -71,40 +64,3 @@ async def test_unload_sysbus_entry(
assert sysbus_config_entry.state is ConfigEntryState.NOT_LOADED
assert not hass.data.get(DOMAIN)
@patch("homeassistant.components.onewire.PLATFORMS", [SENSOR_DOMAIN])
async def test_registry_cleanup(
hass: HomeAssistant, config_entry: ConfigEntry, owproxy: MagicMock
):
"""Test for 1-Wire device.
As they would be on a clean setup: all binary-sensors and switches disabled.
"""
entity_registry = mock_registry(hass)
device_registry = mock_device_registry(hass)
# Initialise with two components
setup_owproxy_mock_devices(
owproxy, SENSOR_DOMAIN, ["10.111111111111", "28.111111111111"]
)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
assert len(dr.async_entries_for_config_entry(device_registry, "2")) == 2
assert len(er.async_entries_for_config_entry(entity_registry, "2")) == 2
# Second item has disappeared from bus, and was removed manually from the front-end
setup_owproxy_mock_devices(owproxy, SENSOR_DOMAIN, ["10.111111111111"])
entity_registry.async_remove("sensor.28_111111111111_temperature")
await hass.async_block_till_done()
assert len(er.async_entries_for_config_entry(entity_registry, "2")) == 1
assert len(dr.async_entries_for_config_entry(device_registry, "2")) == 2
# Second item has disappeared from bus, and was removed manually from the front-end
await hass.config_entries.async_reload("2")
await hass.async_block_till_done()
assert len(er.async_entries_for_config_entry(entity_registry, "2")) == 1
assert len(dr.async_entries_for_config_entry(device_registry, "2")) == 1