2018-04-08 19:59:19 +00:00
|
|
|
"""Test qwikswitch sensors."""
|
2020-04-10 21:57:39 +00:00
|
|
|
import asyncio
|
2018-04-08 19:59:19 +00:00
|
|
|
import logging
|
|
|
|
|
2019-12-09 13:56:20 +00:00
|
|
|
from homeassistant.components.qwikswitch import DOMAIN as QWIKSWITCH
|
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
2020-04-10 21:57:39 +00:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-04-08 19:59:19 +00:00
|
|
|
|
2020-04-10 21:57:39 +00:00
|
|
|
from tests.test_util.aiohttp import MockLongPollSideEffect
|
2018-04-08 19:59:19 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2020-04-10 21:57:39 +00:00
|
|
|
DEVICES = [
|
|
|
|
{
|
|
|
|
"id": "@000001",
|
|
|
|
"name": "Switch 1",
|
|
|
|
"type": "rel",
|
|
|
|
"val": "OFF",
|
|
|
|
"time": "1522777506",
|
|
|
|
"rssi": "51%",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "@000002",
|
|
|
|
"name": "Light 2",
|
|
|
|
"type": "rel",
|
|
|
|
"val": "ON",
|
|
|
|
"time": "1522777507",
|
|
|
|
"rssi": "45%",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": "@000003",
|
|
|
|
"name": "Dim 3",
|
|
|
|
"type": "dim",
|
|
|
|
"val": "280c00",
|
|
|
|
"time": "1522777544",
|
|
|
|
"rssi": "62%",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
async def test_binary_sensor_device(hass, aioclient_mock):
|
2018-04-21 06:34:42 +00:00
|
|
|
"""Test a binary sensor device."""
|
2018-04-08 19:59:19 +00:00
|
|
|
config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"qwikswitch": {
|
|
|
|
"sensors": {"name": "s1", "id": "@a00001", "channel": 1, "type": "imod"}
|
2018-04-08 19:59:19 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 21:57:39 +00:00
|
|
|
aioclient_mock.get("http://127.0.0.1:2020/&device", json=DEVICES)
|
|
|
|
listen_mock = MockLongPollSideEffect()
|
|
|
|
aioclient_mock.get("http://127.0.0.1:2020/&listen", side_effect=listen_mock)
|
2018-04-08 19:59:19 +00:00
|
|
|
await async_setup_component(hass, QWIKSWITCH, config)
|
2020-04-10 21:57:39 +00:00
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
2018-04-08 19:59:19 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state_obj = hass.states.get("binary_sensor.s1")
|
|
|
|
assert state_obj.state == "off"
|
2018-04-21 06:34:42 +00:00
|
|
|
|
2020-04-10 21:57:39 +00:00
|
|
|
listen_mock.queue_response(
|
|
|
|
json={"id": "@a00001", "cmd": "", "data": "4e0e1601", "rssi": "61%"}
|
|
|
|
)
|
|
|
|
await asyncio.sleep(0.01)
|
2018-04-08 19:59:19 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state_obj = hass.states.get("binary_sensor.s1")
|
|
|
|
assert state_obj.state == "on"
|
2018-04-21 06:34:42 +00:00
|
|
|
|
2020-04-10 21:57:39 +00:00
|
|
|
listen_mock.queue_response(
|
|
|
|
json={"id": "@a00001", "cmd": "", "data": "4e0e1701", "rssi": "61%"},
|
|
|
|
)
|
|
|
|
await asyncio.sleep(0.01)
|
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state_obj = hass.states.get("binary_sensor.s1")
|
|
|
|
assert state_obj.state == "off"
|
2018-04-21 06:34:42 +00:00
|
|
|
|
2020-04-10 21:57:39 +00:00
|
|
|
listen_mock.stop()
|
|
|
|
|
2018-04-21 06:34:42 +00:00
|
|
|
|
2020-04-10 21:57:39 +00:00
|
|
|
async def test_sensor_device(hass, aioclient_mock):
|
2018-04-21 06:34:42 +00:00
|
|
|
"""Test a sensor device."""
|
|
|
|
config = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"qwikswitch": {
|
|
|
|
"sensors": {
|
|
|
|
"name": "ss1",
|
|
|
|
"id": "@a00001",
|
|
|
|
"channel": 1,
|
|
|
|
"type": "qwikcord",
|
2018-04-21 06:34:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 21:57:39 +00:00
|
|
|
aioclient_mock.get("http://127.0.0.1:2020/&device", json=DEVICES)
|
|
|
|
listen_mock = MockLongPollSideEffect()
|
|
|
|
aioclient_mock.get("http://127.0.0.1:2020/&listen", side_effect=listen_mock)
|
2018-04-21 06:34:42 +00:00
|
|
|
await async_setup_component(hass, QWIKSWITCH, config)
|
2020-04-10 21:57:39 +00:00
|
|
|
hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
|
2019-04-02 03:57:25 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-04-10 21:57:39 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
state_obj = hass.states.get("sensor.ss1")
|
|
|
|
assert state_obj.state == "None"
|
2018-04-21 06:34:42 +00:00
|
|
|
|
2020-04-10 21:57:39 +00:00
|
|
|
listen_mock.queue_response(
|
|
|
|
json={"id": "@a00001", "name": "ss1", "type": "rel", "val": "4733800001a00000"},
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2020-04-10 21:57:39 +00:00
|
|
|
await asyncio.sleep(0.01)
|
2019-04-02 03:57:25 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
state_obj = hass.states.get("sensor.ss1")
|
|
|
|
assert state_obj.state == "416"
|
2020-04-10 21:57:39 +00:00
|
|
|
|
|
|
|
listen_mock.stop()
|