def extract_from_frame(data): """Extract payload and command from frame.""" if len(data) <= 4: raise PyVLXException("could_not_extract_from_frame_too_short", data=data) length = data[0] * 256 + data[1] - 1 if len(data) != length + 3: raise PyVLXException( "could_not_extract_from_frame_invalid_length", data=data, current_length=len(data), expected_length=length + 3, ) if calc_crc(data[:-1]) != data[-1]: raise PyVLXException( "could_not_extract_from_frame_invalid_crc", data=data, expected_crc=calc_crc(data[:-1]), current_crc=data[-1], ) payload = data[4:-1] try: command = Command(data[2] * 256 + data[3]) except ValueError as type_error: raise PyVLXException("could_not_extract_from_frame_command", data=data) from type_error return command, payload
def get_payload(self): """Return Payload.""" if self.password is None: raise PyVLXException("password is none") if len(self.password) > self.MAX_SIZE: raise PyVLXException("password is too long") return string_to_bytes(self.password, self.MAX_SIZE)
def parse_raw(self, raw): """Parse alias array from raw bytes.""" if not isinstance(raw, bytes): raise PyVLXException("AliasArray::invalid_type_if_raw", type_raw=type(raw)) if len(raw) != 21: raise PyVLXException("AliasArray::invalid_size", size=len(raw)) nbr_of_alias = raw[0] if nbr_of_alias > 5: raise PyVLXException("AliasArray::invalid_nbr_of_alias", nbr_of_alias=nbr_of_alias) for i in range(0, nbr_of_alias): self.alias_array_.append( (raw[i * 4 + 1:i * 4 + 3], raw[i * 4 + 3:i * 4 + 5]))
def test_str_with_parameter(self): """Test string representation of PyVLXException with parameter.""" exception = PyVLXException("fnord fnord", fnord="fnord", bla="blub") self.assertEqual( str(exception), '<PyVLXException description="fnord fnord" bla="blub" fnord="fnord"/>', )
def serial_number(self, serial_number): """Set serial number.""" if serial_number is None: self._serial_number = bytes(8) return self._serial_number = b"" for elem in serial_number.split(":"): self._serial_number += bytes.fromhex(elem) if len(self._serial_number) != 8: raise PyVLXException("could_not_parse_serial_number")
def validate_payload_len(self, payload): """Validate payload len.""" if not hasattr(self, "PAYLOAD_LEN"): # No fixed payload len, e.g. within FrameGetSceneListNotification return # pylint: disable=no-member if len(payload) != self.PAYLOAD_LEN: raise PyVLXException("Invalid payload len", expected_len=self.PAYLOAD_LEN, current_len=len(payload), frame_type=type(self).__name__)
def from_payload(self, payload): """Init frame from binary data.""" number_of_objects = payload[0] self.remaining_scenes = payload[-1] predicted_len = number_of_objects * 65 + 2 if len(payload) != predicted_len: raise PyVLXException('scene_list_notification_wrong_length') self.scenes = [] for i in range(number_of_objects): scene = payload[(i*65+1):(i*65+66)] number = scene[0] name = bytes_to_string(scene[1:]) self.scenes.append((number, name))
def from_payload(self, payload): """Init frame from binary data.""" self.session_id = payload[0] * 256 + payload[1] len_node_ids = payload[2] if len_node_ids > 20: raise PyVLXException("command_send_request_wrong_node_length") self.node_ids = [] for i in range(len_node_ids): self.node_ids.append(payload[3] + i) self.status_type = StatusType(payload[23]) self.fpi1 = payload[24] self.fpi2 = payload[25]
def from_payload(self, payload): """Init frame from binary data.""" self.session_id = payload[0] * 256 + payload[1] self.originator = Originator(payload[2]) self.priority = Priority(payload[3]) len_node_ids = payload[41] if len_node_ids > 20: raise PyVLXException("command_send_request_wrong_node_length") self.node_ids = [] for i in range(len_node_ids): self.node_ids.append(payload[42] + i) self.parameter = Parameter(payload[7:9])
def test_str(self): """Test string representation of PyVLXException.""" exception = PyVLXException("fnord fnord") self.assertEqual(str(exception), '<PyVLXException description="fnord fnord" />')
async def house_status_monitor_disable(pyvlx): """Disable house status monitor.""" status_monitor_disable = HouseStatusMonitorDisable(pyvlx=pyvlx) await status_monitor_disable.do_api_call() if not status_monitor_disable.success: raise PyVLXException("Unable disable house status monitor.")