async def test_sensor_device(hass, aioclient_mock): """Test a sensor device.""" config = { "qwikswitch": { "sensors": { "name": "ss1", "id": "@a00001", "channel": 1, "type": "qwikcord", } } } 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) await async_setup_component(hass, QWIKSWITCH, config) hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done() state_obj = hass.states.get("sensor.ss1") assert state_obj.state == "None" listen_mock.queue_response(json={ "id": "@a00001", "name": "ss1", "type": "rel", "val": "4733800001a00000" }, ) await asyncio.sleep(0.01) await hass.async_block_till_done() state_obj = hass.states.get("sensor.ss1") assert state_obj.state == "416" listen_mock.stop()
async def test_button(hass, aioclient_mock, qs_devices): """Test that buttons fire an event.""" async def get_devices_json(method, url, data): return AiohttpClientMockResponse(method=method, url=url, json=qs_devices) config = {"qwikswitch": {"button_events": "TOGGLE"}} aioclient_mock.get("http://127.0.0.1:2020/&device", side_effect=get_devices_json) listen_mock = MockLongPollSideEffect() aioclient_mock.get("http://127.0.0.1:2020/&listen", side_effect=listen_mock) assert await async_setup_component(hass, QWIKSWITCH, config) await hass.async_start() await hass.async_block_till_done() button_pressed = Mock() hass.bus.async_listen_once("qwikswitch.button.@a00002", button_pressed) listen_mock.queue_response(json={"id": "@a00002", "cmd": "TOGGLE"}, ) await asyncio.sleep(0.01) await hass.async_block_till_done() button_pressed.assert_called_once() listen_mock.stop()
async def test_binary_sensor_device(hass, aioclient_mock, qs_devices): """Test a binary sensor device.""" config = { "qwikswitch": { "sensors": { "name": "s1", "id": "@a00001", "channel": 1, "type": "imod" } } } aioclient_mock.get("http://127.0.0.1:2020/&device", json=qs_devices) listen_mock = MockLongPollSideEffect() aioclient_mock.get("http://127.0.0.1:2020/&listen", side_effect=listen_mock) assert await async_setup_component(hass, QWIKSWITCH, config) await hass.async_start() await hass.async_block_till_done() # verify initial state is off per the 'val' in qs_devices state_obj = hass.states.get("binary_sensor.s1") assert state_obj.state == "off" # receive turn on command from network listen_mock.queue_response(json={ "id": "@a00001", "cmd": "STATUS.ACK", "data": "4e0e1601", "rssi": "61%" }) await asyncio.sleep(0.01) await hass.async_block_till_done() state_obj = hass.states.get("binary_sensor.s1") assert state_obj.state == "on" # receive turn off command from network listen_mock.queue_response(json={ "id": "@a00001", "cmd": "STATUS.ACK", "data": "4e0e1701", "rssi": "61%" }, ) await asyncio.sleep(0.01) await hass.async_block_till_done() state_obj = hass.states.get("binary_sensor.s1") assert state_obj.state == "off" listen_mock.stop()
async def test_binary_sensor_device(hass, aioclient_mock): """Test a binary sensor device.""" config = { "qwikswitch": { "sensors": { "name": "s1", "id": "@a00001", "channel": 1, "type": "imod" } } } 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) await async_setup_component(hass, QWIKSWITCH, config) hass.bus.async_fire(EVENT_HOMEASSISTANT_START) await hass.async_block_till_done() state_obj = hass.states.get("binary_sensor.s1") assert state_obj.state == "off" listen_mock.queue_response(json={ "id": "@a00001", "cmd": "", "data": "4e0e1601", "rssi": "61%" }) await asyncio.sleep(0.01) await hass.async_block_till_done() state_obj = hass.states.get("binary_sensor.s1") assert state_obj.state == "on" listen_mock.queue_response(json={ "id": "@a00001", "cmd": "", "data": "4e0e1701", "rssi": "61%" }, ) await asyncio.sleep(0.01) await hass.async_block_till_done() state_obj = hass.states.get("binary_sensor.s1") assert state_obj.state == "off" listen_mock.stop()
async def test_light_device(hass, aioclient_mock, qs_devices): """Test a light device.""" async def get_devices_json(method, url, data): return AiohttpClientMockResponse(method=method, url=url, json=qs_devices) config = {"qwikswitch": {}} aioclient_mock.get("http://127.0.0.1:2020/&device", side_effect=get_devices_json) listen_mock = MockLongPollSideEffect() aioclient_mock.get("http://127.0.0.1:2020/&listen", side_effect=listen_mock) assert await async_setup_component(hass, QWIKSWITCH, config) await hass.async_start() await hass.async_block_till_done() # verify initial state is on per the 'val' in qs_devices state_obj = hass.states.get("light.dim_3") assert state_obj.state == "on" assert state_obj.attributes["brightness"] == 255 # ask hass to turn off and verify command is sent to device aioclient_mock.mock_calls.clear() aioclient_mock.get("http://127.0.0.1:2020/@a00003=0", json={"data": "OK"}) await hass.services.async_call("light", "turn_off", {"entity_id": "light.dim_3"}, blocking=True) await asyncio.sleep(0.01) assert ( "GET", URL("http://127.0.0.1:2020/@a00003=0"), None, None, ) in aioclient_mock.mock_calls state_obj = hass.states.get("light.dim_3") assert state_obj.state == "off" # change brightness in network and check that hass updates qs_devices[2]["val"] = "280c55" # half dimmed listen_mock.queue_response(json=EMPTY_PACKET) await asyncio.sleep(0.01) await hass.async_block_till_done() state_obj = hass.states.get("light.dim_3") assert state_obj.state == "on" assert 16 < state_obj.attributes["brightness"] < 240 # turn off in the network and see that it is off in hass as well qs_devices[2]["val"] = "280c78" # off listen_mock.queue_response(json=EMPTY_PACKET) await asyncio.sleep(0.01) await hass.async_block_till_done() state_obj = hass.states.get("light.dim_3") assert state_obj.state == "off" # ask hass to turn on and verify command is sent to device aioclient_mock.mock_calls.clear() aioclient_mock.get("http://127.0.0.1:2020/@a00003=100", json={"data": "OK"}) await hass.services.async_call("light", "turn_on", {"entity_id": "light.dim_3"}, blocking=True) assert ( "GET", URL("http://127.0.0.1:2020/@a00003=100"), None, None, ) in aioclient_mock.mock_calls await hass.async_block_till_done() state_obj = hass.states.get("light.dim_3") assert state_obj.state == "on" listen_mock.stop()
async def test_switch_device(hass, aioclient_mock, qs_devices): """Test a switch device.""" async def get_devices_json(method, url, data): return AiohttpClientMockResponse(method=method, url=url, json=qs_devices) config = {"qwikswitch": {"switches": ["@a00001"]}} aioclient_mock.get("http://127.0.0.1:2020/&device", side_effect=get_devices_json) listen_mock = MockLongPollSideEffect() aioclient_mock.get("http://127.0.0.1:2020/&listen", side_effect=listen_mock) assert await async_setup_component(hass, QWIKSWITCH, config) await hass.async_start() await hass.async_block_till_done() # verify initial state is off per the 'val' in qs_devices state_obj = hass.states.get("switch.switch_1") assert state_obj.state == "off" # ask hass to turn on and verify command is sent to device aioclient_mock.mock_calls.clear() aioclient_mock.get("http://127.0.0.1:2020/@a00001=100", json={"data": "OK"}) await hass.services.async_call("switch", "turn_on", {"entity_id": "switch.switch_1"}, blocking=True) await asyncio.sleep(0.01) assert ( "GET", URL("http://127.0.0.1:2020/@a00001=100"), None, None, ) in aioclient_mock.mock_calls # verify state is on state_obj = hass.states.get("switch.switch_1") assert state_obj.state == "on" # ask hass to turn off and verify command is sent to device aioclient_mock.mock_calls.clear() aioclient_mock.get("http://127.0.0.1:2020/@a00001=0", json={"data": "OK"}) await hass.services.async_call("switch", "turn_off", {"entity_id": "switch.switch_1"}, blocking=True) assert ( "GET", URL("http://127.0.0.1:2020/@a00001=0"), None, None, ) in aioclient_mock.mock_calls # verify state is off state_obj = hass.states.get("switch.switch_1") assert state_obj.state == "off" # check if setting the value in the network show in hass qs_devices[0]["val"] = "ON" listen_mock.queue_response(json=EMPTY_PACKET) await hass.async_block_till_done() state_obj = hass.states.get("switch.switch_1") assert state_obj.state == "on" listen_mock.stop()