| 1 |
"""Qbis discovery models.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from .const import ( |
| 6 |
KEY_OUTPUT_ACTIONS, |
| 7 |
KEY_OUTPUT_ID, |
| 8 |
KEY_OUTPUT_LOCATION, |
| 9 |
KEY_OUTPUT_LOCATION_ID, |
| 10 |
KEY_OUTPUT_NAME, |
| 11 |
KEY_OUTPUT_PROPERTIES, |
| 12 |
KEY_OUTPUT_REF_ID, |
| 13 |
KEY_OUTPUT_TYPE, |
| 14 |
KEY_OUTPUT_VARIANT, |
| 15 |
) |
| 16 |
|
| 17 |
KEY_DEVICES = "devices" |
| 18 |
|
| 19 |
KEY_DEVICE_FUNCTIONBLOCKS = "functionBlocks" |
| 20 |
KEY_DEVICE_ID = "id" |
| 21 |
KEY_DEVICE_IP = "ip" |
| 22 |
KEY_DEVICE_MAC = "mac" |
| 23 |
KEY_DEVICE_NAME = "name" |
| 24 |
KEY_DEVICE_SERIAL_NR = "serialNr" |
| 25 |
KEY_DEVICE_TYPE = "type" |
| 26 |
KEY_DEVICE_VERSION = "version" |
| 27 |
|
| 28 |
|
| 29 |
class QbusMqttOutput: |
| 30 |
"""MQTT representation of a Qbus output.""" |
| 31 |
|
| 32 |
def __init__(self, data: dict, device: QbusMqttDevice) -> None: |
| 33 |
"""Initialize based on a json loaded dictionary.""" |
| 34 |
self._data = data |
| 35 |
self._device = device |
| 36 |
|
| 37 |
@property |
| 38 |
def id(self) -> str: |
| 39 |
"""Return the id.""" |
| 40 |
return self._data.get(KEY_OUTPUT_ID) or "" |
| 41 |
|
| 42 |
@property |
| 43 |
def type(self) -> str: |
| 44 |
"""Return the type.""" |
| 45 |
return self._data.get(KEY_OUTPUT_TYPE) or "" |
| 46 |
|
| 47 |
@property |
| 48 |
def name(self) -> str: |
| 49 |
"""Return the name.""" |
| 50 |
return self._data.get(KEY_OUTPUT_NAME) or "" |
| 51 |
|
| 52 |
@property |
| 53 |
def ref_id(self) -> str: |
| 54 |
"""Return the ref id.""" |
| 55 |
return self._data.get(KEY_OUTPUT_REF_ID) or "" |
| 56 |
|
| 57 |
@property |
| 58 |
def properties(self) -> dict: |
| 59 |
"""Return the properties.""" |
| 60 |
return self._data.get(KEY_OUTPUT_PROPERTIES) or {} |
| 61 |
|
| 62 |
@property |
| 63 |
def actions(self) -> dict: |
| 64 |
"""Return the actions.""" |
| 65 |
return self._data.get(KEY_OUTPUT_ACTIONS) or {} |
| 66 |
|
| 67 |
@property |
| 68 |
def location(self) -> str: |
| 69 |
"""Return the location.""" |
| 70 |
return self._data.get(KEY_OUTPUT_LOCATION) or "" |
| 71 |
|
| 72 |
@property |
| 73 |
def location_id(self) -> int: |
| 74 |
"""Return the location id.""" |
| 75 |
return self._data.get(KEY_OUTPUT_LOCATION_ID) or 0 |
| 76 |
|
| 77 |
@property |
| 78 |
def variant(self) -> str | tuple | list: |
| 79 |
"""Return the variant.""" |
| 80 |
value = self._data.get(KEY_OUTPUT_VARIANT) or "" |
| 81 |
|
| 82 |
if isinstance(value, list | tuple): |
| 83 |
value = [x for x in value if x is not None] |
| 84 |
|
| 85 |
return value |
| 86 |
|
| 87 |
@property |
| 88 |
def device(self) -> QbusMqttDevice: |
| 89 |
"""Return the device.""" |
| 90 |
return self._device |
| 91 |
|
| 92 |
|
| 93 |
class QbusMqttDevice: |
| 94 |
"""MQTT representation of a Qbus device.""" |
| 95 |
|
| 96 |
def __init__(self, data: dict) -> None: |
| 97 |
"""Initialize based on a json loaded dictionary.""" |
| 98 |
self._data = data |
| 99 |
|
| 100 |
@property |
| 101 |
def id(self) -> str: |
| 102 |
"""Return the id.""" |
| 103 |
return self._data.get(KEY_DEVICE_ID) or "" |
| 104 |
|
| 105 |
@property |
| 106 |
def ip(self) -> str: |
| 107 |
"""Return the ip address.""" |
| 108 |
return self._data.get(KEY_DEVICE_IP) or "" |
| 109 |
|
| 110 |
@property |
| 111 |
def mac(self) -> str: |
| 112 |
"""Return the ip address.""" |
| 113 |
return self._data.get(KEY_DEVICE_MAC) or "" |
| 114 |
|
| 115 |
@property |
| 116 |
def name(self) -> str: |
| 117 |
"""Return the ip address.""" |
| 118 |
return self._data.get(KEY_DEVICE_NAME) or "" |
| 119 |
|
| 120 |
@property |
| 121 |
def serial_number(self) -> str: |
| 122 |
"""Return the serial number.""" |
| 123 |
return self._data.get(KEY_DEVICE_SERIAL_NR) or "" |
| 124 |
|
| 125 |
@property |
| 126 |
def type(self) -> str: |
| 127 |
"""Return the mac address.""" |
| 128 |
return self._data.get(KEY_DEVICE_TYPE) or "" |
| 129 |
|
| 130 |
@property |
| 131 |
def version(self) -> str: |
| 132 |
"""Return the version.""" |
| 133 |
return self._data.get(KEY_DEVICE_VERSION) or "" |
| 134 |
|
| 135 |
@property |
| 136 |
def outputs(self) -> list[QbusMqttOutput]: |
| 137 |
"""Return the outputs.""" |
| 138 |
|
| 139 |
outputs: list[QbusMqttOutput] = [] |
| 140 |
|
| 141 |
if self._data.get(KEY_DEVICE_FUNCTIONBLOCKS): |
| 142 |
outputs = [QbusMqttOutput(x, self) for x in self._data[KEY_DEVICE_FUNCTIONBLOCKS]] |
| 143 |
|
| 144 |
return outputs |
| 145 |
|
| 146 |
|
| 147 |
class QbusDiscovery: |
| 148 |
"""MQTT representation of a Qbus config.""" |
| 149 |
|
| 150 |
def __init__(self, data: dict) -> None: |
| 151 |
"""Initialize based on a json loaded dictionary.""" |
| 152 |
if KEY_DEVICES in data: |
| 153 |
self._devices = [QbusMqttDevice(x) for x in data[KEY_DEVICES]] |
| 154 |
|
| 155 |
self._name: str = data["app"] |
| 156 |
|
| 157 |
def get_device_by_id(self, id: str) -> QbusMqttDevice | None: |
| 158 |
"""Get the device by id.""" |
| 159 |
return next((x for x in self.devices if x.id.casefold() == id.casefold()), None) |
| 160 |
|
| 161 |
def get_device_by_serial(self, serial: str) -> QbusMqttDevice | None: |
| 162 |
"""Get the device by serial.""" |
| 163 |
return next( |
| 164 |
(x for x in self.devices if x.serial_number.casefold() == serial.casefold()), |
| 165 |
None, |
| 166 |
) |
| 167 |
|
| 168 |
@property |
| 169 |
def devices(self) -> list[QbusMqttDevice]: |
| 170 |
"""Return the devices.""" |
| 171 |
return self._devices |
| 172 |
|
| 173 |
@property |
| 174 |
def name(self) -> str: |
| 175 |
"""Return device name.""" |
| 176 |
return self._name |