Remove ZHA device storage (#74837)
* Remove ZHA device storage * remove storage file if it exists
This commit is contained in:
parent
240a83239a
commit
edaafadde0
6 changed files with 15 additions and 259 deletions
|
@ -15,7 +15,6 @@ from zigpy.state import State
|
|||
import zigpy.types
|
||||
import zigpy.zdo.types as zdo_t
|
||||
|
||||
from homeassistant.components.zha import DOMAIN
|
||||
import homeassistant.components.zha.core.const as zha_const
|
||||
import homeassistant.components.zha.core.device as zha_core_device
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
@ -188,26 +187,14 @@ def zha_device_joined(hass, setup_zha):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def zha_device_restored(hass, zigpy_app_controller, setup_zha, hass_storage):
|
||||
def zha_device_restored(hass, zigpy_app_controller, setup_zha):
|
||||
"""Return a restored ZHA device."""
|
||||
|
||||
async def _zha_device(zigpy_dev, last_seen=None):
|
||||
zigpy_app_controller.devices[zigpy_dev.ieee] = zigpy_dev
|
||||
|
||||
if last_seen is not None:
|
||||
hass_storage[f"{DOMAIN}.storage"] = {
|
||||
"key": f"{DOMAIN}.storage",
|
||||
"version": 1,
|
||||
"data": {
|
||||
"devices": [
|
||||
{
|
||||
"ieee": str(zigpy_dev.ieee),
|
||||
"last_seen": last_seen,
|
||||
"name": f"{zigpy_dev.manufacturer} {zigpy_dev.model}",
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
zigpy_dev.last_seen = last_seen
|
||||
|
||||
await setup_zha()
|
||||
zha_gateway = hass.data[zha_const.DATA_ZHA][zha_const.DATA_ZHA_GATEWAY]
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
"""Test ZHA Gateway."""
|
||||
import asyncio
|
||||
import math
|
||||
import time
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
|
@ -10,10 +8,9 @@ import zigpy.zcl.clusters.general as general
|
|||
import zigpy.zcl.clusters.lighting as lighting
|
||||
|
||||
from homeassistant.components.zha.core.group import GroupMember
|
||||
from homeassistant.components.zha.core.store import TOMBSTONE_LIFETIME
|
||||
from homeassistant.const import Platform
|
||||
|
||||
from .common import async_enable_traffic, async_find_group_entity_id, get_zha_gateway
|
||||
from .common import async_find_group_entity_id, get_zha_gateway
|
||||
from .conftest import SIG_EP_INPUT, SIG_EP_OUTPUT, SIG_EP_PROFILE, SIG_EP_TYPE
|
||||
|
||||
IEEE_GROUPABLE_DEVICE = "01:2d:6f:00:0a:90:69:e8"
|
||||
|
@ -214,62 +211,3 @@ async def test_gateway_create_group_with_id(hass, device_light_1, coordinator):
|
|||
assert len(zha_group.members) == 1
|
||||
assert zha_group.members[0].device is device_light_1
|
||||
assert zha_group.group_id == 0x1234
|
||||
|
||||
|
||||
async def test_updating_device_store(hass, zigpy_dev_basic, zha_dev_basic):
|
||||
"""Test saving data after a delay."""
|
||||
zha_gateway = get_zha_gateway(hass)
|
||||
assert zha_gateway is not None
|
||||
await async_enable_traffic(hass, [zha_dev_basic])
|
||||
|
||||
assert zha_dev_basic.last_seen is not None
|
||||
entry = zha_gateway.zha_storage.async_get_or_create_device(zha_dev_basic)
|
||||
assert math.isclose(entry.last_seen, zha_dev_basic.last_seen, rel_tol=1e-06)
|
||||
|
||||
assert zha_dev_basic.last_seen is not None
|
||||
last_seen = zha_dev_basic.last_seen
|
||||
|
||||
# test that we can't set None as last seen any more
|
||||
zha_dev_basic.async_update_last_seen(None)
|
||||
assert math.isclose(last_seen, zha_dev_basic.last_seen, rel_tol=1e-06)
|
||||
|
||||
# test that we won't put None in storage
|
||||
zigpy_dev_basic.last_seen = None
|
||||
assert zha_dev_basic.last_seen is None
|
||||
await zha_gateway.async_update_device_storage()
|
||||
await hass.async_block_till_done()
|
||||
entry = zha_gateway.zha_storage.async_get_or_create_device(zha_dev_basic)
|
||||
assert math.isclose(entry.last_seen, last_seen, rel_tol=1e-06)
|
||||
|
||||
# test that we can still set a good last_seen
|
||||
last_seen = time.time()
|
||||
zha_dev_basic.async_update_last_seen(last_seen)
|
||||
assert math.isclose(last_seen, zha_dev_basic.last_seen, rel_tol=1e-06)
|
||||
|
||||
# test that we still put good values in storage
|
||||
await zha_gateway.async_update_device_storage()
|
||||
await hass.async_block_till_done()
|
||||
entry = zha_gateway.zha_storage.async_get_or_create_device(zha_dev_basic)
|
||||
assert math.isclose(entry.last_seen, last_seen, rel_tol=1e-06)
|
||||
|
||||
|
||||
async def test_cleaning_up_storage(hass, zigpy_dev_basic, zha_dev_basic, hass_storage):
|
||||
"""Test cleaning up zha storage and remove stale devices."""
|
||||
zha_gateway = get_zha_gateway(hass)
|
||||
assert zha_gateway is not None
|
||||
await async_enable_traffic(hass, [zha_dev_basic])
|
||||
|
||||
assert zha_dev_basic.last_seen is not None
|
||||
await zha_gateway.zha_storage.async_save()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass_storage["zha.storage"]["data"]["devices"]
|
||||
device = hass_storage["zha.storage"]["data"]["devices"][0]
|
||||
assert device["ieee"] == str(zha_dev_basic.ieee)
|
||||
|
||||
zha_dev_basic.device.last_seen = time.time() - TOMBSTONE_LIFETIME - 1
|
||||
await zha_gateway.async_update_device_storage()
|
||||
await hass.async_block_till_done()
|
||||
await zha_gateway.zha_storage.async_save()
|
||||
await hass.async_block_till_done()
|
||||
assert not hass_storage["zha.storage"]["data"]["devices"]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue