File size: 5,142 Bytes
3ea44ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | """deCONZ switch platform tests."""
from unittest.mock import Mock, patch
from homeassistant import config_entries
from homeassistant.components import deconz
from homeassistant.components.deconz.const import SWITCH_TYPES
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.setup import async_setup_component
import homeassistant.components.switch as switch
from tests.common import mock_coro
SUPPORTED_SWITCHES = {
"1": {
"id": "Switch 1 id",
"name": "Switch 1 name",
"type": "On/Off plug-in unit",
"state": {"on": True, "reachable": True},
"uniqueid": "00:00:00:00:00:00:00:00-00",
},
"2": {
"id": "Switch 2 id",
"name": "Switch 2 name",
"type": "Smart plug",
"state": {"on": True, "reachable": True},
},
"3": {
"id": "Switch 3 id",
"name": "Switch 3 name",
"type": "Warning device",
"state": {"alert": "lselect", "reachable": True},
},
}
UNSUPPORTED_SWITCH = {
"1": {
"id": "Switch id",
"name": "Unsupported switch",
"type": "Not a smart plug",
"state": {},
}
}
ENTRY_CONFIG = {
deconz.const.CONF_ALLOW_CLIP_SENSOR: True,
deconz.const.CONF_ALLOW_DECONZ_GROUPS: True,
deconz.config_flow.CONF_API_KEY: "ABCDEF",
deconz.config_flow.CONF_BRIDGEID: "0123456789",
deconz.config_flow.CONF_HOST: "1.2.3.4",
deconz.config_flow.CONF_PORT: 80,
}
async def setup_gateway(hass, data):
"""Load the deCONZ switch platform."""
from pydeconz import DeconzSession
loop = Mock()
session = Mock()
config_entry = config_entries.ConfigEntry(
1,
deconz.DOMAIN,
"Mock Title",
ENTRY_CONFIG,
"test",
config_entries.CONN_CLASS_LOCAL_PUSH,
)
gateway = deconz.DeconzGateway(hass, config_entry)
gateway.api = DeconzSession(loop, session, **config_entry.data)
gateway.api.config = Mock()
hass.data[deconz.DOMAIN] = {gateway.bridgeid: gateway}
with patch("pydeconz.DeconzSession.async_get_state", return_value=mock_coro(data)):
await gateway.api.async_load_parameters()
await hass.config_entries.async_forward_entry_setup(config_entry, "switch")
# To flush out the service call to update the group
await hass.async_block_till_done()
return gateway
async def test_platform_manually_configured(hass):
"""Test that we do not discover anything or try to set up a gateway."""
assert (
await async_setup_component(
hass, switch.DOMAIN, {"switch": {"platform": deconz.DOMAIN}}
)
is True
)
assert deconz.DOMAIN not in hass.data
async def test_no_switches(hass):
"""Test that no switch entities are created."""
gateway = await setup_gateway(hass, {})
assert not hass.data[deconz.DOMAIN][gateway.bridgeid].deconz_ids
assert len(hass.states.async_all()) == 0
async def test_switches(hass):
"""Test that all supported switch entities are created."""
with patch("pydeconz.DeconzSession.async_put_state", return_value=mock_coro(True)):
gateway = await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
assert "switch.switch_1_name" in gateway.deconz_ids
assert "switch.switch_2_name" in gateway.deconz_ids
assert "switch.switch_3_name" in gateway.deconz_ids
assert len(SUPPORTED_SWITCHES) == len(SWITCH_TYPES)
assert len(hass.states.async_all()) == 4
switch_1 = hass.states.get("switch.switch_1_name")
assert switch_1 is not None
assert switch_1.state == "on"
switch_3 = hass.states.get("switch.switch_3_name")
assert switch_3 is not None
assert switch_3.state == "on"
gateway.api.lights["1"].async_update({})
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.switch_1_name"}, blocking=True
)
await hass.services.async_call(
"switch", "turn_off", {"entity_id": "switch.switch_1_name"}, blocking=True
)
await hass.services.async_call(
"switch", "turn_on", {"entity_id": "switch.switch_3_name"}, blocking=True
)
await hass.services.async_call(
"switch", "turn_off", {"entity_id": "switch.switch_3_name"}, blocking=True
)
async def test_add_new_switch(hass):
"""Test successful creation of switch entity."""
gateway = await setup_gateway(hass, {})
switch = Mock()
switch.name = "name"
switch.type = "Smart plug"
switch.register_async_callback = Mock()
async_dispatcher_send(hass, gateway.async_event_new_device("light"), [switch])
await hass.async_block_till_done()
assert "switch.name" in gateway.deconz_ids
async def test_unsupported_switch(hass):
"""Test that unsupported switches are not created."""
await setup_gateway(hass, {"lights": UNSUPPORTED_SWITCH})
assert len(hass.states.async_all()) == 0
async def test_unload_switch(hass):
"""Test that it works to unload switch entities."""
gateway = await setup_gateway(hass, {"lights": SUPPORTED_SWITCHES})
await gateway.async_reset()
assert len(hass.states.async_all()) == 1
|