Add deCONZ service to clean up orphaned device and entity entries (#40629)
* Add service that can clean up by deCONZ orphaned entries from device and entity registry * Fix existing tests * Add new test * Add some inline comment to the service
This commit is contained in:
1 parent
9bdc716e60
commit
9d56d3bb31
4 files changed
+169
-6
No files matched your search
@@ -1,12 +1,16 @@
|
||||
"""deCONZ service tests."""
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components import deconz
|
||||
from homeassistant.components.deconz.const import CONF_BRIDGE_ID
|
||||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
|
||||
from homeassistant.helpers.entity_registry import async_entries_for_config_entry
|
||||
|
||||
from .test_gateway import BRIDGEID, setup_deconz_integration
|
||||
from .test_gateway import BRIDGEID, DECONZ_WEB_REQUEST, setup_deconz_integration
|
||||
|
||||
from tests.async_mock import Mock, patch
|
||||
|
||||
@@ -43,6 +47,17 @@ SENSOR = {
|
||||
}
|
||||
}
|
||||
|
||||
SWITCH = {
|
||||
"1": {
|
||||
"id": "Switch 1 id",
|
||||
"name": "Switch 1",
|
||||
"type": "ZHASwitch",
|
||||
"state": {"buttonevent": 1000, "gesture": 1},
|
||||
"config": {"battery": 100},
|
||||
"uniqueid": "00:00:00:00:00:00:00:03-00",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def test_service_setup(hass):
|
||||
"""Verify service setup works."""
|
||||
@@ -52,7 +67,7 @@ async def test_service_setup(hass):
|
||||
) as async_register:
|
||||
await deconz.services.async_setup_services(hass)
|
||||
assert hass.data[deconz.services.DECONZ_SERVICES] is True
|
||||
assert async_register.call_count == 2
|
||||
assert async_register.call_count == 3
|
||||
|
||||
|
||||
async def test_service_setup_already_registered(hass):
|
||||
@@ -73,7 +88,7 @@ async def test_service_unload(hass):
|
||||
) as async_remove:
|
||||
await deconz.services.async_unload_services(hass)
|
||||
assert hass.data[deconz.services.DECONZ_SERVICES] is False
|
||||
assert async_remove.call_count == 2
|
||||
assert async_remove.call_count == 3
|
||||
|
||||
|
||||
async def test_service_unload_not_registered(hass):
|
||||
@@ -198,3 +213,75 @@ async def test_service_refresh_devices(hass):
|
||||
"scene.group_1_name_scene_1": "/groups/1/scenes/1",
|
||||
"sensor.sensor_1_name": "/sensors/1",
|
||||
}
|
||||
|
||||
|
||||
async def test_remove_orphaned_entries_service(hass):
|
||||
"""Test service works and also don't remove more than expected."""
|
||||
data = deepcopy(DECONZ_WEB_REQUEST)
|
||||
data["lights"] = deepcopy(LIGHT)
|
||||
data["sensors"] = deepcopy(SWITCH)
|
||||
gateway = await setup_deconz_integration(hass, get_state_response=data)
|
||||
|
||||
data = {CONF_BRIDGE_ID: BRIDGEID}
|
||||
|
||||
device_registry = await hass.helpers.device_registry.async_get_registry()
|
||||
device = device_registry.async_get_or_create(
|
||||
config_entry_id=gateway.config_entry.entry_id, identifiers={("mac", "123")}
|
||||
)
|
||||
|
||||
assert (
|
||||
len(
|
||||
[
|
||||
entry
|
||||
for entry in device_registry.devices.values()
|
||||
if gateway.config_entry.entry_id in entry.config_entries
|
||||
]
|
||||
)
|
||||
== 4 # Gateway, light, switch and orphan
|
||||
)
|
||||
|
||||
entity_registry = await hass.helpers.entity_registry.async_get_registry()
|
||||
entity_registry.async_get_or_create(
|
||||
SENSOR_DOMAIN,
|
||||
deconz.DOMAIN,
|
||||
"12345",
|
||||
suggested_object_id="Orphaned sensor",
|
||||
config_entry=gateway.config_entry,
|
||||
device_id=device.id,
|
||||
)
|
||||
|
||||
assert (
|
||||
len(
|
||||
async_entries_for_config_entry(
|
||||
entity_registry, gateway.config_entry.entry_id
|
||||
)
|
||||
)
|
||||
== 3 # Light, switch battery and orphan
|
||||
)
|
||||
|
||||
await hass.services.async_call(
|
||||
deconz.DOMAIN,
|
||||
deconz.services.SERVICE_REMOVE_ORPHANED_ENTRIES,
|
||||
service_data=data,
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert (
|
||||
len(
|
||||
[
|
||||
entry
|
||||
for entry in device_registry.devices.values()
|
||||
if gateway.config_entry.entry_id in entry.config_entries
|
||||
]
|
||||
)
|
||||
== 3 # Gateway, light and switch
|
||||
)
|
||||
|
||||
assert (
|
||||
len(
|
||||
async_entries_for_config_entry(
|
||||
entity_registry, gateway.config_entry.entry_id
|
||||
)
|
||||
)
|
||||
== 2 # Light and switch battery
|
||||
)
|
||||
Reference in new issue
Block a user