2018-08-01 09:03:08 +00:00
|
|
|
"""deCONZ switch platform tests."""
|
2020-10-17 16:20:06 +00:00
|
|
|
|
2021-03-18 10:44:52 +00:00
|
|
|
from unittest.mock import patch
|
2019-09-18 10:47:26 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
from homeassistant.components.switch import (
|
|
|
|
DOMAIN as SWITCH_DOMAIN,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
)
|
2021-02-08 09:45:46 +00:00
|
|
|
from homeassistant.const import ATTR_ENTITY_ID, STATE_OFF, STATE_ON, STATE_UNAVAILABLE
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
from .test_gateway import (
|
|
|
|
DECONZ_WEB_REQUEST,
|
|
|
|
mock_deconz_put_request,
|
|
|
|
setup_deconz_integration,
|
|
|
|
)
|
2019-09-19 21:44:09 +00:00
|
|
|
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
async def test_no_switches(hass, aioclient_mock):
|
2018-08-01 09:03:08 +00:00
|
|
|
"""Test that no switch entities are created."""
|
2021-02-09 07:31:29 +00:00
|
|
|
await setup_deconz_integration(hass, aioclient_mock)
|
2018-08-01 09:03:08 +00:00
|
|
|
assert len(hass.states.async_all()) == 0
|
|
|
|
|
|
|
|
|
2021-03-18 10:44:52 +00:00
|
|
|
async def test_power_plugs(hass, aioclient_mock, mock_deconz_websocket):
|
2018-09-21 17:59:20 +00:00
|
|
|
"""Test that all supported switch entities are created."""
|
2021-03-18 10:44:52 +00:00
|
|
|
data = {
|
|
|
|
"lights": {
|
|
|
|
"1": {
|
|
|
|
"name": "On off switch",
|
|
|
|
"type": "On/Off plug-in unit",
|
|
|
|
"state": {"on": True, "reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
|
|
|
},
|
|
|
|
"2": {
|
|
|
|
"name": "Smart plug",
|
|
|
|
"type": "Smart plug",
|
|
|
|
"state": {"on": False, "reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:01-00",
|
|
|
|
},
|
|
|
|
"3": {
|
|
|
|
"name": "Unsupported switch",
|
|
|
|
"type": "Not a switch",
|
|
|
|
"state": {"reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:03-00",
|
|
|
|
},
|
|
|
|
"4": {
|
|
|
|
"name": "On off relay",
|
|
|
|
"state": {"on": True, "reachable": True},
|
|
|
|
"type": "On/Off light",
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:04-00",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
with patch.dict(DECONZ_WEB_REQUEST, data):
|
|
|
|
config_entry = await setup_deconz_integration(hass, aioclient_mock)
|
2018-08-01 09:03:08 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
assert len(hass.states.async_all()) == 4
|
2020-10-17 16:20:06 +00:00
|
|
|
assert hass.states.get("switch.on_off_switch").state == STATE_ON
|
|
|
|
assert hass.states.get("switch.smart_plug").state == STATE_OFF
|
|
|
|
assert hass.states.get("switch.on_off_relay").state == STATE_ON
|
2020-10-02 09:20:33 +00:00
|
|
|
assert hass.states.get("switch.unsupported_switch") is None
|
2020-02-01 16:08:49 +00:00
|
|
|
|
2021-03-18 10:44:52 +00:00
|
|
|
event_changed_light = {
|
2019-12-08 15:53:34 +00:00
|
|
|
"t": "event",
|
|
|
|
"e": "changed",
|
|
|
|
"r": "lights",
|
|
|
|
"id": "1",
|
|
|
|
"state": {"on": False},
|
|
|
|
}
|
2021-03-18 10:44:52 +00:00
|
|
|
await mock_deconz_websocket(data=event_changed_light)
|
|
|
|
await hass.async_block_till_done()
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2020-10-17 16:20:06 +00:00
|
|
|
assert hass.states.get("switch.on_off_switch").state == STATE_OFF
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
# Verify service calls
|
2018-10-31 21:38:04 +00:00
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
mock_deconz_put_request(aioclient_mock, config_entry.data, "/lights/1/state")
|
2019-12-08 15:53:34 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
# Service turn on power plug
|
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
SWITCH_DOMAIN,
|
|
|
|
SERVICE_TURN_ON,
|
|
|
|
{ATTR_ENTITY_ID: "switch.on_off_switch"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert aioclient_mock.mock_calls[1][2] == {"on": True}
|
2019-09-18 10:47:26 +00:00
|
|
|
|
2020-10-02 09:20:33 +00:00
|
|
|
# Service turn off power plug
|
|
|
|
|
2021-02-09 07:31:29 +00:00
|
|
|
await hass.services.async_call(
|
|
|
|
SWITCH_DOMAIN,
|
|
|
|
SERVICE_TURN_OFF,
|
|
|
|
{ATTR_ENTITY_ID: "switch.on_off_switch"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
assert aioclient_mock.mock_calls[2][2] == {"on": False}
|
2019-12-08 15:53:34 +00:00
|
|
|
|
2020-10-06 21:25:57 +00:00
|
|
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
2020-10-02 09:20:33 +00:00
|
|
|
|
2021-02-08 09:45:46 +00:00
|
|
|
states = hass.states.async_all()
|
2021-03-18 10:44:52 +00:00
|
|
|
assert len(states) == 4
|
2021-02-08 09:45:46 +00:00
|
|
|
for state in states:
|
|
|
|
assert state.state == STATE_UNAVAILABLE
|
|
|
|
|
|
|
|
await hass.config_entries.async_remove(config_entry.entry_id)
|
|
|
|
await hass.async_block_till_done()
|
2020-10-02 09:20:33 +00:00
|
|
|
assert len(hass.states.async_all()) == 0
|