| 1 |
"""Test pydeCONZ cover.""" |
| 2 |
|
| 3 |
from copy import deepcopy |
| 4 |
from unittest.mock import Mock |
| 5 |
|
| 6 |
from pydeconz.interfaces.lights import CoverAction |
| 7 |
|
| 8 |
DATA = { |
| 9 |
"etag": "87269755b9b3a046485fdae8d96b252c", |
| 10 |
"hascolor": False, |
| 11 |
"lastannounced": None, |
| 12 |
"lastseen": "2020-08-01T16:22:05Z", |
| 13 |
"manufacturername": "AXIS", |
| 14 |
"modelid": "Gear", |
| 15 |
"name": "Covering device", |
| 16 |
"state": { |
| 17 |
"bri": 0, |
| 18 |
"lift": 0, |
| 19 |
"on": False, |
| 20 |
"open": True, |
| 21 |
"reachable": True, |
| 22 |
}, |
| 23 |
"swversion": "100-5.3.5.1122", |
| 24 |
"type": "Window covering device", |
| 25 |
"uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01", |
| 26 |
} |
| 27 |
|
| 28 |
DATA_LEGACY = { |
| 29 |
"etag": "87269755b9b3a046485fdae8d96b252c", |
| 30 |
"hascolor": False, |
| 31 |
"lastannounced": None, |
| 32 |
"lastseen": "2020-08-01T16:22:05Z", |
| 33 |
"manufacturername": "AXIS", |
| 34 |
"modelid": "Gear", |
| 35 |
"name": "Covering device", |
| 36 |
"state": { |
| 37 |
"bri": 0, |
| 38 |
"on": False, |
| 39 |
"reachable": True, |
| 40 |
}, |
| 41 |
"swversion": "100-5.3.5.1122", |
| 42 |
"type": "Window covering device", |
| 43 |
"uniqueid": "xx:xx:xx:xx:xx:xx:xx:xx-01", |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
async def test_handler_cover(mock_aioresponse, deconz_session, deconz_called_with): |
| 48 |
"""Verify that controlling covers work.""" |
| 49 |
covers = deconz_session.lights.covers |
| 50 |
|
| 51 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 52 |
await covers.set_state("0", action=CoverAction.OPEN) |
| 53 |
assert deconz_called_with("put", path="/lights/0/state", json={"open": True}) |
| 54 |
|
| 55 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 56 |
await covers.set_state("0", action=CoverAction.CLOSE) |
| 57 |
assert deconz_called_with("put", path="/lights/0/state", json={"open": False}) |
| 58 |
|
| 59 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 60 |
await covers.set_state("0", action=CoverAction.STOP) |
| 61 |
assert deconz_called_with("put", path="/lights/0/state", json={"stop": True}) |
| 62 |
|
| 63 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 64 |
await covers.set_state("0", lift=30, tilt=60) |
| 65 |
assert deconz_called_with( |
| 66 |
"put", path="/lights/0/state", json={"lift": 30, "tilt": 60} |
| 67 |
) |
| 68 |
|
| 69 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 70 |
await covers.set_state("0", action=CoverAction.STOP, lift=20) |
| 71 |
assert deconz_called_with("put", path="/lights/0/state", json={"stop": True}) |
| 72 |
|
| 73 |
|
| 74 |
async def test_handler_cover_legacy( |
| 75 |
mock_aioresponse, deconz_session, deconz_called_with |
| 76 |
): |
| 77 |
"""Verify that controlling covers work.""" |
| 78 |
covers = deconz_session.lights.covers |
| 79 |
|
| 80 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 81 |
await covers.set_state("0", action=CoverAction.OPEN, legacy_mode=True) |
| 82 |
assert deconz_called_with("put", path="/lights/0/state", json={"on": False}) |
| 83 |
|
| 84 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 85 |
await covers.set_state("0", action=CoverAction.CLOSE, legacy_mode=True) |
| 86 |
assert deconz_called_with("put", path="/lights/0/state", json={"on": True}) |
| 87 |
|
| 88 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 89 |
await covers.set_state("0", action=CoverAction.STOP, legacy_mode=True) |
| 90 |
assert deconz_called_with("put", path="/lights/0/state", json={"bri_inc": 0}) |
| 91 |
|
| 92 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 93 |
await covers.set_state("0", lift=30, tilt=60, legacy_mode=True) |
| 94 |
assert deconz_called_with( |
| 95 |
"put", path="/lights/0/state", json={"bri": 76, "sat": 152} |
| 96 |
) |
| 97 |
|
| 98 |
mock_aioresponse.put("http://host:80/api/apikey/lights/0/state") |
| 99 |
await covers.set_state("0", action=CoverAction.STOP, lift=20) |
| 100 |
assert deconz_called_with("put", path="/lights/0/state", json={"stop": True}) |
| 101 |
|
| 102 |
|
| 103 |
async def test_light_cover(deconz_light): |
| 104 |
"""Verify that covers work.""" |
| 105 |
cover = await deconz_light(DATA) |
| 106 |
|
| 107 |
assert cover.state is False |
| 108 |
assert cover.is_open is True |
| 109 |
assert cover.lift == 0 |
| 110 |
assert cover.tilt is None |
| 111 |
assert cover.supports_tilt is False |
| 112 |
|
| 113 |
assert cover.reachable is True |
| 114 |
|
| 115 |
assert cover.deconz_id == "/lights/0" |
| 116 |
assert cover.etag == "87269755b9b3a046485fdae8d96b252c" |
| 117 |
assert cover.manufacturer == "AXIS" |
| 118 |
assert cover.model_id == "Gear" |
| 119 |
assert cover.name == "Covering device" |
| 120 |
assert cover.software_version == "100-5.3.5.1122" |
| 121 |
assert cover.type == "Window covering device" |
| 122 |
assert cover.unique_id == "xx:xx:xx:xx:xx:xx:xx:xx-01" |
| 123 |
|
| 124 |
cover.register_callback(mock_callback := Mock()) |
| 125 |
assert cover._callbacks |
| 126 |
|
| 127 |
cover.update({"state": {"lift": 50, "open": True}}) |
| 128 |
assert cover.is_open is True |
| 129 |
assert cover.lift == 50 |
| 130 |
mock_callback.assert_called_once() |
| 131 |
assert cover.changed_keys == {"state", "lift", "open"} |
| 132 |
|
| 133 |
cover.update({"state": {"tilt": 25, "open": True}}) |
| 134 |
assert cover.tilt == 25 |
| 135 |
assert cover.supports_tilt is True |
| 136 |
|
| 137 |
cover.update({"state": {"bri": 30, "on": False}}) |
| 138 |
assert cover.is_open is True |
| 139 |
assert cover.lift == 50 |
| 140 |
|
| 141 |
cover.remove_callback(mock_callback) |
| 142 |
assert not cover._callbacks |
| 143 |
|
| 144 |
|
| 145 |
async def test_light_cover_legacy(deconz_light): |
| 146 |
"""Verify that covers work with older deconz versions.""" |
| 147 |
cover = await deconz_light(DATA_LEGACY) |
| 148 |
|
| 149 |
assert cover.state is False |
| 150 |
assert cover.is_open is True |
| 151 |
assert cover.lift == 0 |
| 152 |
assert cover.tilt is None |
| 153 |
|
| 154 |
assert cover.reachable is True |
| 155 |
|
| 156 |
assert cover.deconz_id == "/lights/0" |
| 157 |
assert cover.etag == "87269755b9b3a046485fdae8d96b252c" |
| 158 |
assert cover.manufacturer == "AXIS" |
| 159 |
assert cover.model_id == "Gear" |
| 160 |
assert cover.name == "Covering device" |
| 161 |
assert cover.software_version == "100-5.3.5.1122" |
| 162 |
assert cover.type == "Window covering device" |
| 163 |
assert cover.unique_id == "xx:xx:xx:xx:xx:xx:xx:xx-01" |
| 164 |
|
| 165 |
cover.register_callback(mock_callback := Mock()) |
| 166 |
assert cover._callbacks |
| 167 |
|
| 168 |
event = {"state": {"bri": 50, "on": True}} |
| 169 |
cover.update(event) |
| 170 |
assert cover.is_open is False |
| 171 |
assert cover.lift == 19 |
| 172 |
mock_callback.assert_called_once() |
| 173 |
assert cover.changed_keys == {"state", "bri", "on"} |
| 174 |
|
| 175 |
event = {"state": {"bri": 30, "on": False}} |
| 176 |
cover.update(event) |
| 177 |
assert cover.is_open is True |
| 178 |
assert cover.lift == 11 |
| 179 |
|
| 180 |
|
| 181 |
cover.raw["state"]["sat"] = 40 |
| 182 |
assert cover.tilt == 15 |
| 183 |
|
| 184 |
cover.raw["state"]["lift"] = 0 |
| 185 |
cover.raw["state"]["tilt"] = 0 |
| 186 |
cover.raw["state"]["open"] = True |
| 187 |
|
| 188 |
assert cover.tilt == 0 |
| 189 |
|
| 190 |
cover.remove_callback(mock_callback) |
| 191 |
assert not cover._callbacks |
| 192 |
|
| 193 |
|
| 194 |
async def test_cover_tilt_value_limit(deconz_light): |
| 195 |
"""Verify that value limit of tilt property work.""" |
| 196 |
data = deepcopy(DATA) |
| 197 |
data["state"]["tilt"] = 101 |
| 198 |
cover = await deconz_light(data) |
| 199 |
|
| 200 |
assert cover.tilt is None |