| 1 |
"""BleBox cover entities tests.""" |
| 2 |
|
| 3 |
import json |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from blebox_uniapi.box_types import get_latest_api_level |
| 8 |
from blebox_uniapi import error |
| 9 |
from blebox_uniapi.cover import Shutter, ShutterBoxControlType, UnifiedCoverType |
| 10 |
|
| 11 |
from .conftest import CommonEntity, DefaultBoxTest, future_date, jmerge |
| 12 |
|
| 13 |
|
| 14 |
ATTR_POSITION = "ATTR_POSITION" |
| 15 |
DEVICE_CLASS_DOOR = "DEVICE_CLASS_DOOR" |
| 16 |
DEVICE_CLASS_SHUTTER = "DEVICE_CLASS_SHUTTER" |
| 17 |
STATE_CLOSED = "STATE_CLOSED" |
| 18 |
STATE_CLOSING = "STATE_CLOSING" |
| 19 |
STATE_OPEN = "STATE_OPEN" |
| 20 |
STATE_OPENING = "STATE_OPENING" |
| 21 |
|
| 22 |
SUPPORT_OPEN = 1 |
| 23 |
SUPPORT_CLOSE = 2 |
| 24 |
SUPPORT_SET_POSITION = 4 |
| 25 |
SUPPORT_STOP = 8 |
| 26 |
|
| 27 |
|
| 28 |
def patch_version(apiLevel): |
| 29 |
"""Helper function for generate a patch for a JSON state fixture.""" |
| 30 |
return f"""{{ "device": {{ "apiLevel": {apiLevel} }} }}""" |
| 31 |
|
| 32 |
|
| 33 |
class BleBoxCoverEntity(CommonEntity): |
| 34 |
"""Representation of a BleBox cover feature.""" |
| 35 |
|
| 36 |
@property |
| 37 |
def state(self): |
| 38 |
"""Return the equivalent HA cover state.""" |
| 39 |
states = { |
| 40 |
None: None, |
| 41 |
0: STATE_CLOSING, |
| 42 |
1: STATE_OPENING, |
| 43 |
2: STATE_OPEN, |
| 44 |
3: STATE_CLOSED, |
| 45 |
4: STATE_OPEN, |
| 46 |
|
| 47 |
5: STATE_OPEN, |
| 48 |
6: STATE_OPEN, |
| 49 |
|
| 50 |
8: STATE_OPEN, |
| 51 |
} |
| 52 |
|
| 53 |
return states[self._feature.state] |
| 54 |
|
| 55 |
@property |
| 56 |
def device_class(self): |
| 57 |
"""Return the device class.""" |
| 58 |
types = { |
| 59 |
"shutter": DEVICE_CLASS_SHUTTER, |
| 60 |
"gatebox": DEVICE_CLASS_DOOR, |
| 61 |
|
| 62 |
"gate": DEVICE_CLASS_DOOR, |
| 63 |
} |
| 64 |
return types[self._feature.device_class] |
| 65 |
|
| 66 |
|
| 67 |
@property |
| 68 |
def supported_features(self): |
| 69 |
"""Return the supported cover features.""" |
| 70 |
position = SUPPORT_SET_POSITION if self._feature.is_slider else 0 |
| 71 |
stop = SUPPORT_STOP if self._feature.has_stop else 0 |
| 72 |
|
| 73 |
return position | stop | SUPPORT_OPEN | SUPPORT_CLOSE |
| 74 |
|
| 75 |
@property |
| 76 |
def current_cover_position(self): |
| 77 |
"""Return the current cover position.""" |
| 78 |
current = self._invert_position(self._feature.current) |
| 79 |
return current if current else None |
| 80 |
|
| 81 |
@property |
| 82 |
def is_opening(self): |
| 83 |
"""Return whether cover is opening.""" |
| 84 |
return self._is_state(STATE_OPENING) |
| 85 |
|
| 86 |
@property |
| 87 |
def is_closing(self): |
| 88 |
"""Return whether cover is closing.""" |
| 89 |
return self._is_state(STATE_CLOSING) |
| 90 |
|
| 91 |
@property |
| 92 |
def is_closed(self): |
| 93 |
"""Return whether cover is closed.""" |
| 94 |
return self._is_state(STATE_CLOSED) |
| 95 |
|
| 96 |
async def async_open_cover(self, **kwargs): |
| 97 |
"""Open the cover position.""" |
| 98 |
await self._feature.async_open() |
| 99 |
|
| 100 |
async def async_close_cover(self, **kwargs): |
| 101 |
"""Close the cover position.""" |
| 102 |
await self._feature.async_close() |
| 103 |
|
| 104 |
async def async_set_cover_position(self, **kwargs): |
| 105 |
"""Set the cover position.""" |
| 106 |
value = kwargs[ATTR_POSITION] |
| 107 |
await self._feature.async_set_position(self._invert_position(value)) |
| 108 |
|
| 109 |
async def async_stop_cover(self, **kwargs): |
| 110 |
"""Stop the cover.""" |
| 111 |
await self._feature.async_stop() |
| 112 |
|
| 113 |
def _is_state(self, state_name): |
| 114 |
value = self.state |
| 115 |
return None if value is None else value == state_name |
| 116 |
|
| 117 |
def _invert_position(self, position): |
| 118 |
|
| 119 |
return None if position is None else 100 - position |
| 120 |
|
| 121 |
|
| 122 |
class CoverTest(DefaultBoxTest): |
| 123 |
"""Shared test helpers for Cover tests.""" |
| 124 |
|
| 125 |
DEVCLASS = "covers" |
| 126 |
ENTITY_CLASS = BleBoxCoverEntity |
| 127 |
|
| 128 |
|
| 129 |
def assert_state(self, entity, state): |
| 130 |
"""Assert that cover state is correct.""" |
| 131 |
assert entity.state == state |
| 132 |
|
| 133 |
opening, closing, closed = { |
| 134 |
None: [None, None, None], |
| 135 |
STATE_OPEN: [False, False, False], |
| 136 |
STATE_OPENING: [True, False, False], |
| 137 |
STATE_CLOSING: [False, True, False], |
| 138 |
STATE_CLOSED: [False, False, True], |
| 139 |
}[state] |
| 140 |
|
| 141 |
assert entity.is_opening is opening |
| 142 |
assert entity.is_closing is closing |
| 143 |
assert entity.is_closed is closed |
| 144 |
|
| 145 |
|
| 146 |
class TestShutterAttributes: |
| 147 |
"""Unit tests for Shutter attribute properties based on control type.""" |
| 148 |
|
| 149 |
def test_is_calibrated_false(self): |
| 150 |
assert Shutter(1, False).is_calibrated is False |
| 151 |
|
| 152 |
def test_is_calibrated_true(self): |
| 153 |
assert Shutter(1, True).is_calibrated is True |
| 154 |
|
| 155 |
@pytest.mark.parametrize( |
| 156 |
"control_type", |
| 157 |
[ |
| 158 |
ShutterBoxControlType.TILT_SHUTTER_90, |
| 159 |
ShutterBoxControlType.TILT_SHUTTER_180, |
| 160 |
ShutterBoxControlType.TILT_ONLY, |
| 161 |
ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY, |
| 162 |
ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING, |
| 163 |
], |
| 164 |
) |
| 165 |
def test_has_tilt_true(self, control_type): |
| 166 |
assert Shutter(control_type).has_tilt is True |
| 167 |
|
| 168 |
@pytest.mark.parametrize( |
| 169 |
"control_type", |
| 170 |
[ |
| 171 |
ShutterBoxControlType.SEGMENTED_SHUTTER, |
| 172 |
ShutterBoxControlType.NO_CALIBRATION, |
| 173 |
ShutterBoxControlType.WINDOW_OPENER, |
| 174 |
ShutterBoxControlType.PERGOLA_ROOF, |
| 175 |
ShutterBoxControlType.CURTAIN, |
| 176 |
], |
| 177 |
) |
| 178 |
def test_has_tilt_false(self, control_type): |
| 179 |
assert Shutter(control_type).has_tilt is False |
| 180 |
|
| 181 |
@pytest.mark.parametrize( |
| 182 |
"control_type", |
| 183 |
[ |
| 184 |
ShutterBoxControlType.TILT_ONLY, |
| 185 |
ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY, |
| 186 |
], |
| 187 |
) |
| 188 |
def test_tilt_only_true(self, control_type): |
| 189 |
assert Shutter(control_type).tilt_only is True |
| 190 |
|
| 191 |
@pytest.mark.parametrize( |
| 192 |
"control_type", |
| 193 |
[ |
| 194 |
ShutterBoxControlType.TILT_SHUTTER_90, |
| 195 |
ShutterBoxControlType.TILT_SHUTTER_180, |
| 196 |
ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING, |
| 197 |
ShutterBoxControlType.SEGMENTED_SHUTTER, |
| 198 |
], |
| 199 |
) |
| 200 |
def test_tilt_only_false(self, control_type): |
| 201 |
assert Shutter(control_type).tilt_only is False |
| 202 |
|
| 203 |
def test_is_tilt_180_true(self): |
| 204 |
assert Shutter(ShutterBoxControlType.TILT_SHUTTER_180).is_tilt_180 is True |
| 205 |
|
| 206 |
@pytest.mark.parametrize( |
| 207 |
"control_type", |
| 208 |
[ |
| 209 |
ShutterBoxControlType.TILT_SHUTTER_90, |
| 210 |
ShutterBoxControlType.TILT_ONLY, |
| 211 |
ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING, |
| 212 |
], |
| 213 |
) |
| 214 |
def test_is_tilt_180_false(self, control_type): |
| 215 |
assert Shutter(control_type).is_tilt_180 is False |
| 216 |
|
| 217 |
@pytest.mark.parametrize( |
| 218 |
"control_type", |
| 219 |
[ |
| 220 |
ShutterBoxControlType.NO_CALIBRATION, |
| 221 |
ShutterBoxControlType.CURTAIN, |
| 222 |
ShutterBoxControlType.TILT_ONLY, |
| 223 |
ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY, |
| 224 |
ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING, |
| 225 |
], |
| 226 |
) |
| 227 |
def test_is_slider_false_by_control_type(self, control_type): |
| 228 |
assert Shutter(control_type, is_calibrated=True).is_slider is False |
| 229 |
|
| 230 |
@pytest.mark.parametrize( |
| 231 |
"control_type", |
| 232 |
[ |
| 233 |
ShutterBoxControlType.TILT_SHUTTER_90, |
| 234 |
ShutterBoxControlType.TILT_SHUTTER_180, |
| 235 |
ShutterBoxControlType.PERGOLA_ROOF, |
| 236 |
ShutterBoxControlType.SEGMENTED_SHUTTER, |
| 237 |
], |
| 238 |
) |
| 239 |
def test_is_slider_false_when_not_calibrated(self, control_type): |
| 240 |
assert Shutter(control_type, is_calibrated=False).is_slider is False |
| 241 |
|
| 242 |
@pytest.mark.parametrize( |
| 243 |
"control_type", |
| 244 |
[ |
| 245 |
ShutterBoxControlType.TILT_SHUTTER_90, |
| 246 |
ShutterBoxControlType.TILT_SHUTTER_180, |
| 247 |
ShutterBoxControlType.PERGOLA_ROOF, |
| 248 |
ShutterBoxControlType.SEGMENTED_SHUTTER, |
| 249 |
], |
| 250 |
) |
| 251 |
def test_is_slider_true(self, control_type): |
| 252 |
assert Shutter(control_type, is_calibrated=True).is_slider is True |
| 253 |
|
| 254 |
@pytest.mark.parametrize( |
| 255 |
"control_type, expected", |
| 256 |
[ |
| 257 |
(ShutterBoxControlType.ROOF_SEGMENTED_SHUTTER, UnifiedCoverType.SHUTTER), |
| 258 |
(ShutterBoxControlType.SERVOMOTOR, UnifiedCoverType.SHUTTER), |
| 259 |
(ShutterBoxControlType.CURTAIN_ONE_SIDED, UnifiedCoverType.CURTAIN), |
| 260 |
(ShutterBoxControlType.CURTAIN_TWO_SIDED, UnifiedCoverType.CURTAIN), |
| 261 |
(ShutterBoxControlType.TILT_ONLY, UnifiedCoverType.SHUTTER), |
| 262 |
(ShutterBoxControlType.PERGOLA_ROOF, UnifiedCoverType.AWNING), |
| 263 |
(ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY, UnifiedCoverType.AWNING), |
| 264 |
(ShutterBoxControlType.TILT_SHUTTER_180, UnifiedCoverType.SHUTTER), |
| 265 |
( |
| 266 |
ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING, |
| 267 |
UnifiedCoverType.SHUTTER, |
| 268 |
), |
| 269 |
], |
| 270 |
) |
| 271 |
def test_read_cover_type_new_types(self, control_type, expected): |
| 272 |
assert Shutter(control_type).read_cover_type(None, None, None) == expected |
| 273 |
|
| 274 |
|
| 275 |
class TestShutter(CoverTest): |
| 276 |
"""Tests for cover devices representing a BleBox ShutterBox.""" |
| 277 |
|
| 278 |
DEV_INFO_PATH = "api/shutter/state" |
| 279 |
|
| 280 |
DEVICE_INFO = json.loads(""" |
| 281 |
{ |
| 282 |
"device": { |
| 283 |
"deviceName": "My shutter 1", |
| 284 |
"type": "shutterBox", |
| 285 |
"fv": "0.147", |
| 286 |
"hv": "0.7", |
| 287 |
"apiLevel": "20180604", |
| 288 |
"id": "2bee34e750b8", |
| 289 |
"ip": "172.0.0.1" |
| 290 |
} |
| 291 |
} |
| 292 |
""") |
| 293 |
|
| 294 |
DEVICE_EXTENDED_INFO_PATH = "/api/shutter/extended/state" |
| 295 |
DEVICE_EXTENDED_INFO = { |
| 296 |
"shutter": { |
| 297 |
"state": 2, |
| 298 |
"currentPos": {"position": 50, "tilt": 50}, |
| 299 |
"desiredPos": {"position": 50, "tilt": 50}, |
| 300 |
"favPos": {"position": 50, "tilt": 50}, |
| 301 |
"controlType": 1, |
| 302 |
"calibrationParameters": { |
| 303 |
"isCalibrated": 1, |
| 304 |
}, |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date())) |
| 309 |
DEVICE_INFO_LATEST = jmerge( |
| 310 |
DEVICE_INFO, patch_version(get_latest_api_level("shutterBox")) |
| 311 |
) |
| 312 |
DEVICE_INFO_UNSUPPORTED = jmerge(DEVICE_INFO, patch_version(20180603)) |
| 313 |
|
| 314 |
DEVICE_INFO_UNSPECIFIED_API = json.loads(""" |
| 315 |
{ |
| 316 |
"device": { |
| 317 |
"deviceName": "My shutter 1", |
| 318 |
"type": "shutterBox", |
| 319 |
"fv": "0.147", |
| 320 |
"hv": "0.7", |
| 321 |
"id": "2bee34e750b8", |
| 322 |
"ip": "172.0.0.1" |
| 323 |
} |
| 324 |
} |
| 325 |
""") |
| 326 |
|
| 327 |
STATE_DEFAULT = json.loads(""" |
| 328 |
{ |
| 329 |
"shutter": { |
| 330 |
"state": 2, |
| 331 |
"currentPos": { |
| 332 |
"position": 34, |
| 333 |
"tilt": 3 |
| 334 |
}, |
| 335 |
"desiredPos": { |
| 336 |
"position": 78, |
| 337 |
"tilt": 97 |
| 338 |
}, |
| 339 |
"favPos": { |
| 340 |
"position": 13, |
| 341 |
"tilt": 17 |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
""") |
| 346 |
|
| 347 |
def patch_state(state, current, desired): |
| 348 |
"""Generate a patch for a JSON state fixture.""" |
| 349 |
return f""" |
| 350 |
{{ |
| 351 |
"shutter": {{ |
| 352 |
"state": {state}, |
| 353 |
"currentPos": {{ "position": {current} }}, |
| 354 |
"desiredPos": {{ "position": {desired} }} |
| 355 |
}} |
| 356 |
}} |
| 357 |
""" |
| 358 |
|
| 359 |
STATE_CLOSING = jmerge(STATE_DEFAULT, patch_state(0, 78, 100)) |
| 360 |
STATE_CLOSED = jmerge(STATE_DEFAULT, patch_state(3, 100, 100)) |
| 361 |
STATE_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 34, 0)) |
| 362 |
STATE_MINIMALLY_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 97, 100)) |
| 363 |
STATE_STOPPED = jmerge(STATE_DEFAULT, patch_state(2, 34, 100)) |
| 364 |
STATE_UNKNOWN = jmerge(STATE_DEFAULT, patch_state(2, 34, -1)) |
| 365 |
|
| 366 |
async def test_init(self, aioclient_mock): |
| 367 |
"""Test cover default state.""" |
| 368 |
|
| 369 |
await self.allow_get_info(aioclient_mock) |
| 370 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 371 |
|
| 372 |
assert entity.name == "My shutter 1 (shutterBox#position)" |
| 373 |
assert entity.unique_id == "BleBox-shutterBox-2bee34e750b8-position" |
| 374 |
|
| 375 |
assert entity.device_class == DEVICE_CLASS_SHUTTER |
| 376 |
|
| 377 |
assert entity.supported_features & SUPPORT_OPEN |
| 378 |
assert entity.supported_features & SUPPORT_CLOSE |
| 379 |
assert entity.supported_features & SUPPORT_STOP |
| 380 |
|
| 381 |
assert entity._feature.is_calibrated is True |
| 382 |
assert entity.supported_features & SUPPORT_SET_POSITION |
| 383 |
assert entity.current_cover_position == 50 |
| 384 |
assert entity._feature.is_position_inverted is True |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
self.assert_state(entity, STATE_OPEN) |
| 391 |
|
| 392 |
async def test_init_not_calibrated(self, aioclient_mock): |
| 393 |
"""Test that is_calibrated is False when device reports isCalibrated=0.""" |
| 394 |
original = self.DEVICE_EXTENDED_INFO |
| 395 |
self.DEVICE_EXTENDED_INFO = jmerge( |
| 396 |
original, |
| 397 |
'{"shutter": {"calibrationParameters": {"isCalibrated": 0}}}', |
| 398 |
) |
| 399 |
try: |
| 400 |
await self.allow_get_info(aioclient_mock) |
| 401 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 402 |
assert entity._feature.is_calibrated is False |
| 403 |
finally: |
| 404 |
self.DEVICE_EXTENDED_INFO = original |
| 405 |
|
| 406 |
async def test_device_info(self, aioclient_mock): |
| 407 |
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO) |
| 408 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 409 |
assert entity.device_info["name"] == "My shutter 1" |
| 410 |
assert entity.device_info["mac"] == "2bee34e750b8" |
| 411 |
assert entity.device_info["manufacturer"] == "BleBox" |
| 412 |
assert entity.device_info["model"] == "shutterBox" |
| 413 |
assert entity.device_info["sw_version"] == "0.147" |
| 414 |
|
| 415 |
async def test_update(self, aioclient_mock): |
| 416 |
"""Test cover updating.""" |
| 417 |
|
| 418 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 419 |
|
| 420 |
assert entity.current_cover_position == 22 |
| 421 |
self.assert_state(entity, STATE_OPEN) |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
async def test_open(self, aioclient_mock): |
| 427 |
"""Test cover opening.""" |
| 428 |
|
| 429 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 430 |
self.assert_state(entity, STATE_CLOSED) |
| 431 |
self.allow_get(aioclient_mock, "/s/u", self.STATE_OPENING) |
| 432 |
await entity.async_open_cover() |
| 433 |
self.assert_state(entity, STATE_OPENING) |
| 434 |
|
| 435 |
async def test_close(self, aioclient_mock): |
| 436 |
"""Test cover closing.""" |
| 437 |
|
| 438 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 439 |
self.assert_state(entity, STATE_OPEN) |
| 440 |
self.allow_get(aioclient_mock, "/s/d", self.STATE_CLOSING) |
| 441 |
await entity.async_close_cover() |
| 442 |
self.assert_state(entity, STATE_CLOSING) |
| 443 |
|
| 444 |
async def test_set_position(self, aioclient_mock): |
| 445 |
"""Test cover position setting.""" |
| 446 |
|
| 447 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 448 |
self.assert_state(entity, STATE_CLOSED) |
| 449 |
self.allow_get(aioclient_mock, "/s/p/99", self.STATE_MINIMALLY_OPENING) |
| 450 |
await entity.async_set_cover_position(**{ATTR_POSITION: 1}) |
| 451 |
self.assert_state(entity, STATE_OPENING) |
| 452 |
|
| 453 |
async def test_stop(self, aioclient_mock): |
| 454 |
"""Test cover stopping.""" |
| 455 |
|
| 456 |
entity = await self.updated(aioclient_mock, self.STATE_OPENING) |
| 457 |
self.assert_state(entity, STATE_OPENING) |
| 458 |
self.allow_get(aioclient_mock, "/s/s", self.STATE_STOPPED) |
| 459 |
await entity.async_stop_cover() |
| 460 |
self.assert_state(entity, STATE_OPEN) |
| 461 |
|
| 462 |
async def test_unkown_position(self, aioclient_mock): |
| 463 |
"""Test handling cover at unknown position.""" |
| 464 |
entity = await self.updated(aioclient_mock, self.STATE_UNKNOWN) |
| 465 |
self.assert_state(entity, STATE_OPEN) |
| 466 |
|
| 467 |
|
| 468 |
class TestGateBox(CoverTest): |
| 469 |
"""Tests for cover devices representing a BleBox gateBox.""" |
| 470 |
|
| 471 |
DEV_INFO_PATH = "api/gate/state" |
| 472 |
|
| 473 |
DEVICE_INFO = json.loads(""" |
| 474 |
{ |
| 475 |
"deviceName": "My gate 1", |
| 476 |
"type": "gateBox", |
| 477 |
"fv": "0.176", |
| 478 |
"hv": "0.6", |
| 479 |
"id": "1afe34db9437", |
| 480 |
"ip": "192.168.1.11" |
| 481 |
} |
| 482 |
""") |
| 483 |
|
| 484 |
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, f'{{ "apiLevel":"{future_date()}" }}') |
| 485 |
DEVICE_INFO_LATEST = jmerge( |
| 486 |
DEVICE_INFO, f'{{ "apiLevel":"{get_latest_api_level("gateBox")}" }}' |
| 487 |
) |
| 488 |
DEVICE_INFO_UNSUPPORTED = DEVICE_INFO |
| 489 |
|
| 490 |
DEVICE_INFO_UNSPECIFIED_API = None |
| 491 |
|
| 492 |
STATE_DEFAULT = json.loads(""" |
| 493 |
{ |
| 494 |
"currentPos": 50, |
| 495 |
"desiredPos": 50, |
| 496 |
"extraButtonType": 1, |
| 497 |
"extraButtonRelayNumber": 1, |
| 498 |
"extraButtonPulseTimeMs": 800, |
| 499 |
"extraButtonInvert": 1, |
| 500 |
"gateType": 0, |
| 501 |
"gateRelayNumber": 0, |
| 502 |
"gatePulseTimeMs": 800, |
| 503 |
"gateInvert": 0, |
| 504 |
"inputsType": 1, |
| 505 |
"openLimitSwitchInputNumber": 0, |
| 506 |
"closeLimitSwitchInputNumber": 1 |
| 507 |
} |
| 508 |
""") |
| 509 |
|
| 510 |
STATE_CLOSED = jmerge(STATE_DEFAULT, '{ "currentPos": 0, "desiredPos": 0 }') |
| 511 |
STATE_OPENING = jmerge(STATE_DEFAULT, '{ "currentPos": 50, "desiredPos": 100 }') |
| 512 |
STATE_CLOSING = jmerge(STATE_DEFAULT, '{ "currentPos": 50, "desiredPos": 0 }') |
| 513 |
STATE_STOPPED = jmerge(STATE_DEFAULT, '{ "currentPos": 50, "desiredPos": 50 }') |
| 514 |
STATE_FULLY_OPENED = jmerge( |
| 515 |
STATE_DEFAULT, '{ "currentPos": 100, "desiredPos": 100 }' |
| 516 |
) |
| 517 |
|
| 518 |
STATE_OPENING_NO_STOP = jmerge(STATE_OPENING, '{ "extraButtonType": 3}') |
| 519 |
STATE_UNKNOWN = jmerge(STATE_DEFAULT, '{ "currentPos": -1, "desiredPos": 50 }') |
| 520 |
|
| 521 |
async def test_init(self, aioclient_mock): |
| 522 |
"""Test cover default state.""" |
| 523 |
|
| 524 |
await self.allow_get_info(aioclient_mock) |
| 525 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 526 |
|
| 527 |
assert entity.name == "My gate 1 (gateBox#position)" |
| 528 |
assert entity.unique_id == "BleBox-gateBox-1afe34db9437-position" |
| 529 |
assert entity.device_class == DEVICE_CLASS_DOOR |
| 530 |
assert entity.supported_features & SUPPORT_OPEN |
| 531 |
assert entity.supported_features & SUPPORT_CLOSE |
| 532 |
|
| 533 |
|
| 534 |
assert not entity.supported_features & SUPPORT_STOP |
| 535 |
|
| 536 |
assert not entity.supported_features & SUPPORT_SET_POSITION |
| 537 |
assert entity.current_cover_position is None |
| 538 |
assert entity._feature.is_position_inverted is False |
| 539 |
self.assert_state(entity, None) |
| 540 |
|
| 541 |
async def test_device_info(self, aioclient_mock): |
| 542 |
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO) |
| 543 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 544 |
assert entity.device_info["name"] == "My gate 1" |
| 545 |
assert entity.device_info["mac"] == "1afe34db9437" |
| 546 |
assert entity.device_info["manufacturer"] == "BleBox" |
| 547 |
assert entity.device_info["model"] == "gateBox" |
| 548 |
assert entity.device_info["sw_version"] == "0.176" |
| 549 |
|
| 550 |
async def test_update(self, aioclient_mock): |
| 551 |
"""Test cover updating.""" |
| 552 |
|
| 553 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 554 |
|
| 555 |
assert entity.current_cover_position == 50 |
| 556 |
self.assert_state(entity, STATE_OPEN) |
| 557 |
|
| 558 |
async def test_open(self, aioclient_mock): |
| 559 |
"""Test cover opening.""" |
| 560 |
|
| 561 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 562 |
assert entity.state == STATE_CLOSED |
| 563 |
self.assert_state(entity, STATE_CLOSED) |
| 564 |
self.allow_get(aioclient_mock, "/s/p", self.STATE_OPENING) |
| 565 |
await entity.async_open_cover() |
| 566 |
self.assert_state(entity, STATE_OPENING) |
| 567 |
|
| 568 |
async def test_fully_opened(self, aioclient_mock): |
| 569 |
entity = await self.updated(aioclient_mock, self.STATE_FULLY_OPENED) |
| 570 |
assert entity.state == STATE_OPEN |
| 571 |
|
| 572 |
async def test_close(self, aioclient_mock): |
| 573 |
"""Test cover closing.""" |
| 574 |
|
| 575 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 576 |
self.assert_state(entity, STATE_OPEN) |
| 577 |
self.allow_get(aioclient_mock, "/s/p", self.STATE_CLOSING) |
| 578 |
await entity.async_close_cover() |
| 579 |
self.assert_state(entity, STATE_CLOSING) |
| 580 |
|
| 581 |
async def test_closed(self, aioclient_mock): |
| 582 |
"""Test cover closed state.""" |
| 583 |
|
| 584 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 585 |
self.assert_state(entity, STATE_CLOSED) |
| 586 |
|
| 587 |
async def test_stop(self, aioclient_mock): |
| 588 |
"""Test cover stopping.""" |
| 589 |
|
| 590 |
entity = await self.updated(aioclient_mock, self.STATE_OPENING) |
| 591 |
self.assert_state(entity, STATE_OPENING) |
| 592 |
self.allow_get(aioclient_mock, "/s/s", self.STATE_STOPPED) |
| 593 |
await entity.async_stop_cover() |
| 594 |
self.assert_state(entity, STATE_OPEN) |
| 595 |
|
| 596 |
async def test_with_stop(self, aioclient_mock): |
| 597 |
"""Test stop capability is available.""" |
| 598 |
|
| 599 |
entity = await self.updated(aioclient_mock, self.STATE_OPENING) |
| 600 |
assert entity.supported_features & SUPPORT_STOP |
| 601 |
|
| 602 |
async def test_with_no_stop(self, aioclient_mock): |
| 603 |
"""Test stop capability is not available.""" |
| 604 |
|
| 605 |
entity = await self.updated(aioclient_mock, self.STATE_OPENING_NO_STOP) |
| 606 |
assert not entity.supported_features & SUPPORT_STOP |
| 607 |
|
| 608 |
async def test_stop_with_no_stop(self, aioclient_mock): |
| 609 |
"""Test stop capability is not available.""" |
| 610 |
|
| 611 |
entity = await self.updated(aioclient_mock, self.STATE_OPENING_NO_STOP) |
| 612 |
|
| 613 |
with pytest.raises( |
| 614 |
error.MisconfiguredDevice, match=r"second button not configured as 'stop'" |
| 615 |
): |
| 616 |
await entity.async_stop_cover() |
| 617 |
|
| 618 |
async def test_set_position(self, aioclient_mock): |
| 619 |
"""Test cover position setting.""" |
| 620 |
|
| 621 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 622 |
with pytest.raises(NotImplementedError): |
| 623 |
await entity.async_set_cover_position(**{ATTR_POSITION: 1}) |
| 624 |
|
| 625 |
async def test_unkown_position(self, aioclient_mock): |
| 626 |
"""Test handling cover at unknown position.""" |
| 627 |
entity = await self.updated(aioclient_mock, self.STATE_UNKNOWN) |
| 628 |
self.assert_state(entity, None) |
| 629 |
|
| 630 |
|
| 631 |
class TestGateBoxB(CoverTest): |
| 632 |
"""Tests for cover devices representing a BleBox gateBoxB subgroup.""" |
| 633 |
|
| 634 |
DEV_INFO_PATH = "state/extended" |
| 635 |
|
| 636 |
DEVICE_INFO = json.loads(""" |
| 637 |
{ |
| 638 |
"device": { |
| 639 |
"deviceName":"My gateBox 1", |
| 640 |
"type":"gateBox", |
| 641 |
"product":"gateBox", |
| 642 |
"hv":"9.1d", |
| 643 |
"fv":"0.1010", |
| 644 |
"universe":0, |
| 645 |
"apiLevel":"20200831", |
| 646 |
"id":"1afe34d27e4f", |
| 647 |
"ip":"192.168.4.1", |
| 648 |
"availableFv":null |
| 649 |
} |
| 650 |
} |
| 651 |
""") |
| 652 |
|
| 653 |
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date())) |
| 654 |
DEVICE_INFO_LATEST = jmerge( |
| 655 |
DEVICE_INFO, patch_version(get_latest_api_level("gateBox")) |
| 656 |
) |
| 657 |
DEVICE_INFO_UNSUPPORTED = DEVICE_INFO |
| 658 |
|
| 659 |
DEVICE_INFO_UNSPECIFIED_API = None |
| 660 |
|
| 661 |
STATE_DEFAULT = json.loads(""" |
| 662 |
{ |
| 663 |
"gate": { |
| 664 |
"currentPos": 0, |
| 665 |
"openCloseMode": 0, |
| 666 |
"gateType": 1, |
| 667 |
"gatePulseTimeMs": 1500, |
| 668 |
"gateOutputState": 0, |
| 669 |
"extraButtonType": 1, |
| 670 |
"extraButtonPulseTimeMs": 1500, |
| 671 |
"extraButtonOutputState": 0, |
| 672 |
"inputsType": 0 |
| 673 |
} |
| 674 |
} |
| 675 |
""") |
| 676 |
|
| 677 |
STATE_CLOSED = STATE_DEFAULT |
| 678 |
STATE_STOPPED = jmerge(STATE_DEFAULT, '{"gate": {"currentPos": 50 }}') |
| 679 |
STATE_FULLY_OPENED = jmerge(STATE_DEFAULT, '{"gate": {"currentPos": 100 }}') |
| 680 |
|
| 681 |
STATE_UNKNOWN = json.loads(""" |
| 682 |
{ |
| 683 |
"gate": { |
| 684 |
"currentPos": -1, |
| 685 |
"openCloseMode": 0, |
| 686 |
"gateType": 1, |
| 687 |
"gatePulseTimeMs": 1500, |
| 688 |
"gateOutputState": 0, |
| 689 |
"extraButtonType": 1, |
| 690 |
"extraButtonPulseTimeMs": 1500, |
| 691 |
"extraButtonOutputState": 0, |
| 692 |
"inputsType": 0 |
| 693 |
} |
| 694 |
} |
| 695 |
""") |
| 696 |
|
| 697 |
async def test_init(self, aioclient_mock): |
| 698 |
"""Test cover default state.""" |
| 699 |
|
| 700 |
await self.allow_get_info(aioclient_mock) |
| 701 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 702 |
|
| 703 |
assert entity.name == "My gateBox 1 (gateBox#position)" |
| 704 |
assert entity.unique_id == "BleBox-gateBox-1afe34d27e4f-position" |
| 705 |
assert entity.device_class == DEVICE_CLASS_DOOR |
| 706 |
assert entity.supported_features & SUPPORT_OPEN |
| 707 |
assert entity.supported_features & SUPPORT_CLOSE |
| 708 |
assert entity.current_cover_position is None |
| 709 |
assert entity._feature.is_position_inverted is False |
| 710 |
self.assert_state(entity, None) |
| 711 |
|
| 712 |
async def test_device_info(self, aioclient_mock): |
| 713 |
"""Test device info state.""" |
| 714 |
|
| 715 |
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO) |
| 716 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 717 |
|
| 718 |
assert entity.device_info["name"] == "My gateBox 1" |
| 719 |
assert entity.device_info["mac"] == "1afe34d27e4f" |
| 720 |
assert entity.device_info["manufacturer"] == "BleBox" |
| 721 |
assert entity.device_info["model"] == "gateBox" |
| 722 |
assert entity.device_info["sw_version"] == "0.1010" |
| 723 |
|
| 724 |
async def test_fully_opened(self, aioclient_mock): |
| 725 |
"""Test cover fully opened.""" |
| 726 |
|
| 727 |
entity = await self.updated(aioclient_mock, self.STATE_FULLY_OPENED) |
| 728 |
assert entity.state == STATE_OPEN |
| 729 |
|
| 730 |
async def test_stop(self, aioclient_mock): |
| 731 |
"""Test cover stopped.""" |
| 732 |
|
| 733 |
entity = await self.updated(aioclient_mock, self.STATE_STOPPED) |
| 734 |
self.assert_state(entity, STATE_OPEN) |
| 735 |
assert entity.supported_features & SUPPORT_STOP |
| 736 |
|
| 737 |
async def test_closed(self, aioclient_mock): |
| 738 |
"""Test cover closed.""" |
| 739 |
|
| 740 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 741 |
self.assert_state(entity, STATE_CLOSED) |
| 742 |
|
| 743 |
async def test_unkown_position(self, aioclient_mock): |
| 744 |
"""Test handling cover at unknown position.""" |
| 745 |
entity = await self.updated(aioclient_mock, self.STATE_UNKNOWN) |
| 746 |
self.assert_state(entity, None) |
| 747 |
|
| 748 |
|
| 749 |
class TestGateBoxBSecondOutput(CoverTest): |
| 750 |
"""Tests for a GateBoxB whose extra button output is exposed as a button.""" |
| 751 |
|
| 752 |
DEV_INFO_PATH = "state/extended" |
| 753 |
|
| 754 |
DEVICE_INFO = json.loads(""" |
| 755 |
{ |
| 756 |
"device": { |
| 757 |
"deviceName":"My gateBox 1", |
| 758 |
"type":"gateBox", |
| 759 |
"product":"gateBox", |
| 760 |
"hv":"9.1d", |
| 761 |
"fv":"0.1010", |
| 762 |
"universe":0, |
| 763 |
"apiLevel":"20230102", |
| 764 |
"id":"1afe34d27e4f", |
| 765 |
"ip":"192.168.4.1", |
| 766 |
"availableFv":null |
| 767 |
} |
| 768 |
} |
| 769 |
""") |
| 770 |
|
| 771 |
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date())) |
| 772 |
DEVICE_INFO_LATEST = jmerge( |
| 773 |
DEVICE_INFO, patch_version(get_latest_api_level("gateBox")) |
| 774 |
) |
| 775 |
DEVICE_INFO_UNSUPPORTED = DEVICE_INFO |
| 776 |
|
| 777 |
DEVICE_INFO_UNSPECIFIED_API = None |
| 778 |
|
| 779 |
STATE_DEFAULT = json.loads(""" |
| 780 |
{ |
| 781 |
"gate": { |
| 782 |
"currentPos": 0, |
| 783 |
"openCloseMode": 0, |
| 784 |
"gateType": 1, |
| 785 |
"gatePulseTimeMs": 1500, |
| 786 |
"gateOutputState": 0, |
| 787 |
"extraButtonType": 2, |
| 788 |
"extraButtonPulseTimeMs": 1500, |
| 789 |
"extraButtonOutputState": 0, |
| 790 |
"inputsType": 0 |
| 791 |
} |
| 792 |
} |
| 793 |
""") |
| 794 |
|
| 795 |
DEVICE_EXTENDED_INFO_PATH = "/state/extended" |
| 796 |
DEVICE_EXTENDED_INFO = STATE_DEFAULT |
| 797 |
|
| 798 |
async def test_init(self, aioclient_mock): |
| 799 |
"""Test cover default state.""" |
| 800 |
|
| 801 |
await self.allow_get_info(aioclient_mock) |
| 802 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 803 |
|
| 804 |
assert entity.name == "My gateBox 1 (gateBox#position)" |
| 805 |
assert entity.unique_id == "BleBox-gateBox-1afe34d27e4f-position" |
| 806 |
assert entity.device_class == DEVICE_CLASS_DOOR |
| 807 |
assert entity.supported_features & SUPPORT_OPEN |
| 808 |
assert entity.supported_features & SUPPORT_CLOSE |
| 809 |
|
| 810 |
async def test_second_output_claims_stop(self, aioclient_mock): |
| 811 |
"""Test that a second_output button removes the cover's stop action. |
| 812 |
|
| 813 |
Both would issue the same secondary command. |
| 814 |
""" |
| 815 |
|
| 816 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 817 |
assert not entity.supported_features & SUPPORT_STOP |
| 818 |
|
| 819 |
|
| 820 |
class TestGateController(CoverTest): |
| 821 |
"""Tests for cover devices representing a BleBox gateController.""" |
| 822 |
|
| 823 |
DEV_INFO_PATH = "api/gatecontroller/state" |
| 824 |
|
| 825 |
DEVICE_INFO = json.loads(""" |
| 826 |
{ |
| 827 |
"device": { |
| 828 |
"deviceName": "My gate controller 1", |
| 829 |
"type": "gateController", |
| 830 |
"apiLevel": "20180604", |
| 831 |
"fv": "1.390", |
| 832 |
"hv": "custom.2.6", |
| 833 |
"id": "0ff2ffaafe30db9437", |
| 834 |
"ip": "192.168.1.11" |
| 835 |
} |
| 836 |
} |
| 837 |
""") |
| 838 |
|
| 839 |
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date())) |
| 840 |
DEVICE_INFO_LATEST = jmerge( |
| 841 |
DEVICE_INFO, patch_version(get_latest_api_level("gateController")) |
| 842 |
) |
| 843 |
DEVICE_INFO_UNSUPPORTED = jmerge(DEVICE_INFO, patch_version(20180603)) |
| 844 |
|
| 845 |
|
| 846 |
DEVICE_INFO_UNSPECIFIED_API = json.loads(""" |
| 847 |
{ |
| 848 |
"device": { |
| 849 |
"deviceName": "My gate controller 1", |
| 850 |
"type": "gateController", |
| 851 |
"fv": "0.981", |
| 852 |
"hv": "custom.2.6", |
| 853 |
"id": "0ff2ffaafe30db9437", |
| 854 |
"ip": "192.168.1.11" |
| 855 |
} |
| 856 |
} |
| 857 |
""") |
| 858 |
|
| 859 |
STATE_DEFAULT = json.loads(""" |
| 860 |
{ |
| 861 |
"gateController": { |
| 862 |
"state": 2, |
| 863 |
"safety": { |
| 864 |
"eventReason": 0, |
| 865 |
"triggered": [ 0 ] |
| 866 |
}, |
| 867 |
"currentPos": { "positions": [ 31 ] }, |
| 868 |
"desiredPos": { "positions": [ 29 ] } |
| 869 |
} |
| 870 |
} |
| 871 |
""") |
| 872 |
|
| 873 |
def patch_state(state, current, desired): |
| 874 |
"""Generate a patch for a JSON state fixture.""" |
| 875 |
return f""" |
| 876 |
{{ |
| 877 |
"gateController": {{ |
| 878 |
"state": {state}, |
| 879 |
"currentPos": {{ "positions": [ {current} ] }}, |
| 880 |
"desiredPos": {{ "positions": [ {desired} ] }} |
| 881 |
}} |
| 882 |
}} |
| 883 |
""" |
| 884 |
|
| 885 |
STATE_CLOSED = jmerge(STATE_DEFAULT, patch_state(3, 100, 100)) |
| 886 |
STATE_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 34, 0)) |
| 887 |
STATE_CLOSING = jmerge(STATE_DEFAULT, patch_state(0, 78, 100)) |
| 888 |
STATE_MINIMALLY_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 97, 100)) |
| 889 |
STATE_STOPPED = jmerge(STATE_DEFAULT, patch_state(2, 34, 100)) |
| 890 |
|
| 891 |
async def test_init(self, aioclient_mock): |
| 892 |
"""Test cover default state.""" |
| 893 |
|
| 894 |
await self.allow_get_info(aioclient_mock) |
| 895 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 896 |
|
| 897 |
assert entity.name == "My gate controller 1 (gateController#position)" |
| 898 |
assert entity.unique_id == "BleBox-gateController-0ff2ffaafe30db9437-position" |
| 899 |
|
| 900 |
assert entity.device_class == DEVICE_CLASS_DOOR |
| 901 |
|
| 902 |
assert entity.supported_features & SUPPORT_OPEN |
| 903 |
assert entity.supported_features & SUPPORT_CLOSE |
| 904 |
assert entity.supported_features & SUPPORT_STOP |
| 905 |
|
| 906 |
assert entity.supported_features & SUPPORT_SET_POSITION |
| 907 |
assert entity.current_cover_position is None |
| 908 |
assert entity._feature.is_position_inverted is True |
| 909 |
self.assert_state(entity, None) |
| 910 |
|
| 911 |
async def test_device_info(self, aioclient_mock): |
| 912 |
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO) |
| 913 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 914 |
assert entity.device_info["name"] == "My gate controller 1" |
| 915 |
assert entity.device_info["mac"] == "0ff2ffaafe30db9437" |
| 916 |
assert entity.device_info["manufacturer"] == "BleBox" |
| 917 |
assert entity.device_info["model"] == "gateController" |
| 918 |
assert entity.device_info["sw_version"] == "1.390" |
| 919 |
|
| 920 |
async def test_update(self, aioclient_mock): |
| 921 |
"""Test cover updating.""" |
| 922 |
|
| 923 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 924 |
|
| 925 |
assert entity.current_cover_position == 71 |
| 926 |
self.assert_state(entity, STATE_OPEN) |
| 927 |
|
| 928 |
async def test_open(self, aioclient_mock): |
| 929 |
"""Test cover opening.""" |
| 930 |
|
| 931 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 932 |
self.assert_state(entity, STATE_CLOSED) |
| 933 |
self.allow_get(aioclient_mock, "/s/o", self.STATE_OPENING) |
| 934 |
await entity.async_open_cover() |
| 935 |
self.assert_state(entity, STATE_OPENING) |
| 936 |
|
| 937 |
async def test_close(self, aioclient_mock): |
| 938 |
"""Test cover closing.""" |
| 939 |
|
| 940 |
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT) |
| 941 |
self.assert_state(entity, STATE_OPEN) |
| 942 |
self.allow_get(aioclient_mock, "/s/c", self.STATE_CLOSING) |
| 943 |
await entity.async_close_cover() |
| 944 |
self.assert_state(entity, STATE_CLOSING) |
| 945 |
|
| 946 |
async def test_set_position(self, aioclient_mock): |
| 947 |
"""Test cover position setting.""" |
| 948 |
|
| 949 |
entity = await self.updated(aioclient_mock, self.STATE_CLOSED) |
| 950 |
self.assert_state(entity, STATE_CLOSED) |
| 951 |
self.allow_get(aioclient_mock, "/s/p/99", self.STATE_MINIMALLY_OPENING) |
| 952 |
await entity.async_set_cover_position(**{ATTR_POSITION: 1}) |
| 953 |
self.assert_state(entity, STATE_OPENING) |
| 954 |
|
| 955 |
async def test_stop(self, aioclient_mock): |
| 956 |
"""Test cover stopping.""" |
| 957 |
|
| 958 |
entity = await self.updated(aioclient_mock, self.STATE_OPENING) |
| 959 |
self.assert_state(entity, STATE_OPENING) |
| 960 |
self.allow_get(aioclient_mock, "/s/s", self.STATE_STOPPED) |
| 961 |
await entity.async_stop_cover() |
| 962 |
self.assert_state(entity, STATE_OPEN) |