ZHA tests refactoring (#31682)

* Fixtures for restoring/joning a device.

* binary_sensor.zha tests.

* cover.zha platform tests.

* device_tracker.zha platform tests.

* fan.zha platform tests.

* switch.zha platform tests.

* Update light.zha platform tests.

* Update sensor.zha platform tests.

* ZHA api tests refactoring.

* Update lock.zha platform tests.

* Update ZHA gateway tests.

* Update zha device action tests.

* Update zha device trigger tests.

* Cleanup.
This commit is contained in:
Alexei Chetroi authored and GitHub committed 2020-02-09 21:45:35 -05:00
1 parent 118ba10442
commit 28eeed1db3
14 files changed
+648 -741

No files matched your search

+36 -33
View File
@@ -2,6 +2,7 @@
from datetime import timedelta
import time
import pytest
import zigpy.zcl.clusters.general as general
import zigpy.zcl.foundation as zcl_f
@@ -14,8 +15,6 @@ import homeassistant.util.dt as dt_util
from .common import (
async_enable_traffic,
async_init_zigpy_device,
async_test_device_join,
find_entity_id,
make_attribute,
make_zcl_header,
@@ -24,37 +23,39 @@ from .common import (
from tests.common import async_fire_time_changed
async def test_device_tracker(hass, config_entry, zha_gateway):
@pytest.fixture
def zigpy_device_dt(zigpy_device_mock):
"""Device tracker zigpy device."""
endpoints = {
1: {
"in_clusters": [
general.Basic.cluster_id,
general.PowerConfiguration.cluster_id,
general.Identify.cluster_id,
general.PollControl.cluster_id,
general.BinaryInput.cluster_id,
],
"out_clusters": [general.Identify.cluster_id, general.Ota.cluster_id],
"device_type": SMARTTHINGS_ARRIVAL_SENSOR_DEVICE_TYPE,
}
}
return zigpy_device_mock(endpoints)
async def test_device_tracker(
hass, zha_gateway, zha_device_joined_restored, zigpy_device_dt
):
"""Test zha device tracker platform."""
# create zigpy device
zigpy_device = await async_init_zigpy_device(
hass,
[
general.Basic.cluster_id,
general.PowerConfiguration.cluster_id,
general.Identify.cluster_id,
general.PollControl.cluster_id,
general.BinaryInput.cluster_id,
],
[general.Identify.cluster_id, general.Ota.cluster_id],
SMARTTHINGS_ARRIVAL_SENSOR_DEVICE_TYPE,
zha_gateway,
)
# load up device tracker domain
await hass.config_entries.async_forward_entry_setup(config_entry, DOMAIN)
await hass.async_block_till_done()
cluster = zigpy_device.endpoints.get(1).power
zha_device = zha_gateway.get_device(zigpy_device.ieee)
zha_device = await zha_device_joined_restored(zigpy_device_dt)
cluster = zigpy_device_dt.endpoints.get(1).power
entity_id = await find_entity_id(DOMAIN, zha_device, hass)
assert entity_id is not None
# test that the device tracker was created and that it is unavailable
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
zigpy_device.last_seen = time.time() - 120
zigpy_device_dt.last_seen = time.time() - 120
next_update = dt_util.utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, next_update)
await hass.async_block_till_done()
@@ -73,7 +74,7 @@ async def test_device_tracker(hass, config_entry, zha_gateway):
attr = make_attribute(0x0021, 200)
cluster.handle_message(hdr, [[attr]])
zigpy_device.last_seen = time.time() + 10
zigpy_device_dt.last_seen = time.time() + 10
next_update = dt_util.utcnow() + timedelta(seconds=30)
async_fire_time_changed(hass, next_update)
await hass.async_block_till_done()
@@ -87,10 +88,12 @@ async def test_device_tracker(hass, config_entry, zha_gateway):
assert entity.battery_level == 100
# test adding device tracker to the network and HA
await async_test_device_join(
hass,
zha_gateway,
general.PowerConfiguration.cluster_id,
entity_id,
SMARTTHINGS_ARRIVAL_SENSOR_DEVICE_TYPE,
)
cluster.bind.reset_mock()
cluster.configure_reporting.reset_mock()
await zha_gateway.async_device_initialized(zigpy_device_dt)
await hass.async_block_till_done()
assert hass.states.get(entity_id).state == STATE_HOME
assert cluster.bind.call_count == 1
assert cluster.bind.await_count == 1
assert cluster.configure_reporting.call_count == 2
assert cluster.configure_reporting.await_count == 2