| 1 |
from unittest.mock import Mock |
| 2 |
import pytest |
| 3 |
|
| 4 |
from blebox_uniapi.button import Button |
| 5 |
from blebox_uniapi.box import Box |
| 6 |
from blebox_uniapi.box_types import BOX_TYPE_CONF, get_latest_api_level |
| 7 |
from blebox_uniapi.cover import GateBoxControlType, GateBoxExtraButtonType |
| 8 |
|
| 9 |
from .conftest import CommonEntity, DefaultBoxTest, future_date |
| 10 |
|
| 11 |
|
| 12 |
class BleBoxButtonEntity(CommonEntity): |
| 13 |
async def async_press(self): |
| 14 |
return await self._feature.set() |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture |
| 18 |
def product(): |
| 19 |
return Mock(spec=Box) |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture |
| 23 |
def tv_lift_box_0(product): |
| 24 |
product.type = "tvLiftBox" |
| 25 |
extended_state = {"tvLift": {"controlType": 4}} |
| 26 |
many = Button.many_from_config( |
| 27 |
product, |
| 28 |
BOX_TYPE_CONF["tvLiftBox"][20200518]["buttons"], |
| 29 |
extended_state=extended_state, |
| 30 |
) |
| 31 |
assert len(many) == 3 |
| 32 |
return many[0] |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture |
| 36 |
def tv_lift_box_1(product): |
| 37 |
product.type = "tvLiftBox" |
| 38 |
extended_state = {"tvLift": {"controlType": 4}} |
| 39 |
many = Button.many_from_config( |
| 40 |
product, |
| 41 |
BOX_TYPE_CONF["tvLiftBox"][20200518]["buttons"], |
| 42 |
extended_state=extended_state, |
| 43 |
) |
| 44 |
assert len(many) == 3 |
| 45 |
return many[1] |
| 46 |
|
| 47 |
|
| 48 |
async def test_tv_lift_0_box_pressed(tv_lift_box_0: Button, product: Box): |
| 49 |
await tv_lift_box_0.set() |
| 50 |
product.async_api_command.assert_called_with("set", "open_or_stop") |
| 51 |
assert tv_lift_box_0.control_type |
| 52 |
|
| 53 |
|
| 54 |
async def test_tv_lift_1_box_pressed(tv_lift_box_1: Button, product: Box): |
| 55 |
await tv_lift_box_1.set() |
| 56 |
product.async_api_command.assert_called_with("set", "close_or_stop") |
| 57 |
assert tv_lift_box_1.control_type |
| 58 |
|
| 59 |
|
| 60 |
def gate_box_buttons(product: Box, extended_state) -> list[Button]: |
| 61 |
product.type = "gateBox" |
| 62 |
return Button.many_from_config( |
| 63 |
product, |
| 64 |
BOX_TYPE_CONF["gateBox"][20200831]["buttons"], |
| 65 |
extended_state=extended_state, |
| 66 |
) |
| 67 |
|
| 68 |
|
| 69 |
@pytest.fixture |
| 70 |
def gate_box_second_output(product: Box) -> Button: |
| 71 |
many = gate_box_buttons( |
| 72 |
product, |
| 73 |
{ |
| 74 |
"gate": { |
| 75 |
"extraButtonType": GateBoxExtraButtonType.WALK_IN, |
| 76 |
"openCloseMode": GateBoxControlType.STEP_BY_STEP, |
| 77 |
} |
| 78 |
}, |
| 79 |
) |
| 80 |
assert len(many) == 1 |
| 81 |
return many[0] |
| 82 |
|
| 83 |
|
| 84 |
async def test_gate_box_second_output_pressed( |
| 85 |
gate_box_second_output: Button, product: Box |
| 86 |
): |
| 87 |
await gate_box_second_output.set() |
| 88 |
product.async_api_command.assert_called_with("secondary", "second_output") |
| 89 |
|
| 90 |
|
| 91 |
def test_gate_box_second_output_alias(gate_box_second_output: Button): |
| 92 |
assert gate_box_second_output.alias == "second_output" |
| 93 |
assert gate_box_second_output.control_type is None |
| 94 |
|
| 95 |
|
| 96 |
@pytest.mark.parametrize( |
| 97 |
"extra_button_type", |
| 98 |
[GateBoxExtraButtonType.WALK_IN, GateBoxExtraButtonType.OTHER], |
| 99 |
) |
| 100 |
@pytest.mark.parametrize( |
| 101 |
"open_close_mode", |
| 102 |
[GateBoxControlType.STEP_BY_STEP, GateBoxControlType.ONLY_OPEN], |
| 103 |
) |
| 104 |
def test_gate_box_second_output_created( |
| 105 |
product: Box, |
| 106 |
extra_button_type: GateBoxExtraButtonType, |
| 107 |
open_close_mode: GateBoxControlType, |
| 108 |
): |
| 109 |
many = gate_box_buttons( |
| 110 |
product, |
| 111 |
{ |
| 112 |
"gate": { |
| 113 |
"extraButtonType": extra_button_type, |
| 114 |
"openCloseMode": open_close_mode, |
| 115 |
} |
| 116 |
}, |
| 117 |
) |
| 118 |
assert len(many) == 1 |
| 119 |
|
| 120 |
|
| 121 |
@pytest.mark.parametrize( |
| 122 |
"extra_button_type", |
| 123 |
[GateBoxExtraButtonType.WALK_IN, GateBoxExtraButtonType.OTHER], |
| 124 |
) |
| 125 |
def test_gate_box_second_output_created_without_open_close_mode( |
| 126 |
product: Box, extra_button_type: GateBoxExtraButtonType |
| 127 |
): |
| 128 |
"""Test api levels below 20230102, which never report openCloseMode.""" |
| 129 |
|
| 130 |
many = gate_box_buttons(product, {"gate": {"extraButtonType": extra_button_type}}) |
| 131 |
assert len(many) == 1 |
| 132 |
|
| 133 |
|
| 134 |
@pytest.mark.parametrize( |
| 135 |
"gate", |
| 136 |
[ |
| 137 |
{ |
| 138 |
"extraButtonType": GateBoxExtraButtonType.WALK_IN, |
| 139 |
"openCloseMode": GateBoxControlType.OPEN_CLOSE, |
| 140 |
}, |
| 141 |
{ |
| 142 |
"extraButtonType": GateBoxExtraButtonType.OTHER, |
| 143 |
"openCloseMode": GateBoxControlType.OPEN_CLOSE, |
| 144 |
}, |
| 145 |
{ |
| 146 |
"extraButtonType": GateBoxExtraButtonType.DISABLED, |
| 147 |
"openCloseMode": GateBoxControlType.STEP_BY_STEP, |
| 148 |
}, |
| 149 |
{ |
| 150 |
"extraButtonType": GateBoxExtraButtonType.STOP, |
| 151 |
"openCloseMode": GateBoxControlType.STEP_BY_STEP, |
| 152 |
}, |
| 153 |
{"extraButtonType": GateBoxExtraButtonType.DISABLED}, |
| 154 |
{"extraButtonType": GateBoxExtraButtonType.STOP}, |
| 155 |
{"openCloseMode": GateBoxControlType.STEP_BY_STEP}, |
| 156 |
{}, |
| 157 |
], |
| 158 |
) |
| 159 |
def test_gate_box_second_output_not_created(product: Box, gate: dict): |
| 160 |
assert gate_box_buttons(product, {"gate": gate}) == [] |
| 161 |
|
| 162 |
|
| 163 |
@pytest.mark.parametrize( |
| 164 |
("box_type", "api_level"), [("gateBox", 20200831), ("tvLiftBox", 20200518)] |
| 165 |
) |
| 166 |
@pytest.mark.parametrize("extended_state", [None, {}, "not a dict"]) |
| 167 |
def test_no_buttons_without_usable_extended_state( |
| 168 |
product: Box, box_type: str, api_level: int, extended_state |
| 169 |
): |
| 170 |
product.type = box_type |
| 171 |
many = Button.many_from_config( |
| 172 |
product, |
| 173 |
BOX_TYPE_CONF[box_type][api_level]["buttons"], |
| 174 |
extended_state=extended_state, |
| 175 |
) |
| 176 |
assert many == [] |
| 177 |
|
| 178 |
|
| 179 |
class TestGateBoxSecondOutput(DefaultBoxTest): |
| 180 |
"""Tests for a gateBox exposing its extra button output as a button.""" |
| 181 |
|
| 182 |
DEVCLASS = "buttons" |
| 183 |
ENTITY_CLASS = BleBoxButtonEntity |
| 184 |
DEV_INFO_PATH = "state/extended" |
| 185 |
|
| 186 |
DEVICE_INFO = { |
| 187 |
"device": { |
| 188 |
"deviceName": "My gateBox 1", |
| 189 |
"type": "gateBox", |
| 190 |
"product": "gateBox", |
| 191 |
"fv": "0.1010", |
| 192 |
"hv": "9.1d", |
| 193 |
"id": "1afe34d27e4f", |
| 194 |
"apiLevel": "20230102", |
| 195 |
} |
| 196 |
} |
| 197 |
DEVICE_INFO_FUTURE = { |
| 198 |
"device": {**DEVICE_INFO["device"], "apiLevel": future_date()} |
| 199 |
} |
| 200 |
DEVICE_INFO_LATEST = { |
| 201 |
"device": { |
| 202 |
**DEVICE_INFO["device"], |
| 203 |
"apiLevel": get_latest_api_level("gateBox"), |
| 204 |
} |
| 205 |
} |
| 206 |
DEVICE_INFO_UNSUPPORTED = DEVICE_INFO |
| 207 |
DEVICE_INFO_UNSPECIFIED_API = None |
| 208 |
|
| 209 |
STATE_DEFAULT = { |
| 210 |
"gate": { |
| 211 |
"currentPos": 0, |
| 212 |
"openCloseMode": 0, |
| 213 |
"gateType": 1, |
| 214 |
"gatePulseTimeMs": 1500, |
| 215 |
"gateOutputState": 0, |
| 216 |
"extraButtonType": 2, |
| 217 |
"extraButtonPulseTimeMs": 1500, |
| 218 |
"extraButtonOutputState": 0, |
| 219 |
"inputsType": 0, |
| 220 |
} |
| 221 |
} |
| 222 |
DEVICE_EXTENDED_INFO = STATE_DEFAULT |
| 223 |
DEVICE_EXTENDED_INFO_PATH = "/state/extended" |
| 224 |
|
| 225 |
async def test_init(self, aioclient_mock): |
| 226 |
"""Test that a usable extra button output yields exactly one button.""" |
| 227 |
|
| 228 |
await self.allow_get_info(aioclient_mock) |
| 229 |
entities = await self.async_entities(aioclient_mock) |
| 230 |
|
| 231 |
assert len(entities) == 1 |
| 232 |
entity = entities[0] |
| 233 |
assert entity.name == "My gateBox 1 (gateBox#second_output)" |
| 234 |
assert entity.unique_id == "BleBox-gateBox-1afe34d27e4f-second_output" |
| 235 |
|
| 236 |
async def test_pressed(self, aioclient_mock): |
| 237 |
"""Test that pressing the button pulses the extra button output.""" |
| 238 |
|
| 239 |
await self.allow_get_info(aioclient_mock) |
| 240 |
entity = (await self.async_entities(aioclient_mock))[0] |
| 241 |
|
| 242 |
self.allow_get(aioclient_mock, "/s/s", self.STATE_DEFAULT) |
| 243 |
await entity.async_press() |
| 244 |
|
| 245 |
|
| 246 |
class TestGateBoxSecondOutputWithoutOpenCloseMode(TestGateBoxSecondOutput): |
| 247 |
"""Tests for a gateBox on an api level that never reports openCloseMode.""" |
| 248 |
|
| 249 |
DEVICE_INFO = { |
| 250 |
"device": { |
| 251 |
**TestGateBoxSecondOutput.DEVICE_INFO["device"], |
| 252 |
"apiLevel": "20220713", |
| 253 |
} |
| 254 |
} |
| 255 |
DEVICE_INFO_UNSUPPORTED = DEVICE_INFO |
| 256 |
|
| 257 |
STATE_DEFAULT = { |
| 258 |
"gate": { |
| 259 |
"currentPos": 0, |
| 260 |
"gateType": 1, |
| 261 |
"gatePulseTimeMs": 1500, |
| 262 |
"extraButtonType": 2, |
| 263 |
"extraButtonPulseTimeMs": 1500, |
| 264 |
} |
| 265 |
} |
| 266 |
DEVICE_EXTENDED_INFO = STATE_DEFAULT |