2018-08-01 11:03:08 +02:00
|
|
|
"""deCONZ switch platform tests."""
|
2019-09-18 12:47:26 +02:00
|
|
|
from copy import deepcopy
|
|
|
|
|
2018-08-01 11:03:08 +02:00
|
|
|
from homeassistant.components import deconz
|
2020-10-06 23:25:57 +02:00
|
|
|
from homeassistant.components.deconz.gateway import get_gateway_from_config_entry
|
2019-09-18 12:47:26 +02:00
|
|
|
import homeassistant.components.switch as switch
|
2019-12-09 12:25:35 +01:00
|
|
|
from homeassistant.setup import async_setup_component
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2020-01-03 18:11:04 +01:00
|
|
|
from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration
|
2019-09-19 23:44:09 +02:00
|
|
|
|
2020-04-30 13:29:50 -07:00
|
|
|
from tests.async_mock import patch
|
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
POWER_PLUGS = {
|
2018-08-01 11:03:08 +02:00
|
|
|
"1": {
|
2019-09-18 12:47:26 +02:00
|
|
|
"id": "On off switch id",
|
|
|
|
"name": "On off switch",
|
2018-08-01 11:03:08 +02:00
|
|
|
"type": "On/Off plug-in unit",
|
2018-10-31 22:38:04 +01:00
|
|
|
"state": {"on": True, "reachable": True},
|
2019-07-31 12:25:30 -07:00
|
|
|
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
2018-08-01 11:03:08 +02:00
|
|
|
},
|
|
|
|
"2": {
|
2019-09-18 12:47:26 +02:00
|
|
|
"id": "Smart plug id",
|
|
|
|
"name": "Smart plug",
|
2018-08-01 11:03:08 +02:00
|
|
|
"type": "Smart plug",
|
2019-09-18 12:47:26 +02:00
|
|
|
"state": {"on": False, "reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:01-00",
|
2018-08-10 19:22:12 +02:00
|
|
|
},
|
|
|
|
"3": {
|
2019-09-18 12:47:26 +02:00
|
|
|
"id": "Unsupported switch id",
|
2018-08-01 11:03:08 +02:00
|
|
|
"name": "Unsupported switch",
|
2020-10-02 11:20:33 +02:00
|
|
|
"type": "Not a switch",
|
2019-09-18 12:47:26 +02:00
|
|
|
"state": {"reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:03-00",
|
|
|
|
},
|
2020-10-02 11:20:33 +02:00
|
|
|
"4": {
|
2020-02-01 17:08:49 +01:00
|
|
|
"id": "On off relay id",
|
|
|
|
"name": "On off relay",
|
|
|
|
"state": {"on": True, "reachable": True},
|
|
|
|
"type": "On/Off light",
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:04-00",
|
|
|
|
},
|
2018-08-01 11:03:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
SIRENS = {
|
|
|
|
"1": {
|
|
|
|
"id": "Warning device id",
|
|
|
|
"name": "Warning device",
|
|
|
|
"type": "Warning device",
|
|
|
|
"state": {"alert": "lselect", "reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:00-00",
|
|
|
|
},
|
|
|
|
"2": {
|
|
|
|
"id": "Unsupported switch id",
|
|
|
|
"name": "Unsupported switch",
|
|
|
|
"type": "Not a switch",
|
|
|
|
"state": {"reachable": True},
|
|
|
|
"uniqueid": "00:00:00:00:00:00:00:01-00",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2018-10-31 22:38:04 +01:00
|
|
|
async def test_platform_manually_configured(hass):
|
|
|
|
"""Test that we do not discover anything or try to set up a gateway."""
|
2019-07-31 12:25:30 -07:00
|
|
|
assert (
|
|
|
|
await async_setup_component(
|
|
|
|
hass, switch.DOMAIN, {"switch": {"platform": deconz.DOMAIN}}
|
|
|
|
)
|
|
|
|
is True
|
|
|
|
)
|
2018-10-31 22:38:04 +01:00
|
|
|
assert deconz.DOMAIN not in hass.data
|
|
|
|
|
|
|
|
|
2018-08-01 11:03:08 +02:00
|
|
|
async def test_no_switches(hass):
|
|
|
|
"""Test that no switch entities are created."""
|
2020-10-02 11:20:33 +02:00
|
|
|
await setup_deconz_integration(hass)
|
2018-08-01 11:03:08 +02:00
|
|
|
assert len(hass.states.async_all()) == 0
|
|
|
|
|
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
async def test_power_plugs(hass):
|
2018-09-21 19:59:20 +02:00
|
|
|
"""Test that all supported switch entities are created."""
|
2019-09-18 12:47:26 +02:00
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
2020-10-02 11:20:33 +02:00
|
|
|
data["lights"] = deepcopy(POWER_PLUGS)
|
2020-10-06 23:25:57 +02:00
|
|
|
config_entry = await setup_deconz_integration(hass, get_state_response=data)
|
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2018-08-01 11:03:08 +02:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
assert len(hass.states.async_all()) == 4
|
|
|
|
assert hass.states.get("switch.on_off_switch").state == "on"
|
|
|
|
assert hass.states.get("switch.smart_plug").state == "off"
|
|
|
|
assert hass.states.get("switch.on_off_relay").state == "on"
|
|
|
|
assert hass.states.get("switch.unsupported_switch") is None
|
2020-02-01 17:08:49 +01:00
|
|
|
|
2019-12-08 16:53:34 +01:00
|
|
|
state_changed_event = {
|
|
|
|
"t": "event",
|
|
|
|
"e": "changed",
|
|
|
|
"r": "lights",
|
|
|
|
"id": "1",
|
|
|
|
"state": {"on": False},
|
|
|
|
}
|
2020-02-02 19:07:20 +01:00
|
|
|
gateway.api.event_handler(state_changed_event)
|
2018-10-31 22:38:04 +01:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
assert hass.states.get("switch.on_off_switch").state == "off"
|
2018-10-31 22:38:04 +01:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
# Verify service calls
|
2018-10-31 22:38:04 +01:00
|
|
|
|
2019-12-08 16:53:34 +01:00
|
|
|
on_off_switch_device = gateway.api.lights["1"]
|
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
# Service turn on power plug
|
|
|
|
|
2019-09-18 12:47:26 +02:00
|
|
|
with patch.object(
|
2019-12-08 16:53:34 +01:00
|
|
|
on_off_switch_device, "_request", return_value=True
|
2019-09-18 12:47:26 +02:00
|
|
|
) as set_callback:
|
|
|
|
await hass.services.async_call(
|
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_ON,
|
|
|
|
{"entity_id": "switch.on_off_switch"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2019-12-08 16:53:34 +01:00
|
|
|
set_callback.assert_called_with("put", "/lights/1/state", json={"on": True})
|
2019-09-18 12:47:26 +02:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
# Service turn off power plug
|
|
|
|
|
2019-09-18 12:47:26 +02:00
|
|
|
with patch.object(
|
2019-12-08 16:53:34 +01:00
|
|
|
on_off_switch_device, "_request", return_value=True
|
2019-09-18 12:47:26 +02:00
|
|
|
) as set_callback:
|
|
|
|
await hass.services.async_call(
|
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_OFF,
|
|
|
|
{"entity_id": "switch.on_off_switch"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2019-12-08 16:53:34 +01:00
|
|
|
set_callback.assert_called_with("put", "/lights/1/state", json={"on": False})
|
|
|
|
|
2020-10-06 23:25:57 +02:00
|
|
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
2020-10-02 11:20:33 +02:00
|
|
|
|
|
|
|
assert len(hass.states.async_all()) == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_sirens(hass):
|
|
|
|
"""Test that siren entities are created."""
|
|
|
|
data = deepcopy(DECONZ_WEB_REQUEST)
|
|
|
|
data["lights"] = deepcopy(SIRENS)
|
2020-10-06 23:25:57 +02:00
|
|
|
config_entry = await setup_deconz_integration(hass, get_state_response=data)
|
|
|
|
gateway = get_gateway_from_config_entry(hass, config_entry)
|
2020-10-02 11:20:33 +02:00
|
|
|
|
|
|
|
assert len(hass.states.async_all()) == 2
|
|
|
|
assert hass.states.get("switch.warning_device").state == "on"
|
|
|
|
assert hass.states.get("switch.unsupported_switch") is None
|
|
|
|
|
|
|
|
state_changed_event = {
|
|
|
|
"t": "event",
|
|
|
|
"e": "changed",
|
|
|
|
"r": "lights",
|
|
|
|
"id": "1",
|
|
|
|
"state": {"alert": None},
|
|
|
|
}
|
|
|
|
gateway.api.event_handler(state_changed_event)
|
|
|
|
|
|
|
|
assert hass.states.get("switch.warning_device").state == "off"
|
|
|
|
|
|
|
|
# Verify service calls
|
|
|
|
|
|
|
|
warning_device_device = gateway.api.lights["1"]
|
|
|
|
|
|
|
|
# Service turn on siren
|
2019-09-18 12:47:26 +02:00
|
|
|
|
|
|
|
with patch.object(
|
2019-12-08 16:53:34 +01:00
|
|
|
warning_device_device, "_request", return_value=True
|
2019-09-18 12:47:26 +02:00
|
|
|
) as set_callback:
|
|
|
|
await hass.services.async_call(
|
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_ON,
|
|
|
|
{"entity_id": "switch.warning_device"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2019-12-08 16:53:34 +01:00
|
|
|
set_callback.assert_called_with(
|
2020-10-02 11:20:33 +02:00
|
|
|
"put", "/lights/1/state", json={"alert": "lselect"}
|
2019-12-08 16:53:34 +01:00
|
|
|
)
|
2019-09-18 12:47:26 +02:00
|
|
|
|
2020-10-02 11:20:33 +02:00
|
|
|
# Service turn off siren
|
|
|
|
|
2019-09-18 12:47:26 +02:00
|
|
|
with patch.object(
|
2019-12-08 16:53:34 +01:00
|
|
|
warning_device_device, "_request", return_value=True
|
2019-09-18 12:47:26 +02:00
|
|
|
) as set_callback:
|
|
|
|
await hass.services.async_call(
|
|
|
|
switch.DOMAIN,
|
|
|
|
switch.SERVICE_TURN_OFF,
|
|
|
|
{"entity_id": "switch.warning_device"},
|
|
|
|
blocking=True,
|
|
|
|
)
|
|
|
|
await hass.async_block_till_done()
|
2019-12-08 16:53:34 +01:00
|
|
|
set_callback.assert_called_with(
|
2020-10-02 11:20:33 +02:00
|
|
|
"put", "/lights/1/state", json={"alert": "none"}
|
2019-12-08 16:53:34 +01:00
|
|
|
)
|
2019-09-19 23:44:09 +02:00
|
|
|
|
2020-10-06 23:25:57 +02:00
|
|
|
await hass.config_entries.async_unload(config_entry.entry_id)
|
2019-09-19 23:44:09 +02:00
|
|
|
|
2020-01-07 17:30:53 +01:00
|
|
|
assert len(hass.states.async_all()) == 0
|