def to_bytes(self, fixed_header: MQTTFixedHeader, variable_header: ConnectVariableHeader): out = bytearray() # Client identifier out.extend(encode_string(self.client_id)) # Will topic / message if variable_header.will_flag: out.extend(encode_string(self.will_topic)) out.extend(encode_data_with_length(self.will_message)) # username if variable_header.username_flag: out.extend(encode_string(self.username)) # password if variable_header.password_flag: out.extend(encode_string(self.password)) return out