| 1 |
from enum import IntEnum, auto |
| 2 |
|
| 3 |
from .error import MisconfiguredDevice |
| 4 |
from .feature import Feature |
| 5 |
from typing import TYPE_CHECKING, Any, Optional, Type, TypeVar |
| 6 |
|
| 7 |
if TYPE_CHECKING: |
| 8 |
from .box import Box |
| 9 |
|
| 10 |
|
| 11 |
class BleboxCoverState(IntEnum): |
| 12 |
"""BleboxCoverState defines possible states of cover devices. |
| 13 |
|
| 14 |
Note that enumeration of states is partially shared between |
| 15 |
different types of devices (shutterBox, gateController) but |
| 16 |
not all states are possible for every type. For details of |
| 17 |
states refer to blebox official API documentation. |
| 18 |
""" |
| 19 |
|
| 20 |
MOVING_DOWN = 0 |
| 21 |
MOVING_UP = 1 |
| 22 |
MANUALLY_STOPPED = 2 |
| 23 |
LOWER_LIMIT_REACHED = 3 |
| 24 |
UPPER_LIMIT_REACHED = 4 |
| 25 |
OVERLOAD = 5 |
| 26 |
MOTOR_FAILURE = 6 |
| 27 |
UNUSED = 7 |
| 28 |
SAFETY_STOP = 8 |
| 29 |
|
| 30 |
|
| 31 |
class ShutterBoxControlType(IntEnum): |
| 32 |
"""ShutterBoxControlType defines shuterBox command semantics""" |
| 33 |
|
| 34 |
SEGMENTED_SHUTTER = 1 |
| 35 |
NO_CALIBRATION = 2 |
| 36 |
TILT_SHUTTER_90 = 3 |
| 37 |
WINDOW_OPENER = 4 |
| 38 |
MATERIAL_SHUTTER = 5 |
| 39 |
AWNING = 6 |
| 40 |
SCREEN = 7 |
| 41 |
CURTAIN = 8 |
| 42 |
ROOF_SEGMENTED_SHUTTER = 9 |
| 43 |
SERVOMOTOR = 10 |
| 44 |
CURTAIN_ONE_SIDED = 11 |
| 45 |
CURTAIN_TWO_SIDED = 12 |
| 46 |
TILT_ONLY = 13 |
| 47 |
PERGOLA_ROOF = 14 |
| 48 |
PERGOLA_ROOF_TILT_ONLY = 15 |
| 49 |
TILT_SHUTTER_180 = 16 |
| 50 |
TILT_SHUTTER_WITHOUT_POSITIONING = 17 |
| 51 |
|
| 52 |
|
| 53 |
class GateBoxControlType(IntEnum): |
| 54 |
"""GateBoxControlType defines gateBox command semantics known as `openCloseMode`. |
| 55 |
|
| 56 |
Control type affects mainly [o]pen, [c]lose, and [n]ext commands which |
| 57 |
are wrappers around [p]rimary and [s]econdary outputs. The only exception |
| 58 |
is OPEN_CLOSE (2) control type that also means that the gateBox lacks |
| 59 |
stop action because typical stop output is wired to [c]lose/[s]econdary |
| 60 |
command. |
| 61 |
""" |
| 62 |
|
| 63 |
STEP_BY_STEP = 0 |
| 64 |
ONLY_OPEN = 1 |
| 65 |
OPEN_CLOSE = 2 |
| 66 |
|
| 67 |
|
| 68 |
class GateBoxExtraButtonType(IntEnum): |
| 69 |
"""GateBoxExtraButtonType defines gateBox extra button semantics. |
| 70 |
|
| 71 |
Only meaningful outside OPEN_CLOSE mode, which takes the extra button output |
| 72 |
for the close action and leaves this field unused. |
| 73 |
""" |
| 74 |
|
| 75 |
DISABLED = 0 |
| 76 |
STOP = 1 |
| 77 |
WALK_IN = 2 |
| 78 |
OTHER = 3 |
| 79 |
|
| 80 |
|
| 81 |
class GateBoxGateType(IntEnum): |
| 82 |
"""GateBoxGateType defines possible gate/cover types reported by gateBox""" |
| 83 |
|
| 84 |
SLIDING_DOOR = 0 |
| 85 |
GARAGE_DOOR = 1 |
| 86 |
OVER_DOOR = 2 |
| 87 |
DOOR = 3 |
| 88 |
|
| 89 |
|
| 90 |
class UnifiedCoverType(IntEnum): |
| 91 |
"""UnifiedCoverType defines single "cover type" concept shared between different |
| 92 |
devices. |
| 93 |
|
| 94 |
Some device types have concept of control type/mode that affects how device |
| 95 |
operates and how it is being used (e.g. control type in shutterBox/gateControler), |
| 96 |
but others have these two concepts separated (e.g. open mode vs. gate type in |
| 97 |
gateBox). This enum provides unified concept of controlled cover type that |
| 98 |
can be infered from internal device information end exposed to library user. |
| 99 |
|
| 100 |
""" |
| 101 |
|
| 102 |
AWNING = auto() |
| 103 |
BLIND = auto() |
| 104 |
CURTAIN = auto() |
| 105 |
DAMPER = auto() |
| 106 |
DOOR = auto() |
| 107 |
GARAGE = auto() |
| 108 |
GATE = auto() |
| 109 |
SHADE = auto() |
| 110 |
SHUTTER = auto() |
| 111 |
WINDOW = auto() |
| 112 |
|
| 113 |
|
| 114 |
class Gate: |
| 115 |
_control_type: Optional[int] |
| 116 |
|
| 117 |
def __init__(self, control_type: int, is_calibrated: bool = False): |
| 118 |
self._control_type = control_type |
| 119 |
|
| 120 |
def read_state(self, alias: str, raw_value: Any, product: "Box") -> int: |
| 121 |
raw = raw_value("state") |
| 122 |
return product.expect_int(alias, raw, max(BleboxCoverState).value, 0) |
| 123 |
|
| 124 |
def read_desired(self, alias: str, raw_value: Any, product: "Box") -> Optional[int]: |
| 125 |
raw = raw_value("desired") |
| 126 |
min_position = self.min_position |
| 127 |
return product.expect_int(alias, raw, 100, min_position) |
| 128 |
|
| 129 |
def read_tilt(self, alias: str, raw_value: Any, product: "Box") -> int: |
| 130 |
raw = raw_value("tilt") |
| 131 |
min_position = self.min_position |
| 132 |
return product.expect_int(alias, raw, 100, min_position) |
| 133 |
|
| 134 |
def read_cover_type( |
| 135 |
self, alias: str, raw_value: Any, product: "Box" |
| 136 |
) -> UnifiedCoverType: |
| 137 |
return UnifiedCoverType.GATE |
| 138 |
|
| 139 |
@property |
| 140 |
def min_position(self) -> int: |
| 141 |
return 0 |
| 142 |
|
| 143 |
@property |
| 144 |
def is_position_inverted(self) -> bool: |
| 145 |
|
| 146 |
return True |
| 147 |
|
| 148 |
@property |
| 149 |
def is_slider(self) -> bool: |
| 150 |
return True |
| 151 |
|
| 152 |
@property |
| 153 |
def has_tilt(self) -> bool: |
| 154 |
return False |
| 155 |
|
| 156 |
@property |
| 157 |
def open_command(self) -> str: |
| 158 |
return "open" |
| 159 |
|
| 160 |
@property |
| 161 |
def close_command(self) -> str: |
| 162 |
return "close" |
| 163 |
|
| 164 |
@property |
| 165 |
def is_calibrated(self) -> None: |
| 166 |
return None |
| 167 |
|
| 168 |
def stop_command(self, has_stop: bool) -> str: |
| 169 |
return "stop" |
| 170 |
|
| 171 |
def read_has_stop(self, alias: str, raw_value: Any, product: "Box") -> bool: |
| 172 |
return True |
| 173 |
|
| 174 |
|
| 175 |
class Shutter(Gate): |
| 176 |
_control_type: Optional[ShutterBoxControlType] |
| 177 |
_is_calibrated: bool |
| 178 |
|
| 179 |
def __init__(self, control_type: int, is_calibrated: bool = False): |
| 180 |
super().__init__(control_type) |
| 181 |
self._is_calibrated = is_calibrated |
| 182 |
|
| 183 |
@property |
| 184 |
def min_position(self) -> int: |
| 185 |
return -1 |
| 186 |
|
| 187 |
@property |
| 188 |
def has_tilt(self) -> bool: |
| 189 |
return self._control_type in ( |
| 190 |
ShutterBoxControlType.TILT_SHUTTER_90, |
| 191 |
ShutterBoxControlType.TILT_SHUTTER_180, |
| 192 |
ShutterBoxControlType.TILT_ONLY, |
| 193 |
ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY, |
| 194 |
ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING, |
| 195 |
) |
| 196 |
|
| 197 |
@property |
| 198 |
def tilt_only(self) -> bool: |
| 199 |
return self._control_type in ( |
| 200 |
ShutterBoxControlType.TILT_ONLY, |
| 201 |
ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY, |
| 202 |
) |
| 203 |
|
| 204 |
@property |
| 205 |
def is_tilt_180(self) -> bool: |
| 206 |
return self._control_type == ShutterBoxControlType.TILT_SHUTTER_180 |
| 207 |
|
| 208 |
@property |
| 209 |
def is_slider(self) -> bool: |
| 210 |
return ( |
| 211 |
self._control_type |
| 212 |
not in ( |
| 213 |
ShutterBoxControlType.NO_CALIBRATION, |
| 214 |
ShutterBoxControlType.CURTAIN, |
| 215 |
ShutterBoxControlType.TILT_ONLY, |
| 216 |
ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY, |
| 217 |
ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING, |
| 218 |
) |
| 219 |
and self.is_calibrated is True |
| 220 |
) |
| 221 |
|
| 222 |
@property |
| 223 |
def is_calibrated(self) -> bool: |
| 224 |
return self._is_calibrated |
| 225 |
|
| 226 |
def read_cover_type( |
| 227 |
self, alias: str, raw_value: Any, product: "Box" |
| 228 |
) -> UnifiedCoverType: |
| 229 |
if self._control_type == ShutterBoxControlType.SEGMENTED_SHUTTER: |
| 230 |
return UnifiedCoverType.SHUTTER |
| 231 |
if self._control_type == ShutterBoxControlType.NO_CALIBRATION: |
| 232 |
return UnifiedCoverType.SHUTTER |
| 233 |
if self._control_type == ShutterBoxControlType.TILT_SHUTTER_90: |
| 234 |
return UnifiedCoverType.SHUTTER |
| 235 |
if self._control_type == ShutterBoxControlType.WINDOW_OPENER: |
| 236 |
return UnifiedCoverType.WINDOW |
| 237 |
if self._control_type == ShutterBoxControlType.MATERIAL_SHUTTER: |
| 238 |
return UnifiedCoverType.SHADE |
| 239 |
if self._control_type == ShutterBoxControlType.AWNING: |
| 240 |
return UnifiedCoverType.AWNING |
| 241 |
if self._control_type == ShutterBoxControlType.SCREEN: |
| 242 |
return UnifiedCoverType.SHADE |
| 243 |
if self._control_type == ShutterBoxControlType.CURTAIN: |
| 244 |
return UnifiedCoverType.CURTAIN |
| 245 |
if self._control_type == ShutterBoxControlType.ROOF_SEGMENTED_SHUTTER: |
| 246 |
return UnifiedCoverType.SHUTTER |
| 247 |
if self._control_type == ShutterBoxControlType.SERVOMOTOR: |
| 248 |
return UnifiedCoverType.SHUTTER |
| 249 |
if self._control_type == ShutterBoxControlType.CURTAIN_ONE_SIDED: |
| 250 |
return UnifiedCoverType.CURTAIN |
| 251 |
if self._control_type == ShutterBoxControlType.CURTAIN_TWO_SIDED: |
| 252 |
return UnifiedCoverType.CURTAIN |
| 253 |
if self._control_type == ShutterBoxControlType.TILT_ONLY: |
| 254 |
return UnifiedCoverType.SHUTTER |
| 255 |
if self._control_type == ShutterBoxControlType.PERGOLA_ROOF: |
| 256 |
return UnifiedCoverType.AWNING |
| 257 |
if self._control_type == ShutterBoxControlType.PERGOLA_ROOF_TILT_ONLY: |
| 258 |
return UnifiedCoverType.AWNING |
| 259 |
if self._control_type == ShutterBoxControlType.TILT_SHUTTER_180: |
| 260 |
return UnifiedCoverType.SHUTTER |
| 261 |
if self._control_type == ShutterBoxControlType.TILT_SHUTTER_WITHOUT_POSITIONING: |
| 262 |
return UnifiedCoverType.SHUTTER |
| 263 |
|
| 264 |
|
| 265 |
class GateBox(Gate): |
| 266 |
_control_type: Optional[GateBoxControlType] |
| 267 |
|
| 268 |
@property |
| 269 |
def is_position_inverted(self) -> bool: |
| 270 |
|
| 271 |
return False |
| 272 |
|
| 273 |
@property |
| 274 |
def is_slider(self) -> bool: |
| 275 |
return False |
| 276 |
|
| 277 |
@property |
| 278 |
def open_command(self) -> str: |
| 279 |
return "primary" |
| 280 |
|
| 281 |
@property |
| 282 |
def close_command(self) -> str: |
| 283 |
return "primary" |
| 284 |
|
| 285 |
def read_state(self, alias: str, raw_value: Any, product: "Box") -> int: |
| 286 |
current = raw_value("position") |
| 287 |
desired = raw_value("desired") |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
if current == -1: |
| 292 |
return None |
| 293 |
|
| 294 |
if desired < current: |
| 295 |
return 0 |
| 296 |
|
| 297 |
if desired > current: |
| 298 |
return 1 |
| 299 |
|
| 300 |
if current == 0: |
| 301 |
return 3 |
| 302 |
|
| 303 |
if current == 100: |
| 304 |
return 4 |
| 305 |
|
| 306 |
return 2 |
| 307 |
|
| 308 |
def stop_command(self, has_stop: bool) -> str: |
| 309 |
if not has_stop: |
| 310 |
raise MisconfiguredDevice("second button not configured as 'stop'") |
| 311 |
return "secondary" |
| 312 |
|
| 313 |
def read_has_stop(self, alias: str, raw_value: Any, product: "Box") -> bool: |
| 314 |
if product.last_data is None: |
| 315 |
return False |
| 316 |
|
| 317 |
button_type = raw_value("extraButtonType") |
| 318 |
if button_type is None: |
| 319 |
return False |
| 320 |
|
| 321 |
return button_type == GateBoxExtraButtonType.STOP |
| 322 |
|
| 323 |
|
| 324 |
class GateBoxB(GateBox): |
| 325 |
def read_state(self, alias: str, raw_value: Any, product: "Box") -> int: |
| 326 |
current = raw_value("position") |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
if current == -1: |
| 331 |
return None |
| 332 |
|
| 333 |
elif 0 < current < 100: |
| 334 |
return 2 |
| 335 |
elif current == 0: |
| 336 |
return 3 |
| 337 |
|
| 338 |
return 4 |
| 339 |
|
| 340 |
def read_desired(self, alias: str, raw_value: Any, product: "Box") -> Optional[int]: |
| 341 |
return raw_value("position") |
| 342 |
|
| 343 |
def read_has_stop(self, alias: str, raw_value: Any, product: "Box") -> bool: |
| 344 |
if product.last_data is None: |
| 345 |
return False |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
if self._control_type == GateBoxControlType.OPEN_CLOSE: |
| 350 |
return False |
| 351 |
|
| 352 |
|
| 353 |
extra_button_type = raw_value("extra_button_type") |
| 354 |
if extra_button_type in ( |
| 355 |
GateBoxExtraButtonType.WALK_IN, |
| 356 |
GateBoxExtraButtonType.OTHER, |
| 357 |
): |
| 358 |
return False |
| 359 |
|
| 360 |
return True |
| 361 |
|
| 362 |
def read_cover_type( |
| 363 |
self, alias: str, raw_value: Any, product: "Box" |
| 364 |
) -> Optional[UnifiedCoverType]: |
| 365 |
if (gate_type := raw_value("gate_type")) is None: |
| 366 |
return |
| 367 |
|
| 368 |
if gate_type == GateBoxGateType.GARAGE_DOOR: |
| 369 |
return UnifiedCoverType.GARAGE |
| 370 |
if gate_type == GateBoxGateType.SLIDING_DOOR: |
| 371 |
return UnifiedCoverType.GATE |
| 372 |
return UnifiedCoverType.DOOR |
| 373 |
|
| 374 |
@property |
| 375 |
def close_command(self) -> str: |
| 376 |
if self._control_type == GateBoxControlType.OPEN_CLOSE: |
| 377 |
return "secondary" |
| 378 |
|
| 379 |
return super().close_command |
| 380 |
|
| 381 |
|
| 382 |
GateT = TypeVar("GateT", bound=Gate) |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
class Cover(Feature): |
| 387 |
_desired: Optional[int] |
| 388 |
_state: Optional[BleboxCoverState] |
| 389 |
_has_stop: Optional[bool] |
| 390 |
_cover_type: Optional[UnifiedCoverType] |
| 391 |
|
| 392 |
def __init__( |
| 393 |
self, |
| 394 |
product: "Box", |
| 395 |
alias: str, |
| 396 |
methods: dict, |
| 397 |
dev_class: str, |
| 398 |
subclass: Type[GateT], |
| 399 |
extended_state: dict, |
| 400 |
) -> None: |
| 401 |
control_type = None |
| 402 |
is_calibrated: bool = False |
| 403 |
if extended_state and issubclass(subclass, Shutter): |
| 404 |
control_type = extended_state.get("shutter", {}).get("controlType", None) |
| 405 |
is_calibrated = bool( |
| 406 |
extended_state.get("shutter", {}) |
| 407 |
.get("calibrationParameters", {}) |
| 408 |
.get("isCalibrated", False) |
| 409 |
) |
| 410 |
|
| 411 |
elif extended_state and issubclass(subclass, GateBoxB): |
| 412 |
control_type = extended_state.get("gate", {}).get("openCloseMode", None) |
| 413 |
|
| 414 |
self._device_class = dev_class |
| 415 |
self._attributes: GateT = subclass(control_type, is_calibrated) |
| 416 |
self._tilt_current = None |
| 417 |
super().__init__(product, alias, methods) |
| 418 |
|
| 419 |
@classmethod |
| 420 |
def many_from_config( |
| 421 |
cls, product, box_type_config, extended_state |
| 422 |
) -> list["Feature"]: |
| 423 |
return [cls(product, *args, extended_state) for args in box_type_config] |
| 424 |
|
| 425 |
@property |
| 426 |
def current(self) -> Any: |
| 427 |
return self._desired |
| 428 |
|
| 429 |
@property |
| 430 |
def state(self) -> Any: |
| 431 |
return self._state |
| 432 |
|
| 433 |
@property |
| 434 |
def tilt_current(self): |
| 435 |
return self._tilt_current |
| 436 |
|
| 437 |
@property |
| 438 |
def is_calibrated(self) -> Optional[bool]: |
| 439 |
return self._attributes.is_calibrated |
| 440 |
|
| 441 |
@property |
| 442 |
def is_slider(self) -> Any: |
| 443 |
return self._attributes.is_slider |
| 444 |
|
| 445 |
@property |
| 446 |
def has_tilt(self) -> bool: |
| 447 |
return self._attributes.has_tilt |
| 448 |
|
| 449 |
@property |
| 450 |
def is_position_inverted(self) -> bool: |
| 451 |
return self._attributes.is_position_inverted |
| 452 |
|
| 453 |
@property |
| 454 |
def has_stop(self) -> bool: |
| 455 |
return self._has_stop |
| 456 |
|
| 457 |
@property |
| 458 |
def tilt_only(self) -> bool: |
| 459 |
return ( |
| 460 |
self._attributes.tilt_only |
| 461 |
if hasattr(self._attributes, "tilt_only") |
| 462 |
else False |
| 463 |
) |
| 464 |
|
| 465 |
@property |
| 466 |
def is_tilt_180(self) -> bool: |
| 467 |
return ( |
| 468 |
self._attributes.is_tilt_180 |
| 469 |
if hasattr(self._attributes, "is_tilt_180") |
| 470 |
else False |
| 471 |
) |
| 472 |
|
| 473 |
@property |
| 474 |
def cover_type(self) -> Optional[UnifiedCoverType]: |
| 475 |
return self._cover_type |
| 476 |
|
| 477 |
async def async_open(self) -> None: |
| 478 |
await self.async_api_command(self._attributes.open_command) |
| 479 |
|
| 480 |
async def async_close(self) -> None: |
| 481 |
await self.async_api_command(self._attributes.close_command) |
| 482 |
|
| 483 |
async def async_stop(self) -> None: |
| 484 |
await self.async_api_command(self._attributes.stop_command(self._has_stop)) |
| 485 |
|
| 486 |
async def async_set_position(self, value: Any) -> None: |
| 487 |
if not self.is_slider: |
| 488 |
raise NotImplementedError |
| 489 |
|
| 490 |
await self.async_api_command("position", value) |
| 491 |
|
| 492 |
async def async_set_tilt_position(self, value: Any) -> None: |
| 493 |
if self.has_tilt: |
| 494 |
await self.async_api_command("tilt", value) |
| 495 |
else: |
| 496 |
raise NotImplementedError |
| 497 |
|
| 498 |
async def async_close_tilt(self, **kwargs: Any) -> None: |
| 499 |
await self.async_api_command("tilt", 100) |
| 500 |
|
| 501 |
async def async_open_tilt(self, **kwargs: Any) -> None: |
| 502 |
await self.async_api_command("tilt", 0) |
| 503 |
|
| 504 |
def _read_cover_type(self) -> Optional[UnifiedCoverType]: |
| 505 |
product = self._product |
| 506 |
if not product.last_data: |
| 507 |
return None |
| 508 |
|
| 509 |
alias = self._alias |
| 510 |
return self._attributes.read_cover_type(alias, self.raw_value, self._product) |
| 511 |
|
| 512 |
def _read_desired(self) -> Any: |
| 513 |
product = self._product |
| 514 |
if not product.last_data: |
| 515 |
return None |
| 516 |
|
| 517 |
alias = self._alias |
| 518 |
return self._attributes.read_desired(alias, self.raw_value, self._product) |
| 519 |
|
| 520 |
|
| 521 |
def _read_state(self) -> Any: |
| 522 |
product = self._product |
| 523 |
if not product.last_data: |
| 524 |
return None |
| 525 |
|
| 526 |
alias = self._alias |
| 527 |
return self._attributes.read_state(alias, self.raw_value, self._product) |
| 528 |
|
| 529 |
def _read_tilt(self) -> Any: |
| 530 |
product = self._product |
| 531 |
if not product.last_data: |
| 532 |
return None |
| 533 |
|
| 534 |
alias = self._alias |
| 535 |
return self._attributes.read_tilt(alias, self.raw_value, self._product) |
| 536 |
|
| 537 |
def _read_has_stop(self) -> bool: |
| 538 |
return self._attributes.read_has_stop( |
| 539 |
self._alias, self.raw_value, self._product |
| 540 |
) |
| 541 |
|
| 542 |
def after_update(self) -> None: |
| 543 |
self._desired = self._read_desired() |
| 544 |
self._state = self._read_state() |
| 545 |
self._has_stop = self._read_has_stop() |
| 546 |
self._cover_type = self._read_cover_type() |
| 547 |
|
| 548 |
if self._attributes.has_tilt: |
| 549 |
self._tilt_current = self._read_tilt() |