2019-07-04 10:44:40 +00:00
|
|
|
"""Test ZHA Device Tracker."""
|
|
|
|
from datetime import timedelta
|
|
|
|
import time
|
2019-10-21 17:14:17 +00:00
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
import pytest
|
2019-10-21 23:30:56 +00:00
|
|
|
import zigpy.zcl.clusters.general as general
|
|
|
|
import zigpy.zcl.foundation as zcl_f
|
2019-10-21 17:14:17 +00:00
|
|
|
|
2019-07-04 10:44:40 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN, SOURCE_TYPE_ROUTER
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.zha.core.registries import (
|
|
|
|
SMARTTHINGS_ARRIVAL_SENSOR_DEVICE_TYPE,
|
2019-07-04 10:44:40 +00:00
|
|
|
)
|
2019-10-21 17:14:17 +00:00
|
|
|
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_UNAVAILABLE
|
2019-07-04 10:44:40 +00:00
|
|
|
import homeassistant.util.dt as dt_util
|
2019-10-21 17:14:17 +00:00
|
|
|
|
2019-07-04 10:44:40 +00:00
|
|
|
from .common import (
|
2019-10-21 17:14:17 +00:00
|
|
|
async_enable_traffic,
|
2020-02-12 21:12:14 +00:00
|
|
|
async_test_rejoin,
|
2019-10-31 16:31:06 +00:00
|
|
|
find_entity_id,
|
2019-07-31 19:25:30 +00:00
|
|
|
make_attribute,
|
2019-10-21 17:14:17 +00:00
|
|
|
make_zcl_header,
|
2019-07-04 10:44:40 +00:00
|
|
|
)
|
2019-10-21 17:14:17 +00:00
|
|
|
|
2019-07-04 10:44:40 +00:00
|
|
|
from tests.common import async_fire_time_changed
|
|
|
|
|
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
@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)
|
|
|
|
|
|
|
|
|
2020-02-12 21:12:14 +00:00
|
|
|
async def test_device_tracker(hass, zha_device_joined_restored, zigpy_device_dt):
|
2019-07-04 10:44:40 +00:00
|
|
|
"""Test zha device tracker platform."""
|
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
zha_device = await zha_device_joined_restored(zigpy_device_dt)
|
|
|
|
cluster = zigpy_device_dt.endpoints.get(1).power
|
2019-10-31 16:31:06 +00:00
|
|
|
entity_id = await find_entity_id(DOMAIN, zha_device, hass)
|
|
|
|
assert entity_id is not None
|
2019-07-04 10:44:40 +00:00
|
|
|
|
|
|
|
# test that the device tracker was created and that it is unavailable
|
|
|
|
assert hass.states.get(entity_id).state == STATE_UNAVAILABLE
|
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
zigpy_device_dt.last_seen = time.time() - 120
|
2019-07-04 10:44:40 +00:00
|
|
|
next_update = dt_util.utcnow() + timedelta(seconds=30)
|
|
|
|
async_fire_time_changed(hass, next_update)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
# allow traffic to flow through the gateway and device
|
2020-02-12 21:12:14 +00:00
|
|
|
await async_enable_traffic(hass, [zha_device])
|
2019-07-04 10:44:40 +00:00
|
|
|
|
|
|
|
# test that the state has changed from unavailable to not home
|
|
|
|
assert hass.states.get(entity_id).state == STATE_NOT_HOME
|
|
|
|
|
|
|
|
# turn state flip
|
|
|
|
attr = make_attribute(0x0020, 23)
|
2019-10-21 23:30:56 +00:00
|
|
|
hdr = make_zcl_header(zcl_f.Command.Report_Attributes)
|
2019-10-21 17:14:17 +00:00
|
|
|
cluster.handle_message(hdr, [[attr]])
|
2019-07-04 10:44:40 +00:00
|
|
|
|
|
|
|
attr = make_attribute(0x0021, 200)
|
2019-10-21 17:14:17 +00:00
|
|
|
cluster.handle_message(hdr, [[attr]])
|
2019-07-04 10:44:40 +00:00
|
|
|
|
2020-02-10 02:45:35 +00:00
|
|
|
zigpy_device_dt.last_seen = time.time() + 10
|
2019-07-04 10:44:40 +00:00
|
|
|
next_update = dt_util.utcnow() + timedelta(seconds=30)
|
|
|
|
async_fire_time_changed(hass, next_update)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
|
|
|
assert hass.states.get(entity_id).state == STATE_HOME
|
|
|
|
|
|
|
|
entity = hass.data[DOMAIN].get_entity(entity_id)
|
|
|
|
|
|
|
|
assert entity.is_connected is True
|
|
|
|
assert entity.source_type == SOURCE_TYPE_ROUTER
|
|
|
|
assert entity.battery_level == 100
|
|
|
|
|
|
|
|
# test adding device tracker to the network and HA
|
2020-02-12 21:12:14 +00:00
|
|
|
await async_test_rejoin(hass, zigpy_device_dt, [cluster], (2,))
|
2020-02-10 02:45:35 +00:00
|
|
|
assert hass.states.get(entity_id).state == STATE_HOME
|