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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
|
"""BleBox cover entities tests."""
import json
import pytest
from blebox_uniapi.box_types import get_latest_api_level
from blebox_uniapi import error
from .conftest import CommonEntity, DefaultBoxTest, future_date, jmerge
# TODO: remove
ATTR_POSITION = "ATTR_POSITION"
DEVICE_CLASS_DOOR = "DEVICE_CLASS_DOOR"
DEVICE_CLASS_SHUTTER = "DEVICE_CLASS_SHUTTER"
STATE_CLOSED = "STATE_CLOSED"
STATE_CLOSING = "STATE_CLOSING"
STATE_OPEN = "STATE_OPEN"
STATE_OPENING = "STATE_OPENING"
SUPPORT_OPEN = 1
SUPPORT_CLOSE = 2
SUPPORT_SET_POSITION = 4
SUPPORT_STOP = 8
def patch_version(apiLevel):
"""Helper function for generate a patch for a JSON state fixture."""
return f"""{{ "device": {{ "apiLevel": {apiLevel} }} }}"""
class BleBoxCoverEntity(CommonEntity):
"""Representation of a BleBox cover feature."""
@property
def state(self):
"""Return the equivalent HA cover state."""
states = {
None: None,
0: STATE_CLOSING, # moving down
1: STATE_OPENING, # moving up
2: STATE_OPEN, # manually stopped
3: STATE_CLOSED, # lower limit
4: STATE_OPEN, # upper limit / open
# gateController
5: STATE_OPEN, # overload
6: STATE_OPEN, # motor failure
# 7: not used
8: STATE_OPEN, # safety stop
}
return states[self._feature.state]
@property
def device_class(self):
"""Return the device class."""
types = {
"shutter": DEVICE_CLASS_SHUTTER,
"gatebox": DEVICE_CLASS_DOOR,
# "gateboxB": DEVICE_CLASS_DOOR,
"gate": DEVICE_CLASS_DOOR,
}
return types[self._feature.device_class]
# TODO: does changing this at runtime really work?
@property
def supported_features(self):
"""Return the supported cover features."""
position = SUPPORT_SET_POSITION if self._feature.is_slider else 0
stop = SUPPORT_STOP if self._feature.has_stop else 0
return position | stop | SUPPORT_OPEN | SUPPORT_CLOSE
@property
def current_cover_position(self):
"""Return the current cover position."""
current = self._invert_position(self._feature.current)
return current if current else None
@property
def is_opening(self):
"""Return whether cover is opening."""
return self._is_state(STATE_OPENING)
@property
def is_closing(self):
"""Return whether cover is closing."""
return self._is_state(STATE_CLOSING)
@property
def is_closed(self):
"""Return whether cover is closed."""
return self._is_state(STATE_CLOSED)
async def async_open_cover(self, **kwargs):
"""Open the cover position."""
await self._feature.async_open()
async def async_close_cover(self, **kwargs):
"""Close the cover position."""
await self._feature.async_close()
async def async_set_cover_position(self, **kwargs):
"""Set the cover position."""
value = kwargs[ATTR_POSITION]
await self._feature.async_set_position(self._invert_position(value))
async def async_stop_cover(self, **kwargs):
"""Stop the cover."""
await self._feature.async_stop()
def _is_state(self, state_name):
value = self.state
return None if value is None else value == state_name
def _invert_position(self, position):
# NOTE: in BleBox, 100% means 'closed'
return None if position is None else 100 - position
class CoverTest(DefaultBoxTest):
"""Shared test helpers for Cover tests."""
DEVCLASS = "covers"
ENTITY_CLASS = BleBoxCoverEntity
# TODO: refactor more
def assert_state(self, entity, state):
"""Assert that cover state is correct."""
assert entity.state == state
opening, closing, closed = {
None: [None, None, None],
STATE_OPEN: [False, False, False],
STATE_OPENING: [True, False, False],
STATE_CLOSING: [False, True, False],
STATE_CLOSED: [False, False, True],
}[state]
assert entity.is_opening is opening
assert entity.is_closing is closing
assert entity.is_closed is closed
class TestShutter(CoverTest):
"""Tests for cover devices representing a BleBox ShutterBox."""
DEV_INFO_PATH = "api/shutter/state"
DEVICE_INFO = json.loads(
"""
{
"device": {
"deviceName": "My shutter 1",
"type": "shutterBox",
"fv": "0.147",
"hv": "0.7",
"apiLevel": "20180604",
"id": "2bee34e750b8",
"ip": "172.0.0.1"
}
}
"""
)
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date()))
DEVICE_INFO_LATEST = jmerge(
DEVICE_INFO, patch_version(get_latest_api_level("shutterBox"))
)
DEVICE_INFO_UNSUPPORTED = jmerge(DEVICE_INFO, patch_version(20180603))
DEVICE_INFO_UNSPECIFIED_API = json.loads(
"""
{
"device": {
"deviceName": "My shutter 1",
"type": "shutterBox",
"fv": "0.147",
"hv": "0.7",
"id": "2bee34e750b8",
"ip": "172.0.0.1"
}
}
"""
)
STATE_DEFAULT = json.loads(
"""
{
"shutter": {
"state": 2,
"currentPos": {
"position": 34,
"tilt": 3
},
"desiredPos": {
"position": 78,
"tilt": 97
},
"favPos": {
"position": 13,
"tilt": 17
}
}
}
"""
)
def patch_state(state, current, desired):
"""Generate a patch for a JSON state fixture."""
return f"""
{{
"shutter": {{
"state": {state},
"currentPos": {{ "position": {current} }},
"desiredPos": {{ "position": {desired} }}
}}
}}
"""
STATE_CLOSING = jmerge(STATE_DEFAULT, patch_state(0, 78, 100))
STATE_CLOSED = jmerge(STATE_DEFAULT, patch_state(3, 100, 100))
STATE_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 34, 0))
STATE_MINIMALLY_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 97, 100))
STATE_STOPPED = jmerge(STATE_DEFAULT, patch_state(2, 34, 100))
STATE_UNKNOWN = jmerge(STATE_DEFAULT, patch_state(2, 34, -1))
async def test_init(self, aioclient_mock):
"""Test cover default state."""
await self.allow_get_info(aioclient_mock)
entity = (await self.async_entities(aioclient_mock))[0]
assert entity.name == "My shutter 1 (shutterBox#position)"
assert entity.unique_id == "BleBox-shutterBox-2bee34e750b8-position"
assert entity.device_class == DEVICE_CLASS_SHUTTER
assert entity.supported_features & SUPPORT_OPEN
assert entity.supported_features & SUPPORT_CLOSE
assert entity.supported_features & SUPPORT_STOP
assert entity.supported_features & SUPPORT_SET_POSITION
assert entity.current_cover_position is None
# TODO: tilt
# assert entity.supported_features & SUPPORT_SET_TILT_POSITION
# assert entity.current_cover_tilt_position == None
self.assert_state(entity, None)
async def test_device_info(self, aioclient_mock):
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
entity = (await self.async_entities(aioclient_mock))[0]
assert entity.device_info["name"] == "My shutter 1"
assert entity.device_info["mac"] == "2bee34e750b8"
assert entity.device_info["manufacturer"] == "BleBox"
assert entity.device_info["model"] == "shutterBox"
assert entity.device_info["sw_version"] == "0.147"
async def test_update(self, aioclient_mock):
"""Test cover updating."""
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
assert entity.current_cover_position == 22 # 100 - 78
self.assert_state(entity, STATE_OPEN)
# TODO: tilt
# assert entity.current_tilt_position == 0
async def test_open(self, aioclient_mock):
"""Test cover opening."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
self.assert_state(entity, STATE_CLOSED)
self.allow_get(aioclient_mock, "/s/u", self.STATE_OPENING)
await entity.async_open_cover()
self.assert_state(entity, STATE_OPENING)
async def test_close(self, aioclient_mock):
"""Test cover closing."""
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
self.assert_state(entity, STATE_OPEN)
self.allow_get(aioclient_mock, "/s/d", self.STATE_CLOSING)
await entity.async_close_cover()
self.assert_state(entity, STATE_CLOSING)
async def test_set_position(self, aioclient_mock):
"""Test cover position setting."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
self.assert_state(entity, STATE_CLOSED)
self.allow_get(aioclient_mock, "/s/p/99", self.STATE_MINIMALLY_OPENING)
await entity.async_set_cover_position(**{ATTR_POSITION: 1}) # almost closed
self.assert_state(entity, STATE_OPENING)
async def test_stop(self, aioclient_mock):
"""Test cover stopping."""
entity = await self.updated(aioclient_mock, self.STATE_OPENING)
self.assert_state(entity, STATE_OPENING)
self.allow_get(aioclient_mock, "/s/s", self.STATE_STOPPED)
await entity.async_stop_cover()
self.assert_state(entity, STATE_OPEN)
async def test_unkown_position(self, aioclient_mock):
"""Test handling cover at unknown position."""
entity = await self.updated(aioclient_mock, self.STATE_UNKNOWN)
self.assert_state(entity, STATE_OPEN)
class TestGateBox(CoverTest):
"""Tests for cover devices representing a BleBox gateBox."""
DEV_INFO_PATH = "api/gate/state"
DEVICE_INFO = json.loads(
"""
{
"deviceName": "My gate 1",
"type": "gateBox",
"fv": "0.176",
"hv": "0.6",
"id": "1afe34db9437",
"ip": "192.168.1.11"
}
"""
)
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, f'{{ "apiLevel":"{future_date()}" }}')
DEVICE_INFO_LATEST = jmerge(
DEVICE_INFO, f'{{ "apiLevel":"{get_latest_api_level("gateBox")}" }}'
)
DEVICE_INFO_UNSUPPORTED = DEVICE_INFO
DEVICE_INFO_UNSPECIFIED_API = None # already handled as default case
STATE_DEFAULT = json.loads(
"""
{
"currentPos": 50,
"desiredPos": 50,
"extraButtonType": 1,
"extraButtonRelayNumber": 1,
"extraButtonPulseTimeMs": 800,
"extraButtonInvert": 1,
"gateType": 0,
"gateRelayNumber": 0,
"gatePulseTimeMs": 800,
"gateInvert": 0,
"inputsType": 1,
"openLimitSwitchInputNumber": 0,
"closeLimitSwitchInputNumber": 1
}
"""
)
STATE_CLOSED = jmerge(STATE_DEFAULT, '{ "currentPos": 0, "desiredPos": 0 }')
STATE_OPENING = jmerge(STATE_DEFAULT, '{ "currentPos": 50, "desiredPos": 100 }')
STATE_CLOSING = jmerge(STATE_DEFAULT, '{ "currentPos": 50, "desiredPos": 0 }')
STATE_STOPPED = jmerge(STATE_DEFAULT, '{ "currentPos": 50, "desiredPos": 50 }')
STATE_FULLY_OPENED = jmerge(
STATE_DEFAULT, '{ "currentPos": 100, "desiredPos": 100 }'
)
STATE_OPENING_NO_STOP = jmerge(STATE_OPENING, '{ "extraButtonType": 3}')
STATE_UNKNOWN = jmerge(STATE_DEFAULT, '{ "currentPos": -1, "desiredPos": 50 }')
async def test_init(self, aioclient_mock):
"""Test cover default state."""
await self.allow_get_info(aioclient_mock)
entity = (await self.async_entities(aioclient_mock))[0]
assert entity.name == "My gate 1 (gateBox#position)"
assert entity.unique_id == "BleBox-gateBox-1afe34db9437-position"
assert entity.device_class == DEVICE_CLASS_DOOR
assert entity.supported_features & SUPPORT_OPEN
assert entity.supported_features & SUPPORT_CLOSE
# Not available since requires fetching state to detect
assert not entity.supported_features & SUPPORT_STOP
assert not entity.supported_features & SUPPORT_SET_POSITION
assert entity.current_cover_position is None
self.assert_state(entity, None)
async def test_device_info(self, aioclient_mock):
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
entity = (await self.async_entities(aioclient_mock))[0]
assert entity.device_info["name"] == "My gate 1"
assert entity.device_info["mac"] == "1afe34db9437"
assert entity.device_info["manufacturer"] == "BleBox"
assert entity.device_info["model"] == "gateBox"
assert entity.device_info["sw_version"] == "0.176"
async def test_update(self, aioclient_mock):
"""Test cover updating."""
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
assert entity.current_cover_position == 50 # 100 - 34
self.assert_state(entity, STATE_OPEN)
async def test_open(self, aioclient_mock):
"""Test cover opening."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
assert entity.state == STATE_CLOSED
self.assert_state(entity, STATE_CLOSED)
self.allow_get(aioclient_mock, "/s/p", self.STATE_OPENING)
await entity.async_open_cover()
self.assert_state(entity, STATE_OPENING)
async def test_fully_opened(self, aioclient_mock):
entity = await self.updated(aioclient_mock, self.STATE_FULLY_OPENED)
assert entity.state == STATE_OPEN
async def test_close(self, aioclient_mock):
"""Test cover closing."""
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
self.assert_state(entity, STATE_OPEN)
self.allow_get(aioclient_mock, "/s/p", self.STATE_CLOSING)
await entity.async_close_cover()
self.assert_state(entity, STATE_CLOSING)
async def test_closed(self, aioclient_mock):
"""Test cover closed state."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
self.assert_state(entity, STATE_CLOSED)
async def test_stop(self, aioclient_mock):
"""Test cover stopping."""
entity = await self.updated(aioclient_mock, self.STATE_OPENING)
self.assert_state(entity, STATE_OPENING)
self.allow_get(aioclient_mock, "/s/s", self.STATE_STOPPED)
await entity.async_stop_cover()
self.assert_state(entity, STATE_OPEN)
async def test_with_stop(self, aioclient_mock):
"""Test stop capability is available."""
entity = await self.updated(aioclient_mock, self.STATE_OPENING)
assert entity.supported_features & SUPPORT_STOP
async def test_with_no_stop(self, aioclient_mock):
"""Test stop capability is not available."""
entity = await self.updated(aioclient_mock, self.STATE_OPENING_NO_STOP)
assert not entity.supported_features & SUPPORT_STOP
async def test_stop_with_no_stop(self, aioclient_mock):
"""Test stop capability is not available."""
entity = await self.updated(aioclient_mock, self.STATE_OPENING_NO_STOP)
with pytest.raises(
error.MisconfiguredDevice, match=r"second button not configured as 'stop'"
):
await entity.async_stop_cover()
async def test_set_position(self, aioclient_mock):
"""Test cover position setting."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
with pytest.raises(NotImplementedError):
await entity.async_set_cover_position(**{ATTR_POSITION: 1}) # almost closed
async def test_unkown_position(self, aioclient_mock):
"""Test handling cover at unknown position."""
entity = await self.updated(aioclient_mock, self.STATE_UNKNOWN)
self.assert_state(entity, None)
class TestGateBoxB(CoverTest):
"""Tests for cover devices representing a BleBox gateBoxB subgroup."""
DEV_INFO_PATH = "state/extended"
DEVICE_INFO = json.loads(
"""
{
"device": {
"deviceName":"My gateBox 1",
"type":"gateBox",
"product":"gateBox",
"hv":"9.1d",
"fv":"0.1010",
"universe":0,
"apiLevel":"20200831",
"id":"1afe34d27e4f",
"ip":"192.168.4.1",
"availableFv":null
}
}
"""
)
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date()))
DEVICE_INFO_LATEST = jmerge(
DEVICE_INFO, patch_version(get_latest_api_level("gateBox"))
)
DEVICE_INFO_UNSUPPORTED = DEVICE_INFO
DEVICE_INFO_UNSPECIFIED_API = None # already handled as default case
STATE_DEFAULT = json.loads(
"""
{
"gate": {
"currentPos": 0,
"openCloseMode": 0,
"gateType": 1,
"gatePulseTimeMs": 1500,
"gateOutputState": 0,
"extraButtonType": 1,
"extraButtonPulseTimeMs": 1500,
"extraButtonOutputState": 0,
"inputsType": 0
}
}
"""
)
STATE_CLOSED = STATE_DEFAULT
STATE_STOPPED = jmerge(STATE_DEFAULT, '{"gate": {"currentPos": 50 }}')
STATE_FULLY_OPENED = jmerge(STATE_DEFAULT, '{"gate": {"currentPos": 100 }}')
STATE_UNKNOWN = json.loads(
"""
{
"gate": {
"currentPos": -1,
"openCloseMode": 0,
"gateType": 1,
"gatePulseTimeMs": 1500,
"gateOutputState": 0,
"extraButtonType": 1,
"extraButtonPulseTimeMs": 1500,
"extraButtonOutputState": 0,
"inputsType": 0
}
}
"""
)
async def test_init(self, aioclient_mock):
"""Test cover default state."""
await self.allow_get_info(aioclient_mock)
entity = (await self.async_entities(aioclient_mock))[0]
assert entity.name == "My gateBox 1 (gateBox#position)"
assert entity.unique_id == "BleBox-gateBox-1afe34d27e4f-position"
assert entity.device_class == DEVICE_CLASS_DOOR
assert entity.supported_features & SUPPORT_OPEN
assert entity.supported_features & SUPPORT_CLOSE
assert entity.current_cover_position is None
self.assert_state(entity, None)
async def test_device_info(self, aioclient_mock):
"""Test device info state."""
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
entity = (await self.async_entities(aioclient_mock))[0]
# import pdb;pdb.set_trace()
assert entity.device_info["name"] == "My gateBox 1"
assert entity.device_info["mac"] == "1afe34d27e4f"
assert entity.device_info["manufacturer"] == "BleBox"
assert entity.device_info["model"] == "gateBox"
assert entity.device_info["sw_version"] == "0.1010"
async def test_fully_opened(self, aioclient_mock):
"""Test cover fully opened."""
entity = await self.updated(aioclient_mock, self.STATE_FULLY_OPENED)
assert entity.state == STATE_OPEN
async def test_stop(self, aioclient_mock):
"""Test cover stopped."""
entity = await self.updated(aioclient_mock, self.STATE_STOPPED)
self.assert_state(entity, STATE_OPEN)
async def test_closed(self, aioclient_mock):
"""Test cover closed."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
self.assert_state(entity, STATE_CLOSED)
async def test_unkown_position(self, aioclient_mock):
"""Test handling cover at unknown position."""
entity = await self.updated(aioclient_mock, self.STATE_UNKNOWN)
self.assert_state(entity, None)
class TestGateController(CoverTest):
"""Tests for cover devices representing a BleBox gateController."""
DEV_INFO_PATH = "api/gatecontroller/state"
DEVICE_INFO = json.loads(
"""
{
"device": {
"deviceName": "My gate controller 1",
"type": "gateController",
"apiLevel": "20180604",
"fv": "1.390",
"hv": "custom.2.6",
"id": "0ff2ffaafe30db9437",
"ip": "192.168.1.11"
}
}
"""
)
DEVICE_INFO_FUTURE = jmerge(DEVICE_INFO, patch_version(future_date()))
DEVICE_INFO_LATEST = jmerge(
DEVICE_INFO, patch_version(get_latest_api_level("gateController"))
)
DEVICE_INFO_UNSUPPORTED = jmerge(DEVICE_INFO, patch_version(20180603))
# NOTE: can't happen with a real device
DEVICE_INFO_UNSPECIFIED_API = json.loads(
"""
{
"device": {
"deviceName": "My gate controller 1",
"type": "gateController",
"fv": "0.981",
"hv": "custom.2.6",
"id": "0ff2ffaafe30db9437",
"ip": "192.168.1.11"
}
}
"""
)
STATE_DEFAULT = json.loads(
"""
{
"gateController": {
"state": 2,
"safety": {
"eventReason": 0,
"triggered": [ 0 ]
},
"currentPos": { "positions": [ 31 ] },
"desiredPos": { "positions": [ 29 ] }
}
}
"""
)
def patch_state(state, current, desired):
"""Generate a patch for a JSON state fixture."""
return f"""
{{
"gateController": {{
"state": {state},
"currentPos": {{ "positions": [ {current} ] }},
"desiredPos": {{ "positions": [ {desired} ] }}
}}
}}
"""
STATE_CLOSED = jmerge(STATE_DEFAULT, patch_state(3, 100, 100))
STATE_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 34, 0))
STATE_CLOSING = jmerge(STATE_DEFAULT, patch_state(0, 78, 100))
STATE_MINIMALLY_OPENING = jmerge(STATE_DEFAULT, patch_state(1, 97, 100))
STATE_STOPPED = jmerge(STATE_DEFAULT, patch_state(2, 34, 100))
async def test_init(self, aioclient_mock):
"""Test cover default state."""
await self.allow_get_info(aioclient_mock)
entity = (await self.async_entities(aioclient_mock))[0]
assert entity.name == "My gate controller 1 (gateController#position)"
assert entity.unique_id == "BleBox-gateController-0ff2ffaafe30db9437-position"
assert entity.device_class == DEVICE_CLASS_DOOR
assert entity.supported_features & SUPPORT_OPEN
assert entity.supported_features & SUPPORT_CLOSE
assert entity.supported_features & SUPPORT_STOP
assert entity.supported_features & SUPPORT_SET_POSITION
assert entity.current_cover_position is None
self.assert_state(entity, None)
async def test_device_info(self, aioclient_mock):
await self.allow_get_info(aioclient_mock, self.DEVICE_INFO)
entity = (await self.async_entities(aioclient_mock))[0]
assert entity.device_info["name"] == "My gate controller 1"
assert entity.device_info["mac"] == "0ff2ffaafe30db9437"
assert entity.device_info["manufacturer"] == "BleBox"
assert entity.device_info["model"] == "gateController"
assert entity.device_info["sw_version"] == "1.390"
async def test_update(self, aioclient_mock):
"""Test cover updating."""
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
assert entity.current_cover_position == 71 # 100 - 29
self.assert_state(entity, STATE_OPEN)
async def test_open(self, aioclient_mock):
"""Test cover opening."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
self.assert_state(entity, STATE_CLOSED)
self.allow_get(aioclient_mock, "/s/o", self.STATE_OPENING)
await entity.async_open_cover()
self.assert_state(entity, STATE_OPENING)
async def test_close(self, aioclient_mock):
"""Test cover closing."""
entity = await self.updated(aioclient_mock, self.STATE_DEFAULT)
self.assert_state(entity, STATE_OPEN)
self.allow_get(aioclient_mock, "/s/c", self.STATE_CLOSING)
await entity.async_close_cover()
self.assert_state(entity, STATE_CLOSING)
async def test_set_position(self, aioclient_mock):
"""Test cover position setting."""
entity = await self.updated(aioclient_mock, self.STATE_CLOSED)
self.assert_state(entity, STATE_CLOSED)
self.allow_get(aioclient_mock, "/s/p/99", self.STATE_MINIMALLY_OPENING)
await entity.async_set_cover_position(**{ATTR_POSITION: 1}) # almost closed
self.assert_state(entity, STATE_OPENING)
async def test_stop(self, aioclient_mock):
"""Test cover stopping."""
entity = await self.updated(aioclient_mock, self.STATE_OPENING)
self.assert_state(entity, STATE_OPENING)
self.allow_get(aioclient_mock, "/s/s", self.STATE_STOPPED)
await entity.async_stop_cover()
self.assert_state(entity, STATE_OPEN)
|