| 1 |
"""Qbus state models.""" |
| 2 |
|
| 3 |
from enum import StrEnum |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from .const import ( |
| 7 |
KEY_OUTPUT_ACTION, |
| 8 |
KEY_OUTPUT_ID, |
| 9 |
KEY_OUTPUT_PROPERTIES, |
| 10 |
KEY_OUTPUT_TYPE, |
| 11 |
KEY_PROPERTIES_CO2, |
| 12 |
KEY_PROPERTIES_CURRENT_TEMPERATURE, |
| 13 |
KEY_PROPERTIES_REFRESH, |
| 14 |
KEY_PROPERTIES_REGIME, |
| 15 |
KEY_PROPERTIES_SET_TEMPERATURE, |
| 16 |
KEY_PROPERTIES_SHUTTER_POSITION, |
| 17 |
KEY_PROPERTIES_SLAT_POSITION, |
| 18 |
KEY_PROPERTIES_STATE, |
| 19 |
KEY_PROPERTIES_VALUE, |
| 20 |
) |
| 21 |
|
| 22 |
KEY_DEVICE_CONNECTABLE = "connectable" |
| 23 |
KEY_DEVICE_CONNECTED = "connected" |
| 24 |
KEY_DEVICE_ID = "id" |
| 25 |
KEY_DEVICE_STATE_PROPERTIES = "properties" |
| 26 |
|
| 27 |
KEY_GATEWAY_ID = "id" |
| 28 |
KEY_GATEWAY_ONLINE = "online" |
| 29 |
KEY_GATEWAY_REASON = "reason" |
| 30 |
|
| 31 |
|
| 32 |
class StateType(StrEnum): |
| 33 |
"""Values to be used as state type.""" |
| 34 |
|
| 35 |
ACTION = "action" |
| 36 |
EVENT = "event" |
| 37 |
STATE = "state" |
| 38 |
|
| 39 |
|
| 40 |
class StateAction(StrEnum): |
| 41 |
"""Values to be used as state action.""" |
| 42 |
|
| 43 |
ACTIVATE = "activate" |
| 44 |
ACTIVE = "active" |
| 45 |
|
| 46 |
|
| 47 |
class GaugeStateProperty(StrEnum): |
| 48 |
"""Keys to read gauge state.""" |
| 49 |
|
| 50 |
CURRENT_VALUE = "currentValue" |
| 51 |
CONSUMPTION_VALUE = "consumptionValue" |
| 52 |
|
| 53 |
|
| 54 |
class WeatherStationStateProperty(StrEnum): |
| 55 |
"""Keys to read the weahter station state.""" |
| 56 |
|
| 57 |
DAYLIGHT = "dayLight" |
| 58 |
LIGHT = "light" |
| 59 |
LIGHT_EAST = "lightEast" |
| 60 |
LIGHT_SOUTH = "lightSouth" |
| 61 |
LIGHT_WEST = "lightWest" |
| 62 |
RAINING = "raining" |
| 63 |
TEMPERATURE = "temperature" |
| 64 |
TWILIGHT = "twilight" |
| 65 |
WIND = "wind" |
| 66 |
|
| 67 |
|
| 68 |
class MultiColorStateProperty(StrEnum): |
| 69 |
"""Keys to read or write the multi-color state.""" |
| 70 |
|
| 71 |
BRIGHTNESS = "brightness" |
| 72 |
CURRENT_REGIME = "currRegime" |
| 73 |
HUE = "hue" |
| 74 |
ON = "on" |
| 75 |
PRESET_COLOR = "presetColor" |
| 76 |
PRESET_MOVIE = "presetMovie" |
| 77 |
SATURATION = "saturation" |
| 78 |
|
| 79 |
|
| 80 |
class MultiColorRegime(StrEnum): |
| 81 |
"""Possible regimes for the multi-color output.""" |
| 82 |
|
| 83 |
COLOR_SELECT = "ColorSelect" |
| 84 |
COLOR_WHEEL = "ColorWheel" |
| 85 |
MOVIE_SELECT = "MovieSelect" |
| 86 |
|
| 87 |
|
| 88 |
class QbusMqttGatewayState: |
| 89 |
"""MQTT representation of a Qbus gateway state.""" |
| 90 |
|
| 91 |
def __init__(self, data: dict) -> None: |
| 92 |
"""Initialize based on a json loaded dictionary.""" |
| 93 |
self.id: str | None = data.get(KEY_GATEWAY_ID) |
| 94 |
self.online: bool | None = data.get(KEY_GATEWAY_ONLINE) |
| 95 |
self.reason: str | None = data.get(KEY_GATEWAY_REASON) |
| 96 |
|
| 97 |
|
| 98 |
class QbusMqttDeviceStateProperties: |
| 99 |
"""MQTT representation of a Qbus device its state properties.""" |
| 100 |
|
| 101 |
def __init__(self, data: dict) -> None: |
| 102 |
"""Initialize based on a json loaded dictionary.""" |
| 103 |
self.connectable: bool | None = data.get(KEY_DEVICE_CONNECTABLE) |
| 104 |
self.connected: bool | None = data.get(KEY_DEVICE_CONNECTED) |
| 105 |
|
| 106 |
|
| 107 |
class QbusMqttDeviceState: |
| 108 |
"""MQTT representation of a Qbus device state.""" |
| 109 |
|
| 110 |
def __init__(self, data: dict) -> None: |
| 111 |
"""Initialize based on a json loaded dictionary.""" |
| 112 |
self.id: str | None = data.get(KEY_DEVICE_ID) |
| 113 |
|
| 114 |
properties = data.get(KEY_DEVICE_STATE_PROPERTIES) |
| 115 |
self.properties: QbusMqttDeviceStateProperties | None = ( |
| 116 |
QbusMqttDeviceStateProperties(properties) if properties is not None else None |
| 117 |
) |
| 118 |
|
| 119 |
|
| 120 |
class QbusMqttState: |
| 121 |
"""MQTT representation of a Qbus state.""" |
| 122 |
|
| 123 |
def __init__( |
| 124 |
self, |
| 125 |
data: dict | None = None, |
| 126 |
*, |
| 127 |
id: str | None = None, |
| 128 |
type: str | None = None, |
| 129 |
action: str | None = None, |
| 130 |
) -> None: |
| 131 |
"""Initialize state.""" |
| 132 |
self.id: str = "" |
| 133 |
self.type: str = "" |
| 134 |
self.action: str | None = None |
| 135 |
self.properties: dict | None = None |
| 136 |
|
| 137 |
if data is not None: |
| 138 |
self.id = data.get(KEY_OUTPUT_ID, "") |
| 139 |
self.type = data.get(KEY_OUTPUT_TYPE, "") |
| 140 |
self.action = data.get(KEY_OUTPUT_ACTION) |
| 141 |
self.properties = data.get(KEY_OUTPUT_PROPERTIES) |
| 142 |
|
| 143 |
if id is not None: |
| 144 |
self.id = id |
| 145 |
|
| 146 |
if type is not None: |
| 147 |
self.type = type |
| 148 |
|
| 149 |
if action is not None: |
| 150 |
self.action = action |
| 151 |
|
| 152 |
def read_property(self, key: str, default: Any) -> Any: |
| 153 |
"""Read a property.""" |
| 154 |
return self.properties.get(key, default) if self.properties else default |
| 155 |
|
| 156 |
def write_property(self, key: str, value: Any) -> None: |
| 157 |
"""Add or update a property.""" |
| 158 |
if self.properties is None: |
| 159 |
self.properties = {} |
| 160 |
|
| 161 |
self.properties[key] = value |
| 162 |
|
| 163 |
|
| 164 |
class QbusMqttOnOffState(QbusMqttState): |
| 165 |
"""MQTT representation of a Qbus on/off output.""" |
| 166 |
|
| 167 |
def __init__( |
| 168 |
self, |
| 169 |
data: dict | None = None, |
| 170 |
*, |
| 171 |
id: str | None = None, |
| 172 |
type: str | None = None, |
| 173 |
) -> None: |
| 174 |
"""Initialize state.""" |
| 175 |
super().__init__(data, id=id, type=type) |
| 176 |
|
| 177 |
def read_value(self) -> bool: |
| 178 |
"""Read the value of the Qbus output.""" |
| 179 |
return self.read_property(KEY_PROPERTIES_VALUE, False) |
| 180 |
|
| 181 |
def write_value(self, on: bool) -> None: |
| 182 |
"""Set the Qbus output on or off.""" |
| 183 |
self.write_property(KEY_PROPERTIES_VALUE, on) |
| 184 |
|
| 185 |
|
| 186 |
class QbusMqttAnalogState(QbusMqttState): |
| 187 |
"""MQTT representation of a Qbus analog output.""" |
| 188 |
|
| 189 |
def __init__( |
| 190 |
self, |
| 191 |
data: dict | None = None, |
| 192 |
*, |
| 193 |
id: str | None = None, |
| 194 |
type: str | None = None, |
| 195 |
) -> None: |
| 196 |
"""Initialize state.""" |
| 197 |
super().__init__(data, id=id, type=type) |
| 198 |
|
| 199 |
def read_percentage(self) -> float | None: |
| 200 |
"""Read the value of the Qbus output.""" |
| 201 |
return self.read_property(KEY_PROPERTIES_VALUE, None) |
| 202 |
|
| 203 |
def write_percentage(self, percentage: float) -> None: |
| 204 |
"""Set the value of the Qbus output.""" |
| 205 |
self.write_property(KEY_PROPERTIES_VALUE, percentage) |
| 206 |
|
| 207 |
def write_on_off(self, on: bool) -> None: |
| 208 |
"""Set the Qbus output on or off.""" |
| 209 |
self.action = "on" if on else "off" |
| 210 |
|
| 211 |
|
| 212 |
class QbusMqttShutterState(QbusMqttState): |
| 213 |
"""MQTT representation of a Qbus shutter output.""" |
| 214 |
|
| 215 |
def __init__( |
| 216 |
self, |
| 217 |
data: dict | None = None, |
| 218 |
*, |
| 219 |
id: str | None = None, |
| 220 |
type: str | None = None, |
| 221 |
) -> None: |
| 222 |
"""Initialize state.""" |
| 223 |
super().__init__(data, id=id, type=type) |
| 224 |
|
| 225 |
def read_state(self) -> str | None: |
| 226 |
"""Read the state of the Qbus output.""" |
| 227 |
return self.read_property(KEY_PROPERTIES_STATE, None) |
| 228 |
|
| 229 |
def write_state(self, state: str) -> None: |
| 230 |
"""Set the state of the Qbus output.""" |
| 231 |
self.write_property(KEY_PROPERTIES_STATE, state) |
| 232 |
|
| 233 |
def read_position(self) -> int | None: |
| 234 |
"""Read the position of the Qbus output.""" |
| 235 |
return self.read_property(KEY_PROPERTIES_SHUTTER_POSITION, None) |
| 236 |
|
| 237 |
def write_position(self, percentage: int) -> None: |
| 238 |
"""Set the position of the Qbus output.""" |
| 239 |
self.write_property(KEY_PROPERTIES_SHUTTER_POSITION, percentage) |
| 240 |
|
| 241 |
def read_slat_position(self) -> int | None: |
| 242 |
"""Read the slat position of the Qbus output.""" |
| 243 |
return self.read_property(KEY_PROPERTIES_SLAT_POSITION, None) |
| 244 |
|
| 245 |
def write_slat_position(self, percentage: int) -> None: |
| 246 |
"""Set the slat position of the Qbus output.""" |
| 247 |
self.write_property(KEY_PROPERTIES_SLAT_POSITION, percentage) |
| 248 |
|
| 249 |
|
| 250 |
class QbusMqttThermoState(QbusMqttState): |
| 251 |
"""MQTT representation of a Qbus thermo output.""" |
| 252 |
|
| 253 |
def __init__( |
| 254 |
self, |
| 255 |
data: dict | None = None, |
| 256 |
*, |
| 257 |
id: str | None = None, |
| 258 |
type: str | None = None, |
| 259 |
) -> None: |
| 260 |
"""Initialize state.""" |
| 261 |
super().__init__(data, id=id, type=type) |
| 262 |
|
| 263 |
def read_current_temperature(self) -> float | None: |
| 264 |
"""Read the current temperature of the Qbus output.""" |
| 265 |
return self.read_property(KEY_PROPERTIES_CURRENT_TEMPERATURE, None) |
| 266 |
|
| 267 |
def read_set_temperature(self) -> float | None: |
| 268 |
"""Read the set temperature of the Qbus output.""" |
| 269 |
return self.read_property(KEY_PROPERTIES_SET_TEMPERATURE, None) |
| 270 |
|
| 271 |
def write_set_temperature(self, temperature: float) -> None: |
| 272 |
"""Set the set temperature of the Qbus output.""" |
| 273 |
self.write_property(KEY_PROPERTIES_SET_TEMPERATURE, temperature) |
| 274 |
|
| 275 |
def read_regime(self) -> str | None: |
| 276 |
"""Read the regime of the Qbus output.""" |
| 277 |
return self.read_property(KEY_PROPERTIES_REGIME, None) |
| 278 |
|
| 279 |
def write_regime(self, regime: str) -> None: |
| 280 |
"""Set the regime of the Qbus output.""" |
| 281 |
self.write_property(KEY_PROPERTIES_REGIME, regime) |
| 282 |
|
| 283 |
|
| 284 |
class QbusMqttGaugeState(QbusMqttState): |
| 285 |
"""MQTT representation of a Qbus gauge output.""" |
| 286 |
|
| 287 |
def __init__( |
| 288 |
self, |
| 289 |
data: dict | None = None, |
| 290 |
*, |
| 291 |
id: str | None = None, |
| 292 |
type: str | None = None, |
| 293 |
) -> None: |
| 294 |
"""Initialize state.""" |
| 295 |
super().__init__(data, id=id, type=type) |
| 296 |
|
| 297 |
def read_value(self, key: GaugeStateProperty) -> float: |
| 298 |
"""Read the value of the Qbus output.""" |
| 299 |
return self.read_property(key, 0) |
| 300 |
|
| 301 |
|
| 302 |
class QbusMqttVentilationState(QbusMqttState): |
| 303 |
"""MQTT representation of a Qbus ventilation output.""" |
| 304 |
|
| 305 |
def __init__( |
| 306 |
self, |
| 307 |
data: dict | None = None, |
| 308 |
*, |
| 309 |
id: str | None = None, |
| 310 |
type: str | None = None, |
| 311 |
) -> None: |
| 312 |
"""Initialize state.""" |
| 313 |
super().__init__(data, id=id, type=type) |
| 314 |
|
| 315 |
def read_co2(self) -> float: |
| 316 |
"""Read the co2 of the Qbus output.""" |
| 317 |
return self.read_property(KEY_PROPERTIES_CO2, 0) |
| 318 |
|
| 319 |
def read_refresh(self) -> int: |
| 320 |
"""Read the co2 of the Qbus output.""" |
| 321 |
return self.read_property(KEY_PROPERTIES_REFRESH, 0) |
| 322 |
|
| 323 |
def read_regime(self) -> str | None: |
| 324 |
"""Read the regime of the Qbus output.""" |
| 325 |
return self.read_property(KEY_PROPERTIES_REGIME, None) |
| 326 |
|
| 327 |
def write_regime(self, regime: str) -> None: |
| 328 |
"""Set the regime of the Qbus output.""" |
| 329 |
self.write_property(KEY_PROPERTIES_REGIME, regime) |
| 330 |
|
| 331 |
|
| 332 |
class QbusMqttHumidityState(QbusMqttState): |
| 333 |
"""MQTT representation of a Qbus humidity output.""" |
| 334 |
|
| 335 |
def __init__( |
| 336 |
self, |
| 337 |
data: dict | None = None, |
| 338 |
*, |
| 339 |
id: str | None = None, |
| 340 |
type: str | None = None, |
| 341 |
) -> None: |
| 342 |
"""Initialize state.""" |
| 343 |
super().__init__(data, id=id, type=type) |
| 344 |
|
| 345 |
def read_value(self) -> float: |
| 346 |
"""Read the value of the Qbus output.""" |
| 347 |
return self.read_property(KEY_PROPERTIES_VALUE, 0) |
| 348 |
|
| 349 |
def read_regime(self) -> str | None: |
| 350 |
"""Read the regime of the Qbus output.""" |
| 351 |
return self.read_property(KEY_PROPERTIES_REGIME, None) |
| 352 |
|
| 353 |
def write_regime(self, regime: str) -> None: |
| 354 |
"""Set the regime of the Qbus output.""" |
| 355 |
self.write_property(KEY_PROPERTIES_REGIME, regime) |
| 356 |
|
| 357 |
|
| 358 |
class QbusMqttWeatherState(QbusMqttState): |
| 359 |
"""MQTT representation of a Qbus weather station output.""" |
| 360 |
|
| 361 |
def __init__( |
| 362 |
self, |
| 363 |
data: dict | None = None, |
| 364 |
*, |
| 365 |
id: str | None = None, |
| 366 |
type: str | None = None, |
| 367 |
) -> None: |
| 368 |
"""Initialize state.""" |
| 369 |
super().__init__(data, id=id, type=type) |
| 370 |
|
| 371 |
def read_daylight(self) -> float: |
| 372 |
"""Read the daylight status of the weather station.""" |
| 373 |
return self.read_property(WeatherStationStateProperty.DAYLIGHT, 0) |
| 374 |
|
| 375 |
def read_light(self) -> float: |
| 376 |
"""Read the light level of the weather station.""" |
| 377 |
return self.read_property(WeatherStationStateProperty.LIGHT, 0) |
| 378 |
|
| 379 |
def read_light_east(self) -> float: |
| 380 |
"""Read the east-facing light level of the weather station.""" |
| 381 |
return self.read_property(WeatherStationStateProperty.LIGHT_EAST, 0) |
| 382 |
|
| 383 |
def read_light_south(self) -> float: |
| 384 |
"""Read the south-facing light level of the weather station.""" |
| 385 |
return self.read_property(WeatherStationStateProperty.LIGHT_SOUTH, 0) |
| 386 |
|
| 387 |
def read_light_west(self) -> float: |
| 388 |
"""Read the west-facing light level of the weather station.""" |
| 389 |
return self.read_property(WeatherStationStateProperty.LIGHT_WEST, 0) |
| 390 |
|
| 391 |
def read_raining(self) -> bool: |
| 392 |
"""Read the rain status of the weather station.""" |
| 393 |
return self.read_property(WeatherStationStateProperty.RAINING, False) |
| 394 |
|
| 395 |
def read_temperature(self) -> float: |
| 396 |
"""Read the temperature from the weather station.""" |
| 397 |
return self.read_property(WeatherStationStateProperty.TEMPERATURE, 0) |
| 398 |
|
| 399 |
def read_twilight(self) -> bool: |
| 400 |
"""Read the twilight status of the weather station.""" |
| 401 |
return self.read_property(WeatherStationStateProperty.TWILIGHT, False) |
| 402 |
|
| 403 |
def read_wind(self) -> float: |
| 404 |
"""Read the wind speed from the weather station.""" |
| 405 |
return self.read_property(WeatherStationStateProperty.WIND, 0) |
| 406 |
|
| 407 |
|
| 408 |
class QbusMqttStepperState(QbusMqttState): |
| 409 |
"""MQTT representation of a Qbus stepper output.""" |
| 410 |
|
| 411 |
def __init__( |
| 412 |
self, |
| 413 |
data: dict | None = None, |
| 414 |
*, |
| 415 |
id: str | None = None, |
| 416 |
type: str | None = None, |
| 417 |
) -> None: |
| 418 |
"""Initialize state.""" |
| 419 |
super().__init__(data, id=id, type=type) |
| 420 |
|
| 421 |
def read_value(self) -> int | None: |
| 422 |
"""Read the value of the Qbus output.""" |
| 423 |
return self.read_property(KEY_PROPERTIES_VALUE, None) |
| 424 |
|
| 425 |
def write_value(self, value: int) -> None: |
| 426 |
"""Set the value of the Qbus output.""" |
| 427 |
self.write_property(KEY_PROPERTIES_VALUE, value) |
| 428 |
|
| 429 |
|
| 430 |
class QbusMqttMultiColorState(QbusMqttState): |
| 431 |
"""MQTT representation of a Qbus multi-color output.""" |
| 432 |
|
| 433 |
def __init__( |
| 434 |
self, |
| 435 |
data: dict | None = None, |
| 436 |
*, |
| 437 |
id: str | None = None, |
| 438 |
type: str | None = None, |
| 439 |
) -> None: |
| 440 |
"""Initialize state.""" |
| 441 |
super().__init__(data, id=id, type=type) |
| 442 |
|
| 443 |
def read_brightness(self) -> float | None: |
| 444 |
"""Read the brightness of the Qbus output.""" |
| 445 |
return self.read_property(MultiColorStateProperty.BRIGHTNESS, None) |
| 446 |
|
| 447 |
def write_brightness(self, value: float) -> None: |
| 448 |
"""Set the brightness of the Qbus output.""" |
| 449 |
self.write_property(MultiColorStateProperty.BRIGHTNESS, value) |
| 450 |
|
| 451 |
def read_current_regime(self) -> MultiColorRegime | None: |
| 452 |
"""Read the current regime of the Qbus output.""" |
| 453 |
value = self.read_property(MultiColorStateProperty.CURRENT_REGIME, None) |
| 454 |
return MultiColorRegime(value) if value is not None else None |
| 455 |
|
| 456 |
def write_current_regime(self, value: MultiColorRegime) -> None: |
| 457 |
"""Set the current regime of the Qbus output.""" |
| 458 |
self.write_property(MultiColorStateProperty.CURRENT_REGIME, value.value) |
| 459 |
|
| 460 |
def read_hue(self) -> float | None: |
| 461 |
"""Read the hue of the Qbus output.""" |
| 462 |
return self.read_property(MultiColorStateProperty.HUE, None) |
| 463 |
|
| 464 |
def write_hue(self, value: float) -> None: |
| 465 |
"""Set the hue of the Qbus output.""" |
| 466 |
self.write_property(MultiColorStateProperty.HUE, value) |
| 467 |
|
| 468 |
def read_on(self) -> bool: |
| 469 |
"""Read the on/off state of the Qbus output.""" |
| 470 |
return self.read_property(MultiColorStateProperty.ON, False) |
| 471 |
|
| 472 |
def write_on(self, value: bool) -> None: |
| 473 |
"""Set the Qbus output on or off.""" |
| 474 |
self.write_property(MultiColorStateProperty.ON, value) |
| 475 |
|
| 476 |
def read_preset_color(self) -> int | None: |
| 477 |
"""Read the preset color of the Qbus output.""" |
| 478 |
return self.read_property(MultiColorStateProperty.PRESET_COLOR, None) |
| 479 |
|
| 480 |
def write_preset_color(self, value: int) -> None: |
| 481 |
"""Set the preset color of the Qbus output.""" |
| 482 |
self.write_property(MultiColorStateProperty.PRESET_COLOR, value) |
| 483 |
|
| 484 |
def read_preset_movie(self) -> int | None: |
| 485 |
"""Read the preset movie of the Qbus output.""" |
| 486 |
return self.read_property(MultiColorStateProperty.PRESET_MOVIE, None) |
| 487 |
|
| 488 |
def write_preset_movie(self, value: int) -> None: |
| 489 |
"""Set the preset movie of the Qbus output.""" |
| 490 |
self.write_property(MultiColorStateProperty.PRESET_MOVIE, value) |
| 491 |
|
| 492 |
def read_saturation(self) -> float | None: |
| 493 |
"""Read the saturation of the Qbus output.""" |
| 494 |
return self.read_property(MultiColorStateProperty.SATURATION, None) |
| 495 |
|
| 496 |
def write_saturation(self, value: float) -> None: |
| 497 |
"""Set the saturation of the Qbus output.""" |
| 498 |
self.write_property(MultiColorStateProperty.SATURATION, value) |